Closed gschaffner closed 2 months ago
I think your analysis might be incorrect here. The reason you're not seeing an error generated for the statement lower_0 = upper_0
is that you've previously assigned a value of lower_0
to upper_0
, and the type of upper_0
has been narrowed on assignment.
You can see this by inserting a few reveal_type
directives.
class A[T]:
pass
def typetest_A() -> None:
upper_0 = A[object]()
lower_0 = A[int]()
reveal_type(upper_0) # Reveals A[object]
upper_0 = lower_0 # OK
reveal_type(upper_0) # Reveals A[int]
lower_0 = upper_0 # OK
If you move the statement lower_0 = upper_0
immediately before the first reveal_type
, you'll see that it does produce an error as expected.
So, it looks like mypy is correctly inferring covariance consistent with the spec.
Oops :facepalm:, thank you! Sorry for the time waste.
Bug Report
When a PEP 695 generic classdef does not contain any methods/etc. that constrain the variance of its generic position, Mypy infers the type variable to be bivariant.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&enable-incomplete-feature=NewGenericSyntax&flags=strict&gist=a82dad9f13f2d8d94c448ea6fa8d327a
Expected Behavior
https://typing.readthedocs.io/en/latest/spec/generics.html#variance-inference states that type checkers should infer covariance as the default in this case:
(Personally I would prefer that type checkers did not make this assumption about the programmer's intent and instead https://peps.python.org/pep-0695/#explicit-variance was required[^1] either in this case or in all cases to prevent subtle miscommunications between the programmer and the type system, but AFAIU the spec. does seem clear that checkers should short-circuit and
return covariant
in this case.)[^1]: Or if not required, at least possible to explicitly specify in the new PEP 695 syntax, so that programmers could opt in to checks that they are not mistakenly exposing types with an unintended variance.
Actual Behavior
A[T]
is inferred as bivariant in the position ofT
. Technically this is correct, but the spec. says to do something else in this case.Your Environment
--strict --enable-incomplete-feature=NewGenericSyntax