mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-14 07:47:09 +08:00
Make AddRelatedManagers look for "objects" on parent model (#730)
* Add failing test for relation to model inheriting `objects` Fails with: ``` pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output: Expected: main:2: note: Revealed type is "myapp.models.MyUser*" main:3: note: Revealed type is "myapp.models.MyUser*" <45 (diff) <45 (diff) Actual: main:2: note: Revealed type is "myapp.models.MyUser*" main:3: note: Revealed type is "myapp.models.MyUser*" main:6: error: "MyUser" has no attribute "book_set" (diff) main:6: note: Revealed type is "Any" (diff) main:7: error: "MyUser" has no attribute "article_set" (diff) main:7: note: Revealed type is "Any" (diff) ``` * Make AddRelatedManagers look for "objects" on parent model Previously, AddRelatedManagers would fail if a related model had inherited its `objects` field from a parent class. This would result in missing relation attributes. This is fixed by using `get()` instead of `names`; the former searches the MRO for the symbol, whereas the latter only looks for symbols declared directly on the class.
This commit is contained in:
@@ -273,7 +273,8 @@ class AddRelatedManagers(ModelClassInitializer):
|
||||
related_manager_info = self.lookup_typeinfo_or_incomplete_defn_error(
|
||||
fullnames.RELATED_MANAGER_CLASS
|
||||
) # noqa: E501
|
||||
if "objects" not in related_model_info.names:
|
||||
objects = related_model_info.get("objects")
|
||||
if not objects:
|
||||
raise helpers.IncompleteDefnException()
|
||||
except helpers.IncompleteDefnException as exc:
|
||||
if not self.api.final_iteration:
|
||||
@@ -283,7 +284,7 @@ class AddRelatedManagers(ModelClassInitializer):
|
||||
|
||||
# create new RelatedManager subclass
|
||||
parametrized_related_manager_type = Instance(related_manager_info, [Instance(related_model_info, [])])
|
||||
default_manager_type = related_model_info.names["objects"].type
|
||||
default_manager_type = objects.type
|
||||
if default_manager_type is None:
|
||||
default_manager_type = self.try_generate_related_manager(related_model_cls, related_model_info)
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user