-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 is unable to assign Callable taking Concatenate to Union of that Callable and another using the same ParamSpec #15177
Comments
The problem seems to be with using I ran into the same issue, but with Dict.
|
@nikochiko that's an unrelated issue. Mypy is correct here; see https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance. Let's keep this issue focused on the ParamSpec problem. |
Ah, TIL. Thanks for the pointer. |
I may have the same issue but with from typing import Concatenate, ParamSpec, Callable, Optional
P = ParamSpec("P")
def func1(func: Callable[Concatenate[str, P], None], *args):
pass
def func2(func: Optional[Callable[Concatenate[str, P], None]], *args):
pass
def func3(a: str, b: str):
pass
func1(func3, "hello")
func2(func3, "hello")
|
I have done some research, those are my findings. The issue seems to be originated here Lines 256 to 288 in 7fe1fdd
While for Line 288 in 7fe1fdd
For Lines 270 to 285 in 7fe1fdd
infer_constraints_if_possible , here Lines 301 to 304 in 7fe1fdd
func1 Callable[[str, str], Any] and Callable[[str, *Any, **Any], None] ) are not one the subtypes of the other (I don't know if this is correct but it seems reasonable). In is_subtype the function is checked with Line 332 in 7fe1fdd
To sum up, I have tried with this change and it works good, but it seems like an hack to me. diff --git a/mypy/constraints.py b/mypy/constraints.py
index 33230871b..2de4ea1e9 100644
--- a/mypy/constraints.py
+++ b/mypy/constraints.py
@@ -298,8 +298,10 @@ def infer_constraints_if_possible(
"""
if direction == SUBTYPE_OF and not mypy.subtypes.is_subtype(erase_typevars(template), actual):
return None
- if direction == SUPERTYPE_OF and not mypy.subtypes.is_subtype(
- actual, erase_typevars(template)
+ if (
+ direction == SUPERTYPE_OF
+ and not isinstance(get_proper_type(template), CallableType)
+ and not mypy.subtypes.is_subtype(actual, erase_typevars(template))
):
return None
if ( I hope that this would be useful for someone |
The original example works on master, likely fixed by #15837 |
Bug Report
mypy is unable to assign
Callable[Concatenate[B, C], D]
toUnion[Callable[Concatenate[B, C], D], Callable[C, D]
.To Reproduce
Expected Behavior
I expect no error.
Actual Behavior
mypy produces this error:
Your Environment
mypy playground
--strict
The text was updated successfully, but these errors were encountered: