We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
The following produces no errors but I expected it to be treated the same as if B.a was a get/set property (which causes a variance error)
B.a
class A: pass class A2(A): something: str = "hello" T_co = TypeVar('T_co', covariant=True) class B(Generic[T_co]): def __init__(self, a: T_co) -> None: self.a: T_co = a # EXPECTED: typing error here like "public attributes must have invariant type" def set_a(b: B[A]) -> None: b.a = A() b2: B[A2] = B(A2()) set_a(b2) print(b2.a.something) # ACTUAL: runtime error here
Python 3.10, mypy 0.961 with --strict
--strict
The text was updated successfully, but these errors were encountered:
The problem here is that you have defined T_co as covariant, but given how you are using it in B, it should be declared as invariant.
T_co
B
Sorry, something went wrong.
Yes, but shouldn't mypy tell me that? It does if I explicitly create a setter for a.
a
Or is variance something that the user needs to check is correct and it's use should be flagged like Any as potentially "unsafe"?
Any
Mypy validates the variance for protocol classes but not for other standard (nominal) classes.
No branches or pull requests
The following produces no errors but I expected it to be treated the same as if
B.a
was a get/set property (which causes a variance error)Python 3.10, mypy 0.961 with
--strict
The text was updated successfully, but these errors were encountered: