docs: clarify that invariant-by-default applies to legacy TypeVar syntax#21108
Merged
JelleZijlstra merged 2 commits intopython:masterfrom Mar 30, 2026
Merged
Conversation
Under PEP 695 class syntax variance is inferred, not invariant by default. Closes python#20366. Made-with: Cursor
docs/source/common_issues.rst
Outdated
| ``TypeVar`` syntax, mypy considers all user-defined generic classes invariant | ||
| by default (see :ref:`variance-of-generics` for motivation). When using the | ||
| :pep:`695` syntax (``class MyClass[T]: ...``), variance is inferred from | ||
| usage rather than defaulting to invariant. This could lead to some unexpected |
Member
There was a problem hiding this comment.
The last sentence reads oddly now, I think "this" refers to the fact that mutable sequences are invariant, which we mentioned several sentences ago. I would start a new paragraph here with something like "The fact that mutable sequences are usually invariant can lead to unexpected errors..."
Made-with: Cursor
Contributor
Author
|
Just fixed, thanks so much for the feedback! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #20366.
The "invariant by default" rule applies to the legacy
TypeVarsyntax. Under PEP 695 (class MyClass[T]: ...), mypy infers variance from usage instead. Updated the wording incommon_issues.rstto make this distinction clear.