-
-
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
mypy --strict requires circular type parameters when type variables are bound to the generic types that use them #11910
Comments
For what it's worth, Any is a bit special; it nearly always works as a type (and where it doesn't work... idk it probably should). |
Yeah, as A5rocks says, Any can behave like a subtype of Value (it can behave both a top type and a bottom type, unlike
This is |
Thank you, @hauntsaninja and @A5rocks: I have updated my original issue to restrict it to the circular reference problem. |
I'm still a little confused by the update, why doesn't the following work for you (note the explicit Any)?
|
That works as well, yes. It still exploits the special behaviour of |
Bug Report
Using
mypy --strict
requires type parameters to be specified even in circumstances where an untyped generic should be accepted, because the covariant type variable is bound to the generic type using it:This creates a circular reference problem, where the type itself must be passed as value to its own type parameter:
I would have expected this first example to not raise any errors, with
Value
treated asValue[any subtype of Value]
by default. This is because the stated bound forValueT
isValue
itself, making the top element of theValue
type hierarchy type-theoretically well-defined (it should consist of all instances ofValue
in this case).On the other hand,
mypy --strict
allowstyping.Any
as a type parameter value (regardless of the stated bound). This can be exploited to solve the problem:At this time, it is impossible to specify the upper bounds for the
Value
without exploiting this behaviour.Your Environment
Related issues
This is, arguably, a well-behaved special case of #4236.
Bonus: A simplified concrete scenario
The following is a (semplified but concrete) scenario involving expressions and value classes (intended to be immutable), which raises two "annoying" errors when
mypy --strict
is used (and one "desired" error using eithermypy
ormypy --strict
):However, the problem can be solved by defining a
GenericValue
type alias, setting the type parameter ofValue
totyping.Any
:The text was updated successfully, but these errors were encountered: