jazzband / djangorestframework-simplejwt

A JSON Web Token authentication plugin for the Django REST Framework.
https://django-rest-framework-simplejwt.readthedocs.io/
MIT License
3.98k stars 660 forks source link

Incorrect typing for refresh token. #821

Open proofit404 opened 1 month ago

proofit404 commented 1 month ago

Let say we have code block like this:


def create_token(profile_id: ProfileId) -> Token:
    user = User.objects.get(profiletable=profile_id)
    refresh = RefreshToken.for_user(user)
    return str(refresh.access_token)

That would produce error in mypy:

venv/lib/python3.12/site-packages/web/repositories/token.py: note: In function "create_token":
venv/lib/python3.12/site-packages/web/repositories/token.py:13:16: error: "Token" has no attribute "access_token" 
[attr-defined]
        return str(refresh.access_token)
                   ^~~~~~~~~~~~~~~~~~~~

Token.for_user class method returns cls() which allows subclasses returns it's own instances. But annotations says it would always be a Token.

Maybe typing.Self would work?

sterliakov commented 3 weeks ago

This should be resolved after #776 (not released yet) - please try the master branch version to confirm.

sterliakov commented 3 weeks ago

And duplicates #794.