-
-
Notifications
You must be signed in to change notification settings - Fork 496
Update Apps.get_model() return type #1318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
@@ -24,7 +26,7 @@ class Apps: | |||
def get_app_config(self, app_label: str) -> AppConfig: ... | |||
# it's not possible to support it in plugin properly now | |||
def get_models(self, include_auto_created: bool = ..., include_swapped: bool = ...) -> list[type[Model]]: ... | |||
def get_model(self, app_label: str, model_name: str | None = ..., require_ready: bool = ...) -> type[Any]: ... | |||
def get_model(self, app_label: str, model_name: str | None = ..., require_ready: bool = ...) -> ModelType: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. I don't think it can work like this. The problem is that the model class name is passed in as string to model_name
argument.
With this construction I suspect it will end up being type[Any]
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works with the static type checker built into pycharm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all you want is the But you usually need the correct Model subclass, for example It used to be d53121b#diff-90641780bd3e00fc85922341c39871903999c6e562992cfc2b065550de1debafL26-R28 I'm personally undecided whether |
This specific annotation could be automatically generated, or I just put something in as I didn't focus much of this specific method. Proper implementation should have custom support from the codebase, it's not typeable just from annotations. See |
For the PyCharm: interop with PyCharm is extremely hard without close collaboration, they have all kind of special-casing for the models inside the plugin. So do we. If we want to support it properly, we need to design it together, let's maybe ask |
If I'm understanding pep484 correctly, the problem with def some_function(model: SomeModel):
pass
cls = apps.get_model('aws_manager', 'SomeModel')
some_function(cls) # type check error here The type checker would report: "Expected type 'SomeModel', got 'Type[Model | Model]' instead" when calling Using TypeVar with the bound parameter tells the type checker that the return type can be a subclass of That being said, type[Model] would definitely be preferred over type[Any], because it at least it would allow you to access the manager without reporting an error. |
I did some tests with this on mypy and PyCharm.
You can tell that mypy doesn't handle it correctly because it doesn't report errors here, it's being treated as from django.apps.registry import Apps
def takes_int(value: int) -> None:
pass
def bla(apps: Apps) -> None:
model_type = apps.get_model("auth", "User")
reveal_type(model_type) # Mypy: note: Revealed type is "ModelType?"
takes_int(model_type) # <-- This should be error, but no error is reported
model_type.no_such_attribute = 123 # <-- No error reported If I change it like this: -ModelType = type[TypeVar("ModelType", bound=Model)]
+ModelType = TypeVar("ModelType", bound=type[Model]) Then mypy actually does something slightly better, it asks for an explicit type hint for def bla(apps: Apps) -> None:
model_type = apps.get_model("auth", "User") # Mypy: error: Need type annotation for "model_type"
reveal_type(model_type) # Mypy: note: Revealed type is "Any"
takes_int(model_type) # <-- Still no error reported But I can hint it as def bla(apps: Apps) -> None:
model_type: int = apps.get_model("auth", "User") # <-- No error
reveal_type(model_type) # Mypy: note: Revealed type is "builtins.int"
takes_int(model_type) # <-- Also no error Arguably it should infer the type as For now, all of these seem to boil down to being equivalent to Is there anything I'm missing? |
As for why this change causes PyCharm to offer better autocompletion suggestions, I don't know, but it doesn't seem to affect the type checking. |
I think this solves the problem for everyone.
Related issues