Skip to content

Incorrect type hints for several classes/methods #787

@mlazar-endear

Description

@mlazar-endear

Hi!

I just updated my version to djangorestframework-simplejwt==5.3.1 and I'm running into several mypy errors that were introduced with the recent typehints PR here: #683

I've looked into it, and there are several incorrect types in this library. Many of them have are arguments/return values which are annotated as Token, but in reality should be str or bytes. Some of them are easy to spot (and should have been caught by mypy when adding the types in the first place). Some of them are more difficult because of the dynamic nature of the library (e.g. AUTH_TOKEN_CLASSES), but they raise errors when you start writing subclasses for the tokens and backends.

For example, this line passes a Token object into the underlying jwt library, which is incorrect (should be str or bytes).

return jwt.decode(
token,

Same thing here, passes a Token object but it should be str or bytes.

if self.jwks_client:
try:
return self.jwks_client.get_signing_key_from_jwt(token).key

Here, the raw_token is correctly annotated, but then it's being passed into an AuthToken initializer which is expecting the argument to be Token.

def get_validated_token(self, raw_token: bytes) -> Token:
"""
Validates an encoded JSON web token and returns a validated token
wrapper object.
"""
messages = []
for AuthToken in api_settings.AUTH_TOKEN_CLASSES:
try:
return AuthToken(raw_token)

This initializer is wrong (Why would the token class be initialized with an instance of itself?)

class Token:
"""
A class which validates and wraps an existing JWT or can be used to build a
new JWT.
"""
token_type: Optional[str] = None
lifetime: Optional[timedelta] = None
def __init__(self, token: Optional["Token"] = None, verify: bool = True) -> None:

Unfortunately, I think fixing these type hints will require significant effort to go through and untangle everything.

Activity

mlazar-endear

mlazar-endear commented on Mar 1, 2024

@mlazar-endear
Author

For anyone else who runs into this, you can configure mypy to ignore the all of the typehints provided by this library.

Here's what I added in my pyproject.toml file:

[[tool.mypy.overrides]]
# https://github.com/jazzband/djangorestframework-simplejwt/issues/787
module = "rest_framework_simplejwt.*"
follow_imports = "skip"
Andrew-Chen-Wang

Andrew-Chen-Wang commented on Mar 19, 2024

@Andrew-Chen-Wang
Member

@mlazar-endear do you have django-stubs enabled? I believe that's a requirement for type checking simplejwt

mlazar-endear

mlazar-endear commented on Mar 19, 2024

@mlazar-endear
Author

@Andrew-Chen-Wang Yes I do, but I don't see any reason why that would matter.

django-stubs==4.2.7
django-stubs-ext==4.2.7
djangorestframework-stubs==3.14.5

django==4.2.10
djangorestframework-simplejwt==5.3.1
Andrew-Chen-Wang

Andrew-Chen-Wang commented on Mar 19, 2024

@Andrew-Chen-Wang
Member

just for diagnosing. yes i see the issues too thanks!

khvn26

khvn26 commented on Mar 13, 2025

@khvn26

Solved in #889

linked a pull request that will close this issue on Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @khvn26@Andrew-Chen-Wang@mlazar-endear

      Issue actions

        Incorrect type hints for several classes/methods · Issue #787 · jazzband/djangorestframework-simplejwt