Are generics in self types always bound to the function? #1340
davidhalter
started this conversation in
General
Replies: 1 comment
-
I agree that this mypy behaviour seems is fragile, if not outright buggy. It would certainly be clearer to use a different type variable if you wanted a different binding. Pyright interprets this as doing something recursive, which looks more correct than what mypy does, but I'm not convinced pyright's behaviour is fully correct either. Here is a program that crashes with a TypeError that neither type checker has any complaints about:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In the mypy test suite there's this example that shows this issue:
It is a bit strange to me that in the case of
use_tuple=True
, we get a tuple, because theT
of the classC
islib.C[builtins.tuple[builtins.int, ...]]
, while theT
in the function__init__
is actuallyint
. This feels very confusing as a user. Is this intentional or a Mypy bug? Because normally the class type variables are bound to the class and not the function. Is this also how pyright and pyre work? Clearly this example should work if it was a different type variable:def __init__(self: C[Tuple[U, ...]], item: U, use_tuple: Literal[True]) -> None: ...
.The test is from mypy's
testSelfTypeRestrictedMethodOverloadFallback
intest-data/unit/check-selftype.test
.Beta Was this translation helpful? Give feedback.
All reactions