-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New check: TypeVar name mismatch (#6168)
* TypeVar name mismatch check * Fix double hashes in changelogs Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
- Loading branch information
1 parent
1534fed
commit 7a9234f
Showing
7 changed files
with
107 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from typing import TypeVar | ||
|
||
X = TypeVar("T") # [typevar-name-mismatch] |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from typing import TypeVar | ||
|
||
T = TypeVar("T") |
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Test case for TypeVar name not matching assigned variable name.""" | ||
from typing import TypeVar | ||
|
||
# Control examples | ||
T = TypeVar("T") | ||
T_co = TypeVar("T_co", covariant=True) | ||
T_contra = TypeVar("T_contra", contravariant=True) | ||
|
||
# Mismatching names | ||
X = TypeVar("T") # [typevar-name-mismatch] | ||
X_co = TypeVar("T_co", covariant=True) # [typevar-name-mismatch] | ||
X_contra = TypeVar("T_contra", contravariant=True) # [typevar-name-mismatch] | ||
X_co, X_contra = ( # [typevar-name-mismatch,typevar-name-mismatch] | ||
TypeVar("T", covariant=True), | ||
TypeVar("T", contravariant=True), | ||
) | ||
|
||
# The user may also violate typevar-double-variance | ||
# or typevar-name-incorrect-variance | ||
# on top of not matching the variable and TypeVar names. | ||
# This rule does not suggest what the correct name is | ||
# (that's already handled by the aforementioned rules), | ||
# it just highlights that the names don't match. | ||
X = TypeVar("T", contravariant=True) # [typevar-name-mismatch,typevar-name-incorrect-variance] | ||
X = TypeVar( # [typevar-name-mismatch,typevar-name-incorrect-variance,typevar-double-variance] | ||
"T", | ||
covariant=True, | ||
contravariant=True | ||
) | ||
X_co = TypeVar("T_co") # [typevar-name-mismatch,typevar-name-incorrect-variance] | ||
X_contra = TypeVar( # [typevar-name-mismatch,typevar-name-incorrect-variance] | ||
"T_contra", | ||
covariant=True | ||
) | ||
|
||
# name can be specified as a keyword argument as well. | ||
T = TypeVar(name="T") | ||
T_co = TypeVar(name="T_co", covariant=True) | ||
T_contra = TypeVar(name="T_contra", contravariant=True) | ||
T_co = TypeVar(covariant=True, name="T_co") | ||
T_contra = TypeVar(contravariant=True, name="T_contra") | ||
|
||
X = TypeVar(name="T") # [typevar-name-mismatch] | ||
X_co = TypeVar(name="T_co", covariant=True) # [typevar-name-mismatch] | ||
X_contra = TypeVar(name="T_contra", contravariant=True) # [typevar-name-mismatch] | ||
X_co = TypeVar(covariant=True, name="T_co") # [typevar-name-mismatch] | ||
X_contra = TypeVar(contravariant=True, name="T_contra") # [typevar-name-mismatch] |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
typevar-name-mismatch:10:0:10:1::"TypeVar name ""T"" does not match assigned variable name ""X""":INFERENCE | ||
typevar-name-mismatch:11:0:11:4::"TypeVar name ""T_co"" does not match assigned variable name ""X_co""":INFERENCE | ||
typevar-name-mismatch:12:0:12:8::"TypeVar name ""T_contra"" does not match assigned variable name ""X_contra""":INFERENCE | ||
typevar-name-mismatch:13:0:13:4::"TypeVar name ""T"" does not match assigned variable name ""X_co""":INFERENCE | ||
typevar-name-mismatch:13:6:13:14::"TypeVar name ""T"" does not match assigned variable name ""X_contra""":INFERENCE | ||
typevar-name-incorrect-variance:24:0:24:1::"Type variable name does not reflect variance. ""X"" is contravariant, use ""X_contra"" instead":INFERENCE | ||
typevar-name-mismatch:24:0:24:1::"TypeVar name ""T"" does not match assigned variable name ""X""":INFERENCE | ||
typevar-double-variance:25:0:25:1::TypeVar cannot be both covariant and contravariant:INFERENCE | ||
typevar-name-incorrect-variance:25:0:25:1::Type variable name does not reflect variance:INFERENCE | ||
typevar-name-mismatch:25:0:25:1::"TypeVar name ""T"" does not match assigned variable name ""X""":INFERENCE | ||
typevar-name-incorrect-variance:30:0:30:4::"Type variable name does not reflect variance. ""X_co"" is invariant, use ""X"" instead":INFERENCE | ||
typevar-name-mismatch:30:0:30:4::"TypeVar name ""T_co"" does not match assigned variable name ""X_co""":INFERENCE | ||
typevar-name-incorrect-variance:31:0:31:8::"Type variable name does not reflect variance. ""X_contra"" is covariant, use ""X_co"" instead":INFERENCE | ||
typevar-name-mismatch:31:0:31:8::"TypeVar name ""T_contra"" does not match assigned variable name ""X_contra""":INFERENCE | ||
typevar-name-mismatch:43:0:43:1::"TypeVar name ""T"" does not match assigned variable name ""X""":INFERENCE | ||
typevar-name-mismatch:44:0:44:4::"TypeVar name ""T_co"" does not match assigned variable name ""X_co""":INFERENCE | ||
typevar-name-mismatch:45:0:45:8::"TypeVar name ""T_contra"" does not match assigned variable name ""X_contra""":INFERENCE | ||
typevar-name-mismatch:46:0:46:4::"TypeVar name ""T_co"" does not match assigned variable name ""X_co""":INFERENCE | ||
typevar-name-mismatch:47:0:47:8::"TypeVar name ""T_contra"" does not match assigned variable name ""X_contra""":INFERENCE |