-
The situation is like this: T = TypeVar("T", covariant=True)
# Type variable "T" used in generic protocol "P1" should be invariant
class P1(Protocol[T]):
value: T
# OK
class P2(Protocol[T]):
_value: T Is it intentional that for Related discussion: #3090 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I added an exemption for variables with protected or private names based on this discussion. This also affects auto-invariance calculations for TypeVars that use the new (PEP 695) syntax. In hindsight, I wonder if this was the right decision — at least for protected variables. I think it's justified for private variables because they cannot be modified outside of the class and cannot be overridden by subclasses (because of name mangling), but I'm not as sure about variables with protected names. The Python typing spec is silent on this topic. Because this is a gray area, I'm reluctant to change pyright's behavior unless/until there is consensus in the community about the "correct" behavior. If you feel strongly about it, you're welcome to start a discussion in the Python typing forum. |
Beta Was this translation helpful? Give feedback.
I added an exemption for variables with protected or private names based on this discussion. This also affects auto-invariance calculations for TypeVars that use the new (PEP 695) syntax.
In hindsight, I wonder if this was the right decision — at least for protected variables. I think it's justified for private variables because they cannot be modified outside of the class and cannot be overridden by subclasses (because of name mangling), but I'm not as sure about variables with protected names.
The Python typing spec is silent on this topic. Because this is a gray area, I'm reluctant to change pyright's behavior unless/until there is consensus in the community about the "correct" behavio…