-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
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
Updated spec and conformance test to reflect that a type variable tha… #1587
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
conformant = "Partial" | ||
notes = """ | ||
Does not reject a function-scoped TypeVar that is marked as covariant or contravariant. | ||
Does not reject use of class-scoped TypeVar used in a base class when variance is incompatible. | ||
""" | ||
output = """ | ||
generics_variance.py:14: error: TypeVar cannot be both covariant and contravariant [misc] | ||
generics_variance.py:78: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:82: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
generics_variance.py:94: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
generics_variance.py:106: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:126: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:132: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
generics_variance.py:77: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:81: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
generics_variance.py:93: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
generics_variance.py:105: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:125: error: Variance of TypeVar "T_co" incompatible with variance in parent type [type-var] | ||
generics_variance.py:131: error: Variance of TypeVar "T_contra" incompatible with variance in parent type [type-var] | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
conformant = "Partial" | ||
notes = """ | ||
Does not reject a TypeVar that is defined as both covariant and contravariant. | ||
Does not reject a function-scoped TypeVar that is marked as covariant or contravariant. | ||
Does not reject use of class-scoped TypeVar used in a base class when variance is incompatible. | ||
""" | ||
output = """ | ||
generics_variance.py:78:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T]` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:82:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T]` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:94:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T_co](covariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:106:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T_contra](contravariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:126:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T_contra](contravariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:132:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T_co](covariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:77:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T]` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:81:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T]` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:93:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T_co](covariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:105:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T_contra](contravariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:125:0 Invalid type variance [46]: The type variable `Variable[T_co](covariant)` is incompatible with parent class type variable `Variable[T_contra](contravariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
generics_variance.py:131:0 Invalid type variance [46]: The type variable `Variable[T_contra](contravariant)` is incompatible with parent class type variable `Variable[T_co](covariant)` because subclasses cannot use more permissive type variables than their superclasses. | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
conformant = "Partial" | ||
notes = """ | ||
Does not reject a function-scoped TypeVar that is marked as covariant or contravariant. | ||
""" | ||
conformant = "Pass" | ||
output = """ | ||
generics_variance.py:14:50 - error: TypeVar cannot be both covariant and contravariant | ||
generics_variance.py:78:18 - error: Type "T_co@Class1" cannot be assigned to type variable "T@Inv" | ||
generics_variance.py:77:18 - error: Type "T_co@Class1" cannot be assigned to type variable "T@Inv" | ||
Variance of type argument "T_co@Class1" is incompatible with base class "Inv" (reportGeneralTypeIssues) | ||
generics_variance.py:82:18 - error: Type "T_contra@Class2" cannot be assigned to type variable "T@Inv" | ||
generics_variance.py:81:18 - error: Type "T_contra@Class2" cannot be assigned to type variable "T@Inv" | ||
Variance of type argument "T_contra@Class2" is incompatible with base class "Inv" (reportGeneralTypeIssues) | ||
generics_variance.py:94:20 - error: Type "T_contra@Co_Child3" cannot be assigned to type variable "T_co@Co" | ||
generics_variance.py:93:20 - error: Type "T_contra@Co_Child3" cannot be assigned to type variable "T_co@Co" | ||
Variance of type argument "T_contra@Co_Child3" is incompatible with base class "Co" (reportGeneralTypeIssues) | ||
generics_variance.py:106:28 - error: Type "T_co@Contra_Child3" cannot be assigned to type variable "T_contra@Contra" | ||
generics_variance.py:105:28 - error: Type "T_co@Contra_Child3" cannot be assigned to type variable "T_contra@Contra" | ||
Variance of type argument "T_co@Contra_Child3" is incompatible with base class "Contra" (reportGeneralTypeIssues) | ||
generics_variance.py:114:28 - error: Type "Co[T_co@Contra_Child5]" cannot be assigned to type variable "T_contra@Contra" | ||
generics_variance.py:113:28 - error: Type "Co[T_co@Contra_Child5]" cannot be assigned to type variable "T_contra@Contra" | ||
Variance of type argument "Co[T_co@Contra_Child5]" is incompatible with base class "Contra" (reportGeneralTypeIssues) | ||
generics_variance.py:127:20 - error: Type "T_co@CoContra_Child2" cannot be assigned to type variable "T_contra@CoContra" | ||
generics_variance.py:126:20 - error: Type "T_co@CoContra_Child2" cannot be assigned to type variable "T_contra@CoContra" | ||
Variance of type argument "T_co@CoContra_Child2" is incompatible with base class "CoContra" (reportGeneralTypeIssues) | ||
generics_variance.py:133:14 - error: Type "T_contra@CoContra_Child3" cannot be assigned to type variable "T_co@CoContra" | ||
generics_variance.py:132:14 - error: Type "T_contra@CoContra_Child3" cannot be assigned to type variable "T_co@CoContra" | ||
Variance of type argument "T_contra@CoContra_Child3" is incompatible with base class "CoContra" (reportGeneralTypeIssues) | ||
generics_variance.py:143:24 - error: Type "Co[T_co@CoContra_Child5]" cannot be assigned to type variable "T_contra@CoContra" | ||
generics_variance.py:142:24 - error: Type "Co[T_co@CoContra_Child5]" cannot be assigned to type variable "T_contra@CoContra" | ||
Variance of type argument "Co[T_co@CoContra_Child5]" is incompatible with base class "CoContra" (reportGeneralTypeIssues) | ||
generics_variance.py:164:33 - error: Type "Co[Contra[T_contra@CoToContraToContra]]" cannot be assigned to type variable "T_contra@Contra" | ||
generics_variance.py:163:33 - error: Type "Co[Contra[T_contra@CoToContraToContra]]" cannot be assigned to type variable "T_contra@Contra" | ||
Variance of type argument "Co[Contra[T_contra@CoToContraToContra]]" is incompatible with base class "Contra" (reportGeneralTypeIssues) | ||
generics_variance.py:168:37 - error: Type "Contra[Contra[T_co@ContraToContraToContra]]" cannot be assigned to type variable "T_contra@Contra" | ||
generics_variance.py:167:37 - error: Type "Contra[Contra[T_co@ContraToContraToContra]]" cannot be assigned to type variable "T_contra@Contra" | ||
Variance of type argument "Contra[Contra[T_co@ContraToContraToContra]]" is incompatible with base class "Contra" (reportGeneralTypeIssues) | ||
generics_variance.py:192:43 - error: Could not specialize type "Contra_TA[T_contra@Contra_TA]" | ||
generics_variance.py:191:43 - error: Could not specialize type "Contra_TA[T_contra@Contra_TA]" | ||
Variance of type argument "Co_TA[Contra_TA[T_contra@CoToContraToContra_WithTA]]" is incompatible with "T_contra@Contra_TA" | ||
generics_variance.py:197:15 - error: Could not specialize type "Contra_TA[T_contra@Contra_TA]" | ||
generics_variance.py:196:15 - error: Could not specialize type "Contra_TA[T_contra@Contra_TA]" | ||
Variance of type argument "Contra_TA[Contra_TA[T_co@ContraToContraToContra_WithTA]]" is incompatible with "T_contra@Contra_TA" | ||
""" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be okay with this PR if you just remove "should therefore be ignored w by type checkers", so type checkers may choose to reject typevars declared as covariant or contravariant in contexts where variance has no meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If type checkers do not ignore variance when it has no meaning, what should they do instead? Keep in mind that every TypeVar (prior to PEP 695's "inferred variance") has an explicit variance; it's either invariant, covariant, or contravariant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since "invariant" is declared by not adding
{co,contra}variant=True
to theTypeVar()
call, I'd propose that type checkers accept invariant (i.e., default) typevars but issue an error for co/contra-variant typevars.I believe it is reasonable to assume that omitting any mention of variance in the
TypeVar()
call is less explicit than explicitly using one of the two variance-declaring options (there is noinvariant=True
option).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was curious, so I quickly implemented a check in pyright for a TypeVar that's covariant or contravariant if it is scoped to a generic function or type alias. Here's the mypy_primer result: microsoft/pyright#6993. This isn't as noisy as I predicted. Interestingly, 100% of the violations it caught were in type aliases rather than functions.
I think we should make it clear in the spec that variance has no meaning for TypeVars scoped to functions or type aliases — and that type checkers should therefore ignore the variance for purposes of type checking. As for additional checks, I think we have three options:
I guess I don't have a strong opinion. The current draft text aligns to 3. It sounds like you prefer 1? Or 2?
I do think that mypy's current behavior is hard to defend. It emits an unrelated (and kind of meaningless) error if a covariant TypeVar is used for an input parameter or if a contravariant TypeVar is used for a return type. Since variance isn't meaningful in this case, those errors are inappropriate and confusing, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd go for (2), or (2a) don't allude to what type checkers should do at all.
(I'm beginning to realize that "has no meaning" is a rather ambiguous phrase, which implies different things in different contexts -- at least to me. I don't think it's the same as "is redundant" here.)
I agree that mypy's error is not what I thought it was and should be fixed independently.