Repository navigation
Replies: 1 comment
|
The new syntax intentionally does not allow declaring variance, with the idea that it's better to infer the safe variance from code. Your own example is actually unsafe, and can be exploited to create unsound code: JelleZijlstra/unsoundness#55. You can get the new syntax to infer covariance by making the |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have the following example, where I am intending that
Combined[T, U]and the underlyingFoo[T]andBar[U]are covariant in both T and U. This passes mypy's type checking and works as intended:With Python 3.12+, I believe I can rewrite the definition of
FoofromFoo(Generic[T])toFoo[T], and the same for Bar. However, when I do so, Mypy no longer considers the types to be covariant, and complains thatfunc1's return type is incompatible withfunc2's:Mypy's complaint being:
Do I need to take extra steps to describe the covariance? Checking the Mypy docs, I can find examples like the below stating that the new syntax implicitly assumed covariance, which suggest it should work as I expect:
(from https://mypy.readthedocs.io/en/stable/generics.html#variance-of-generic-types)
Removing the
TypeVar()definitions which become unnecessary with the new syntax does not change the type checking behaviour.All reactions