-
-
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
ambiguity on using union of type[generic type] and callable in mypy==0.981 #13756
Comments
Yeah, unfortunately IIRC this "rule" ( |
Thanks for getting back to me so quick and letting me know! Do you have any timeline on when you might look into making the semantics here more reasonable? Just for reference so I know roughly when to remove my type ignores 😄 |
It depends, but maybe as soon as this weekend. |
I've found a variant of this affecting overload declarations. Sample code: https://mypy-play.net/?mypy=latest&python=3.11&gist=8d1b1a7dc948f7ad4da6b26b646ab5b6 The core issue is that these overloads overlap: @overload
def decorator(decorated: Type[T]) -> Type[T]: ...
@overload
def decorator(decorated: Callable[Concatenate[Any, P], R]) -> WrapperClass[P, R]: ... |
I was able to resolve this by using a callback protocol, as #14121 explicitly excludes T = TypeVar('T')
P = ParamSpec('P')
R = TypeVar('R')
class MethodProtocol(Protocol[P, R]):
# Using ``def __call__`` seems to break Mypy (1.7.1), so we use this hack:
# https://github.com/python/typing/discussions/1312#discussioncomment-4416217
__call__: Callable[Concatenate[Any, P], R]
@overload
def decorator(decorated: Type[T]) -> Type[T]: ...
@overload
def decorator(decorated: MethodProtocol[Concatenate[Any, P], R]) -> WrapperClass[P, R]: ...
... |
Update: As of Mypy 1.9.0, the hacky use of T = TypeVar('T')
P = ParamSpec('P')
R_co = TypeVar('R_co', covariant=True)
class MethodProtocol(Protocol[P, R_co]):
def __call__(__self, self: Any, *args: P.args, **kwargs: P.kwargs) -> R_co: ...
@overload
def decorator(decorated: Type[T]) -> Type[T]: ...
@overload
def decorator(decorated: MethodProtocol[P, R_co]) -> WrapperClass[P, R_co]: ... |
Bug Report
I have a function which expects either a callable function or a type of generic type (i.e.
float
,str
,int
, ...) as an argument. However inmypy == 0.981
, the mypy type checker strictly assumes that a type of type is a callable first and foremost (to be fair, it is technically a callable since it can also be used as a constructor). This results in an error even if I've passed in a type of type as I've intended.To Reproduce
Here is my code that is erroring:
Expected Behavior
No errors. I don't see any errors when using
mypy <= 0.971
.Actual Behavior
When using
mypy == 0.981
, I get this mypy error:For some reason, mypy expects a
float
instead ofType[float]
as I've specified. I'd love to get some insight on why it changes this expectation.Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: