diff --git a/django-stubs/__init__.pyi b/django-stubs/__init__.pyi new file mode 100644 index 0000000..e69de29 diff --git a/django-stubs/contrib/__init__.pyi b/django-stubs/contrib/__init__.pyi new file mode 100644 index 0000000..e69de29 diff --git a/django-stubs/contrib/postgres/__init__.pyi b/django-stubs/contrib/postgres/__init__.pyi new file mode 100644 index 0000000..e69de29 diff --git a/django-stubs/contrib/postgres/fields/__init__.pyi b/django-stubs/contrib/postgres/fields/__init__.pyi new file mode 100644 index 0000000..879d1b7 --- /dev/null +++ b/django-stubs/contrib/postgres/fields/__init__.pyi @@ -0,0 +1 @@ +from .array import ArrayField as ArrayField \ No newline at end of file diff --git a/django-stubs/contrib/postgres/fields/array.pyi b/django-stubs/contrib/postgres/fields/array.pyi new file mode 100644 index 0000000..c348fc0 --- /dev/null +++ b/django-stubs/contrib/postgres/fields/array.pyi @@ -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]: ... \ No newline at end of file diff --git a/django-stubs/contrib/postgres/fields/mixins.pyi b/django-stubs/contrib/postgres/fields/mixins.pyi new file mode 100644 index 0000000..a92ab36 --- /dev/null +++ b/django-stubs/contrib/postgres/fields/mixins.pyi @@ -0,0 +1,2 @@ +class CheckFieldDefaultMixin: + pass \ No newline at end of file diff --git a/django-stubs/db/__init__.pyi b/django-stubs/db/__init__.pyi new file mode 100644 index 0000000..e69de29 diff --git a/django-stubs/db/models/__init__.pyi b/django-stubs/db/models/__init__.pyi new file mode 100644 index 0000000..796e6af --- /dev/null +++ b/django-stubs/db/models/__init__.pyi @@ -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) \ No newline at end of file diff --git a/django-stubs/db/models/base.pyi b/django-stubs/db/models/base.pyi new file mode 100644 index 0000000..1b044c2 --- /dev/null +++ b/django-stubs/db/models/base.pyi @@ -0,0 +1,6 @@ +class ModelBase(type): + pass + + +class Model(metaclass=ModelBase): + pass \ No newline at end of file diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi new file mode 100644 index 0000000..d57bff7 --- /dev/null +++ b/django-stubs/db/models/fields/__init__.pyi @@ -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: ... + diff --git a/django-stubs/db/models/query_utils.pyi b/django-stubs/db/models/query_utils.pyi new file mode 100644 index 0000000..de4769f --- /dev/null +++ b/django-stubs/db/models/query_utils.pyi @@ -0,0 +1,2 @@ +class RegisterLookupMixin: + pass \ No newline at end of file diff --git a/mypy_django_plugin/plugins/postgres_fields.py b/mypy_django_plugin/plugins/postgres_fields.py new file mode 100644 index 0000000..838fddd --- /dev/null +++ b/mypy_django_plugin/plugins/postgres_fields.py @@ -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 diff --git a/test/test-data/basic-check.test b/test/test-data/basic-check.test deleted file mode 100644 index 3bed52b..0000000 --- a/test/test-data/basic-check.test +++ /dev/null @@ -1,9 +0,0 @@ -[case testBasicCheck] -from typing import Any - -class A: - pass - -class B(A): - pass -[out] \ No newline at end of file diff --git a/test/test-data/check-model-fields.test b/test/test-data/check-model-fields.test new file mode 100644 index 0000000..d1cca41 --- /dev/null +++ b/test/test-data/check-model-fields.test @@ -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] diff --git a/test/test-data/check-postgres-fields.test b/test/test-data/check-postgres-fields.test new file mode 100644 index 0000000..9c86871 --- /dev/null +++ b/test/test-data/check-postgres-fields.test @@ -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] \ No newline at end of file diff --git a/test/testdjango.py b/test/testdjango.py index 76010f1..7f099ec 100644 --- a/test/testdjango.py +++ b/test/testdjango.py @@ -13,6 +13,7 @@ TEST_DATA_DIR = ROOT_DIR / 'test' / 'test-data' class DjangoTestSuite(DataSuite): files = [ + 'check-model-fields.test', 'check-postgres-fields.test' ] data_prefix = str(TEST_DATA_DIR)