mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-09 13:35:01 +08:00
add clean django-stubs folder, add some tests
This commit is contained in:
0
django-stubs/__init__.pyi
Normal file
0
django-stubs/__init__.pyi
Normal file
0
django-stubs/contrib/__init__.pyi
Normal file
0
django-stubs/contrib/__init__.pyi
Normal file
0
django-stubs/contrib/postgres/__init__.pyi
Normal file
0
django-stubs/contrib/postgres/__init__.pyi
Normal file
1
django-stubs/contrib/postgres/fields/__init__.pyi
Normal file
1
django-stubs/contrib/postgres/fields/__init__.pyi
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .array import ArrayField as ArrayField
|
||||||
11
django-stubs/contrib/postgres/fields/array.pyi
Normal file
11
django-stubs/contrib/postgres/fields/array.pyi
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from typing import List, Any
|
||||||
|
|
||||||
|
from django.contrib.postgres.fields.mixins import CheckFieldDefaultMixin
|
||||||
|
from django.db.models import Field
|
||||||
|
|
||||||
|
|
||||||
|
class ArrayField(CheckFieldDefaultMixin, Field):
|
||||||
|
def __init__(self,
|
||||||
|
base_field: Field,
|
||||||
|
**kwargs): ...
|
||||||
|
def __get__(self, instance, owner) -> List[Any]: ...
|
||||||
2
django-stubs/contrib/postgres/fields/mixins.pyi
Normal file
2
django-stubs/contrib/postgres/fields/mixins.pyi
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
class CheckFieldDefaultMixin:
|
||||||
|
pass
|
||||||
0
django-stubs/db/__init__.pyi
Normal file
0
django-stubs/db/__init__.pyi
Normal file
6
django-stubs/db/models/__init__.pyi
Normal file
6
django-stubs/db/models/__init__.pyi
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from .base import Model as Model
|
||||||
|
|
||||||
|
from .fields import (AutoField as AutoField,
|
||||||
|
IntegerField as IntegerField,
|
||||||
|
CharField as CharField,
|
||||||
|
Field as Field)
|
||||||
6
django-stubs/db/models/base.pyi
Normal file
6
django-stubs/db/models/base.pyi
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
class ModelBase(type):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Model(metaclass=ModelBase):
|
||||||
|
pass
|
||||||
26
django-stubs/db/models/fields/__init__.pyi
Normal file
26
django-stubs/db/models/fields/__init__.pyi
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from django.db.models.query_utils import RegisterLookupMixin
|
||||||
|
|
||||||
|
|
||||||
|
class Field(RegisterLookupMixin):
|
||||||
|
def __init__(self,
|
||||||
|
primary_key: bool = False,
|
||||||
|
**kwargs): ...
|
||||||
|
def __get__(self, instance, owner) -> Any: ...
|
||||||
|
|
||||||
|
|
||||||
|
class IntegerField(Field):
|
||||||
|
def __get__(self, instance, owner) -> int: ...
|
||||||
|
|
||||||
|
|
||||||
|
class AutoField(Field):
|
||||||
|
def __get__(self, instance, owner) -> int: ...
|
||||||
|
|
||||||
|
|
||||||
|
class CharField(Field):
|
||||||
|
def __init__(self,
|
||||||
|
max_length: int,
|
||||||
|
**kwargs): ...
|
||||||
|
def __get__(self, instance, owner) -> str: ...
|
||||||
|
|
||||||
2
django-stubs/db/models/query_utils.pyi
Normal file
2
django-stubs/db/models/query_utils.pyi
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
class RegisterLookupMixin:
|
||||||
|
pass
|
||||||
11
mypy_django_plugin/plugins/postgres_fields.py
Normal file
11
mypy_django_plugin/plugins/postgres_fields.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from mypy.plugin import Plugin, ClassDefContext
|
||||||
|
|
||||||
|
|
||||||
|
def determine_type_of_array_field(context: ClassDefContext) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PostgresFieldsPlugin(Plugin):
|
||||||
|
def get_base_class_hook(self, fullname: str
|
||||||
|
):
|
||||||
|
return determine_type_of_array_field
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
[case testBasicCheck]
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
class A:
|
|
||||||
pass
|
|
||||||
|
|
||||||
class B(A):
|
|
||||||
pass
|
|
||||||
[out]
|
|
||||||
12
test/test-data/check-model-fields.test
Normal file
12
test/test-data/check-model-fields.test
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[case testBasicModelFields]
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class User(models.Model):
|
||||||
|
id = models.AutoField(primary_key=True)
|
||||||
|
name = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
user = User()
|
||||||
|
reveal_type(user.id) # E: Revealed type is 'builtins.int'
|
||||||
|
reveal_type(user.name) # E: Revealed type is 'builtins.str'
|
||||||
|
[out]
|
||||||
13
test/test-data/check-postgres-fields.test
Normal file
13
test/test-data/check-postgres-fields.test
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
[case testArrayField]
|
||||||
|
import typing
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib.postgres.fields import ArrayField
|
||||||
|
|
||||||
|
|
||||||
|
class User(models.Model):
|
||||||
|
members = ArrayField(base_field=models.IntegerField())
|
||||||
|
|
||||||
|
user = User()
|
||||||
|
reveal_type(user.members) # E: Revealed type is 'typing.List[int]'
|
||||||
|
[out]
|
||||||
@@ -13,6 +13,7 @@ TEST_DATA_DIR = ROOT_DIR / 'test' / 'test-data'
|
|||||||
|
|
||||||
class DjangoTestSuite(DataSuite):
|
class DjangoTestSuite(DataSuite):
|
||||||
files = [
|
files = [
|
||||||
|
'check-model-fields.test',
|
||||||
'check-postgres-fields.test'
|
'check-postgres-fields.test'
|
||||||
]
|
]
|
||||||
data_prefix = str(TEST_DATA_DIR)
|
data_prefix = str(TEST_DATA_DIR)
|
||||||
|
|||||||
Reference in New Issue
Block a user