summaryrefslogtreecommitdiff
path: root/tests/functional/t
diff options
context:
space:
mode:
authorJoe Young <80432516+jpy-git@users.noreply.github.com>2022-04-04 16:18:25 +0100
committerGitHub <noreply@github.com>2022-04-04 17:18:25 +0200
commit7a9234f681b3c046b97e4371c84fadcc3ac7c721 (patch)
treea650d4b3c01ca5598b570826a532fd36d560935a /tests/functional/t
parent1534fede298e7dafe7d0c7177af86cfe6be0499b (diff)
downloadpylint-git-7a9234f681b3c046b97e4371c84fadcc3ac7c721.tar.gz
New check: TypeVar name mismatch (#6168)
* TypeVar name mismatch check * Fix double hashes in changelogs Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests/functional/t')
-rw-r--r--tests/functional/t/typevar_name_mismatch.py47
-rw-r--r--tests/functional/t/typevar_name_mismatch.txt19
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/functional/t/typevar_name_mismatch.py b/tests/functional/t/typevar_name_mismatch.py
new file mode 100644
index 000000000..54e4798d6
--- /dev/null
+++ b/tests/functional/t/typevar_name_mismatch.py
@@ -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]
diff --git a/tests/functional/t/typevar_name_mismatch.txt b/tests/functional/t/typevar_name_mismatch.txt
new file mode 100644
index 000000000..8589afbfa
--- /dev/null
+++ b/tests/functional/t/typevar_name_mismatch.txt
@@ -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