-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Typing for click.decorators.pass_context
is not covariant and does not support click.Context
subclasses
#2623
Comments
Happy to review a PR, but I do not understand this aspect of typing and so will not be working on it. That last part may be something you should report to mypy. |
To be clear I don't know very much about typing here either. 🤷
I am inclined to think it is not a bug and it is intentional behavior, but also I don't fully understand it. I think the way that I am using TypeVars in the above example is a bit of a hack. I will either research this more or enlist some help. |
Hmm. After thinking about it a bit, I think this problem is essentially unsolvable as it stands. A few observations:
For this reason, I'm concluding it's not feasible to make it work and it's also not worth doing anything to attempt to make it work. There are only two ways to make everything friendly:
On our end in rich-click, what we will be doing is shimming P = ParamSpec("P")
R = TypeVar("R")
def pass_context(f: Callable[Concatenate[RichContext, P], R]) -> Callable[P, R]:
# flake8: noqa: D400,D401
"""Marks a callback as wanting to receive the current context
object as first argument.
"""
return click_pass_context(f) # type: ignore[arg-type] And for our users, this is an appropriate solution. For any other framework developers looking to use a |
Thank you for the detailed updates here and in those linked issues. Dealing with the fact that type checkers can report that our own library's code passes, but still allows users of our library to have issues with our types is a constant source of frustration. If you find anything else, let us know and we can hopefully figure it out. |
Context (no pun intended): I am trying to get
rich-click
up to par with typing standards, and I ran into a minor nuisance with typing functions decorated bypass_context
.An example of
rich_click
code that does not work on the user's end with mypy would be this:Here, mypy will yell at the user:
However, I do believe this to be valid typing. The
RichCommand
has itscontext_class
asRichContext
, so it is valid to annotatectx
withRichContext
. The issue is related to the fact that the function typing by default is contravariant (e.g. it allows forctx: object
as a valid annotation), but it really should be covariant.The solution seems to be, instead of this typing...
It should have this typing instead:
Where
C = t.TypeVar("C", bound=Context)
. This appears to make it so instead ofContext
being contravariant, it becomes covariant, which is the desired behavior to support subclasses ofclick.Context
.Implementing this in the actual
click
source code causes the following mypy error to pop up, however:From here it's unclear what to do. I'd rather not pollute the code with a
# type: ignore
, since this is also a slight abuse of TypeVars. But on the other hand, I do believe this to be a valid behavior to support.Environment:
The text was updated successfully, but these errors were encountered: