Skip to content

Commit

Permalink
Move ModelBase.objects declaration to Model.objects, for mypy 1.5.0
Browse files Browse the repository at this point in the history
mypy 1.5.0 was fixed to understand that metaclass attributes take
precedence over attributes in the regular class.  So we need to
declare `objects` in the regular class to allow it to be overridden in
subclasses.

Fixes typeddjango#1648.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Aug 11, 2023
1 parent 325006c commit 3b546a5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ModelState:
fields_cache: ModelStateFieldsCacheDescriptor

class ModelBase(type):
@property
def objects(cls: type[_Self]) -> BaseManager[_Self]: ... # type: ignore[misc]
@property
def _default_manager(cls: type[_Self]) -> BaseManager[_Self]: ... # type: ignore[misc]
@property
def _base_manager(cls: type[_Self]) -> BaseManager[_Self]: ... # type: ignore[misc]

class Model(metaclass=ModelBase):
objects: BaseManager[Self]

DoesNotExist: Final[type[ObjectDoesNotExist]]
MultipleObjectsReturned: Final[type[BaseMultipleObjectsReturned]]

Expand Down
8 changes: 0 additions & 8 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,13 @@ django.contrib.gis.db.backends.oracle.features.DatabaseFeatures.supports_toleran
django.contrib.gis.db.backends.oracle.features.DatabaseFeatures.unsupported_geojson_options
django.contrib.gis.db.backends.oracle.introspection
django.contrib.gis.db.backends.oracle.models.OracleGeometryColumns.Meta
django.contrib.gis.db.backends.oracle.models.OracleGeometryColumns.objects
django.contrib.gis.db.backends.oracle.models.OracleSpatialRefSys.Meta
django.contrib.gis.db.backends.oracle.models.OracleSpatialRefSys.objects
django.contrib.gis.db.backends.oracle.operations
django.contrib.gis.db.backends.postgis.adapter.PostGISAdapter.prepare
django.contrib.gis.db.backends.postgis.features.DatabaseFeatures.empty_intersection_returns_none
django.contrib.gis.db.backends.postgis.features.DatabaseFeatures.supports_geography
django.contrib.gis.db.backends.postgis.models.PostGISGeometryColumns.Meta
django.contrib.gis.db.backends.postgis.models.PostGISGeometryColumns.objects
django.contrib.gis.db.backends.postgis.models.PostGISSpatialRefSys.Meta
django.contrib.gis.db.backends.postgis.models.PostGISSpatialRefSys.objects
django.contrib.gis.db.backends.postgis.operations.PostGISOperations.convert_extent
django.contrib.gis.db.backends.postgis.operations.PostGISOperations.convert_extent3d
django.contrib.gis.db.backends.postgis.operations.PostGISOperator.check_geography
Expand All @@ -351,9 +347,7 @@ django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.can_alter_ge
django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.django_test_skips
django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.supports_area_geodetic
django.contrib.gis.db.backends.spatialite.models.SpatialiteGeometryColumns.Meta
django.contrib.gis.db.backends.spatialite.models.SpatialiteGeometryColumns.objects
django.contrib.gis.db.backends.spatialite.models.SpatialiteSpatialRefSys.Meta
django.contrib.gis.db.backends.spatialite.models.SpatialiteSpatialRefSys.objects
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.convert_extent
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.from_text
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.geom_lib_version
Expand Down Expand Up @@ -1295,7 +1289,6 @@ django.db.migrations.questioner.NonInteractiveMigrationQuestioner.log_lack_of_mi
django.db.migrations.recorder.MigrationRecorder.Migration.get_next_by_applied
django.db.migrations.recorder.MigrationRecorder.Migration.get_previous_by_applied
django.db.migrations.recorder.MigrationRecorder.Migration.id
django.db.migrations.recorder.MigrationRecorder.Migration.objects
django.db.migrations.serializer.ChoicesSerializer
django.db.migrations.utils.FieldReference
django.db.migrations.utils.field_is_referenced
Expand Down Expand Up @@ -1556,7 +1549,6 @@ django.db.models.base.Model.Meta
django.db.models.base.Model.add_to_class
django.db.models.base.ModelBase.__new__
django.db.models.base.ModelBase.add_to_class
django.db.models.base.ModelBase.objects
django.db.models.base.ModelStateFieldsCacheDescriptor.__get__
django.db.models.base.make_foreign_order_accessors
django.db.models.base.method_get_order
Expand Down
1 change: 1 addition & 0 deletions tests/typecheck/models/test_abstract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Recursive(parent=Recursive(parent=None))
Concrete(parent=Concrete(parent=None))
out: |
main:4: error: Access to generic instance variables via class is ambiguous
main:4: error: Unexpected attribute "parent" for model "Recursive"
main:4: error: Cannot instantiate abstract model "Recursive"
main:5: error: Unexpected attribute "parent" for model "Recursive"
Expand Down
8 changes: 6 additions & 2 deletions tests/typecheck/models/test_meta_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@
MyModel.objects.create(field=2)
# Errors:
AbstractModel() # E: Cannot instantiate abstract model "AbstractModel"
AbstractModel.objects.create() # E: Cannot instantiate abstract model "AbstractModel"
AbstractModel()
AbstractModel.objects.create()
out: |
main:8: error: Cannot instantiate abstract model "AbstractModel"
main:9: error: Access to generic instance variables via class is ambiguous
main:9: error: Cannot instantiate abstract model "AbstractModel"
installed_apps:
- myapp
files:
Expand Down

0 comments on commit 3b546a5

Please sign in to comment.