-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Using class-typed variable as first argument to map #3092
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
Comments
I think there may already be an existing issue requesting that Maybe this particular example is related to overload resolution not checking for However you're also right that for this particular definition of |
@ilevkivskyi explained to me they are not quite the same: But is it worth fixing the behavior with |
Yeah, but... We allow calling the constructor of a class in a classmethod, which has the same weakness, and we allow that intentionally (because we have valid real-world use cases). E.g. this is valid: class A:
def __init__(self, a: int) -> None:
self.a = a
def f(a: Type[A]) -> A:
return a(0) (The eventual solution for this would probably be a decorator constraining |
Both original examples now pass correctly. |
How do I annotate a function that takes a class argument and uses it as the first argument in
map
?A structural type would fit perfectly, but those aren't part of
mypy
yet.Using
Type[X]
doesn't work:Not sure if it's intentional or not, but it's somewhat understandable since
Type[X]
may be a subclass ofX
rather thanX
itself, and so there's no guarantee its__init__
accepts anint
argument.I tried to fall back to just
type
, but it still failed because even thoughcls
can be called with an integer argument and returnsAny
,type
isn't accepted as a subtype ofCallable[[T], S]
:Edit: after I fixed a typo, using
Callable
ended up working:Is this the right way to deal with it?
The text was updated successfully, but these errors were encountered: