-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fixed match-case against typing.Callable #19451
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
Conversation
| isinstance(type_info, Var) | ||
| and type_info.type is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two conditions are also checked in the elif before and below, but I did not bother to try to optimize them away.
| ): | ||
| # Consider as `Callable[..., Any]` | ||
| fallback = self.chk.named_type("builtins.function") | ||
| any_type = AnyType(TypeOfAny.unannotated) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is the right type of Any; I was guessing based on the description.
This comment has been minimized.
This comment has been minimized.
|
Hm, this probably can be improved quite a bit, since currently it always infers |
9c3926a to
5fbcbed
Compare
| fn_type = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback) | ||
| # if typ is itself callable, use its own type, otherwise Callable[..,, Any] | ||
| typ = current_type if is_subtype(current_type, fn_type) else fn_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a solution now to get more precise narrowing, e.g.
def int_fun(x: int) -> int: return x+1
match int_fun:
case Callable() as fn:
reveal_type(fn) # N: Revealed type is "def (x: builtins.int) -> builtins.int"I'm sure there's a more elegant way to do it, but this is the only thing I came up with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this doesn't work as desired when e.g. x: Callable[[int], int] | str, we get Callable[..., Any] and not Callable[[int], int].
I guess we need to patch the conditional_types_with_intersection function instead?
|
Closing temporarily to find a better solution that yields correct narrowing logic. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #14014
Added an extra elif branch that checks if we are matching against
typing.Callable, and usescallable_with_ellipsisto treat it as aCallable[..., Any].