python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.55k stars 2.84k forks source link

Disallow bare `ParamSpec` in type aliases #18174

Closed brianschubert closed 5 days ago

brianschubert commented 5 days ago

Emit errors for both of the following aliases:

P = ParamSpec("P")
Bad1: TypeAlias = P
Bad2: TypeAlias = Concatenate[int, P]

This is done by swapping a top-level call to type.accept(analyzer) with analyzer.anal_type(type), the latter of which simply calls the former and then performs some checks.

brianschubert commented 5 days ago

Conformance results diff for this PR (generics_paramspec_basic now passes):

diff --git a/conformance/results/mypy/generics_paramspec_basic.toml b/conformance/results/mypy/generics_paramspec_basic.toml
index 53fd4f5..a22eb7b 100644
--- a/conformance/results/mypy/generics_paramspec_basic.toml
+++ b/conformance/results/mypy/generics_paramspec_basic.toml
@@ -4,6 +4,8 @@ Does not reject ParamSpec when used "bare" in type alias definition.
 """
 output = """
 generics_paramspec_basic.py:10: error: String argument 1 "NotIt" to ParamSpec(...) does not match variable name "WrongName"  [misc]
+generics_paramspec_basic.py:15: error: Invalid location for ParamSpec "P"  [valid-type]
+generics_paramspec_basic.py:15: note: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
 generics_paramspec_basic.py:23: error: Invalid location for ParamSpec "P"  [valid-type]
 generics_paramspec_basic.py:23: note: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
 generics_paramspec_basic.py:27: error: Invalid location for Concatenate  [valid-type]
@@ -15,7 +17,6 @@ generics_paramspec_basic.py:35: note: You can use ParamSpec as the first argumen
 generics_paramspec_basic.py:39: error: Invalid location for ParamSpec "P"  [valid-type]
 generics_paramspec_basic.py:39: note: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
 """
-conformance_automated = "Fail"
+conformance_automated = "Pass"
 errors_diff = """
-Line 15: Expected 1 errors
 """
diff --git a/conformance/results/mypy/version.toml b/conformance/results/mypy/version.toml
index 699a899..61bb190 100644
--- a/conformance/results/mypy/version.toml
+++ b/conformance/results/mypy/version.toml
@@ -1,2 +1,2 @@
-version = "mypy 1.14.0+dev.21587f01045246a9ecb54a054a5ba03e20cbbd19"
-test_duration = 4.5
+version = "mypy 1.14.0+dev.bf422add81d770eb4896b84afe800da48dbb21f1"
+test_duration = 5.0
github-actions[bot] commented 5 days ago

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅