Skip to content

Type of model is not model's class #40

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

Closed
phwh opened this issue Mar 5, 2019 · 3 comments
Closed

Type of model is not model's class #40

phwh opened this issue Mar 5, 2019 · 3 comments

Comments

@phwh
Copy link

phwh commented Mar 5, 2019

First of all, django-stubs is an amazing project. I built some workarounds for typing Django myself but this project is so much more comprehensive and better. So thank you very much for your work!

However, I have run into my first issue:

For the following code

class Comment(models.Model):
    # fields
    pass

class OtherModel(models.Model):
    comments = GenericRelation(Comment)

mypy complains with the following error message:

error: Argument 1 to "GenericRelation" has incompatible type "Type[Comment]"; expected "Union[Type[Model], str]"

Expecting Union[Type[Model], str] is completely correct imho. But since Comment is a subclass of Model everything should be fine.

In order to find the underlying problem I inserted an reveal_type(Comment) call which gave me the following output:

Revealed type is 'def (**kwargs: Any) -> comments.models.Comment'

Maybe this is totally fine and due to the internals of the plugin. But I expected it to be Type[Comment] which could also be the reason why mypy doesn't recognize it as compatible to Type[Model].

(What I don't understand is why mypy says Type[Comment] in the first error message which contradicts the 'revealed type' output)

@phwh phwh changed the title Type of models is not model's class Type of model is not model's class Mar 5, 2019
@mkurnikov
Copy link
Member

Technically, Type[Comment] is a callable, you can run Comment(*args) and it will return objects of type Comment, which is what mypy shows in the reveal_type.

I tried to reproduce, but had no luck. Could you provide failing test case in the form of

[CASE my_test_case]
class MyClass:
    def method(self) -> str:
        pass
reveal_type(MyClass().method())  # E: Revealed type is 'builtins.str'
[/CASE]

You can look at the plugin test suite for the format and examples
https://github.com/mkurnikov/django-stubs/tree/master/test-data/typecheck

@phwh
Copy link
Author

phwh commented Mar 11, 2019

Thanks, I will try to reproduce.

@phwh
Copy link
Author

phwh commented Mar 11, 2019

Couldn't reproduce. The test case I wrote didn't throw the error.

I probably have a Django model stub in a pyi file somewhere in my code that interfered with mypy-stubs.

Sorry for the false alarm and keep up the good work!

@phwh phwh closed this as completed Mar 11, 2019
@TonyRippy TonyRippy mentioned this issue Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants