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

mypy deduced type on Request.user_agent_class is Type[_UserAgent] #2273

Closed
sirosen opened this issue Nov 2, 2021 · 1 comment · Fixed by #2305
Closed

mypy deduced type on Request.user_agent_class is Type[_UserAgent] #2273

sirosen opened this issue Nov 2, 2021 · 1 comment · Fixed by #2305
Assignees
Labels
Milestone

Comments

@sirosen
Copy link

sirosen commented Nov 2, 2021

I ran into this after updating to v2.0.2 of flask and werkzeug.

Since UserAgent is going away, I replaced it with ua_parser (example will follow).
However, setting user_agent_class = MyUserAgent triggers a typing error as the type of Request.user_agent_class refers to the internal (deprecated) _UserAgent.

Absolutely minimal reproduction:

# minimal.py
from flask import Request
from werkzeug.user_agent import UserAgent

class MyUserAgent(UserAgent): pass

class MyRequest(Request):
    user_agent_class = MyUserAgent

produces

$ mypy minimal.py
minimal.py:7: error: Incompatible types in assignment (expression has type "Type[MyUserAgent]", base class "Request" defined the type as "Type[_UserAgent]")
Found 1 error in 1 file (checked 1 source file)

I've also created a simple reproduction of the failing mypy run in a repo, which includes both the above minimal example, and my usage with ua_parser.

I know that _UserAgent is going away, so maybe this doesn't matter. But I could submit a patch to change this line to read

    user_agent_class: t.Type[UserAgent] = t.cast(t.Type[UserAgent], _DeprecatedUserAgent)

I think that would solve this, since the type would be known to mypy (and other type checkers) as Type[UserAgent], and inheriting subclasses would be accepted.

Would that fix be welcome?

Also, I haven't seen any python projects establish a testing process for type annotations. Do the pallets projects have an approach if I wanted to add a test case for this?

@davidism davidism added this to the 2.0.3 milestone Dec 23, 2021
@davidism
Copy link
Member

This was more straightforward than the suggested fix, only need Type[UserAgent]. It was because I had missed adding any annotation at all, so it was falling back to the assigned type.

@davidism davidism self-assigned this Dec 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants