Skip to content
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

"Type[Model]" has no attribute "objects" (again?) #83

Closed
kam1sh opened this issue Jun 1, 2019 · 13 comments · Fixed by #109
Closed

"Type[Model]" has no attribute "objects" (again?) #83

kam1sh opened this issue Jun 1, 2019 · 13 comments · Fixed by #109

Comments

@kam1sh
Copy link

kam1sh commented Jun 1, 2019

Hello. I tried to use your package yesterday and I like it, although I've encountered one issue, when the django model I was accessing had no attribute 'objects', according to mypy. So it's like #16:

...
anchor/users/forms.py:26: error: "Type[Model]" has no attribute "objects"
anchor/users/views.py:38: error: "Type[Model]" has no attribute "objects"
...

The repo where I found that is https://github.com/kam1sh/anchor.

Also, I looked at django-stubs source code, and the class had no 'objects' field at all. Is it intended?

https://github.com/mkurnikov/django-stubs/blob/0e60b70ae4f521e3a527997d6b79e461d9245dfa/django-stubs/db/models/base.pyi#L9-L16

@mkurnikov
Copy link
Member

Try to enable plugin in mypy.ini via plugins = mypy_django_plugin.main. Let me know if it helped.

@kam1sh
Copy link
Author

kam1sh commented Jun 1, 2019

I've already enabled that, but did not commited yet =)

Also, without plugin there is no errors at all, but they showed up when I installed django-stubs (and typing with typing-extensions as its dependencies):

igor@juno [19:29:48] ~/p/ciconia> mypy anchor
<no output>
igor@juno [19:31:41] ~/p/ciconia> poetry add --dev django-stubs
Using version ^0.12.1 for django-stubs

Updating dependencies
Resolving dependencies... (0.7s)


Package operations: 3 installs, 0 updates, 0 removals

Writing lock file

  - Installing typing (3.6.6)
  - Installing typing-extensions (3.7.2)
  - Installing django-stubs (0.12.1)

igor@juno [19:26:25] ~/p/ciconia> vim setup.cfg # enabled django-stubs plugin
igor@juno [19:32:04] ~/p/anchor> mypy anchor
anchor/users/forms.py:9: error: Name 'forms.UserChangeForm.Meta' is not defined
anchor/users/forms.py:19: error: Name 'forms.UserCreationForm.Meta' is not defined
anchor/users/forms.py:26: error: "Type[Model]" has no attribute "objects"
anchor/users/views.py:38: error: "Type[Model]" has no attribute "objects"
anchor/pypi/models.py:16: error: Module 'django.shortcuts' has no attribute 'reverse'
anchor/pypi/services.py:8: note: unused 'type: ignore' comment

@mkurnikov
Copy link
Member

mkurnikov commented Jun 1, 2019

Yeah, this is Form related stuff, I don't think I tested it much. On internal codebases we only used APIs with DRF.
Probably a valid bug then.

@kam1sh
Copy link
Author

kam1sh commented Jun 1, 2019

I don't ise forms a lot too, those were created by cookiecutter template automatically.

I'll try to fix that and file a pull request tomorrow.

@bluetech
Copy link
Contributor

bluetech commented Jun 3, 2019

The errors you describe in the issue are due to the typing of get_user_model():

def get_user_model() -> Type[Model]

Indeed given just a general Model you can't know that there is an objects, and it will be useless anyway.

You have two avenues to improve this as far as I can see:

  1. Change your code to not use get_user_model but use a concrete model instead. This is only possible if you don't need to support any arbitrarily-configured user model. In your case, seems like you can just import from .models import User instead of User = get_user_model() and that will fix the problem.

  2. Improve the typing of get_user_model(). I didn't investigate this, but there might be some base class or protocol that every custom user model needs to have. I don't know if it requires objects though. Second alternative is to somehow make get_user_model() return the model that is actually configured in the AUTH_USER_MODEL setting. That would be best, but I don't know if it's possible, maybe @mkurnikov can say.

@kam1sh
Copy link
Author

kam1sh commented Jun 6, 2019

I dived in the Django sources, and AbstractUser has objects class variable (contrib/auth/models.py)

So then maybe signature should be like get_user_model() -> Type[AbstractUser] and AbstractUser have a objects = ClassVar[UserManager]?

P.S.: I found yours mypy hook that checks AUTH_USER_MODEL and extracts its value. Cool thing, didn't know about that.

@ivanrvpereira
Copy link

I use an abstract BaseModel, and for models using this BaseModel class mypy gives the "Type[Model]" has no attribute "objects"

@mkurnikov
Copy link
Member

If BaseModel does not depend on django.db.models.Model, it won't work.

@mkurnikov
Copy link
Member

If issue is indeed with get_user_model() typing, it should be fixed in #109

@ivanrvpereira
Copy link

BaseModel depends on django.db.models.Model

Screenshot 2019-07-22 14 25 01
Screenshot 2019-07-22 14 24 45
Screenshot 2019-07-22 14 24 53

@mkurnikov
Copy link
Member

mkurnikov commented Jul 22, 2019

Wait for #109, I've added default objects to models.Model there and fixed a bunch of related bugs. I would be great if you'd test it now, so that I could fix your case before merging.

UPD. Problem here, I think, is because BaseModel does not have objects itself, because it's abstract. It's just a guess though. Try new PR=)

@ivanrvpereira
Copy link

Installing #109 version fixes the reported error, but another one came up. I don't know if it is related with the version on #109
Error importing plugin 'mypy_drf_plugin.main'

@mkurnikov
Copy link
Member

mkurnikov commented Jul 24, 2019

DRF plugin is not compatible to new branch yet, sorry.

Will be at the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants