-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Callables and __init__
methods
#15024
Comments
Thanks for the report! When comparing a type object to a callback protocol, we should be comparing the type's Note that
If you change to The change between 0.991 and 1.0 bisects back to #14121, which is not a PR I really understand. There's definitely a bug here, but it might only be a bug in the error messaging |
Okay yeah, I think just a buggy error message. #15042 should fix |
Thanks for the fix! So is there a way to define the class MiddlewareType(Protocol):
def __call__(self, app: int, *args: Any, **kwargs: Any) -> ASGIApp:
... Also if you agree, I'd happy to open a PR to explicitly state in docs that checking against |
Thanks, PR welcome! :-) If there's a way to spell it (without adding additional nesting), I'm blanking. Maybe the best thing to do is just not put (It's sort of a similar use case to #5876 but only for a partial signature) |
Then it would fail to type check as |
Ah of course, that only works if there are defaults. I should probably go to sleep. Great link, yeah, #14656 looks like a good way to spell it. I think it actually type checks the way you want with mypy today, other than the "unexpected ..." error at the type alias definition |
The only downside is that you can't enforce the arg name to be |
Bug Report
I am currently working on encode/starlette#1987 where I'm defining a type hint for an argument that can take either a type of a class having a
__call__
method, or a callable returning another callable. That is, both definitions work:I was told that the following protocol would support both:
With the class definition, the
__init__
method is checked against the__call__
method ofMiddlewareType
, and with the callable definition, well it is checked against the__call__
method naturally. I was surprised to see that mypy supported the first case, as it was unclear to me what was the correct definition of acallable
(in fact, definitions differ in docs):Now if this behaviour is supported by
mypy
, maybe this could be clarified in this part of the docs?I've made this issue a bug report as I'm still struggling to have the
MiddlewareType
type working, as**options: typing.Any
doesn't seem to catch the args/kwargs followingapp: ASGIApp
.See this mypy-play example:
https://mypy-play.net/?mypy=0.991&python=3.11&gist=49f042e0c90bdaf202e519bd1467ce3a (works on
0.991
)https://mypy-play.net/?mypy=1.1.1&python=3.11&gist=6c7629c60546af1490ba092821564d63 (doesn't work since
1.0.0
)The last example is also returning a misleading error message:
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: