Skip to content
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

Reject invalid ParamSpec locations #18278

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Contributor

@sterliakov sterliakov commented Dec 11, 2024

Fixes #14832, fixes #13966, fixes #14622.

Still does not report error in #14777, I'll work separately on that.

Move all ParamSpec validity checking to typeanal.py. Stop treating P.args and P.kwargs as binding - only bare typevar makes it available in scope. Reject keyword arguments following P.args.

This also makes one more conformance test pass. Diff (against baseline on current master):

diff --git a/conformance/results/mypy/generics_paramspec_components.toml b/conformance/results/mypy/generics_paramspec_components.toml
index 66a386a..86ba0b0 100644
--- a/conformance/results/mypy/generics_paramspec_components.toml
+++ b/conformance/results/mypy/generics_paramspec_components.toml
@@ -8,10 +8,18 @@ Does not report error when keyword argument is specified between P.args and P.kw
 output = """
 generics_paramspec_components.py:17: error: Use "P.args" for variadic "*" parameter  [valid-type]
 generics_paramspec_components.py:17: error: Use "P.kwargs" for variadic "**" parameter  [valid-type]
+generics_paramspec_components.py:20: error: ParamSpec components are not allowed here  [valid-type]
 generics_paramspec_components.py:23: error: Use "P.kwargs" for variadic "**" parameter  [valid-type]
+generics_paramspec_components.py:26: error: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"  [valid-type]
+generics_paramspec_components.py:30: error: ParamSpec "P" is unbound  [valid-type]
+generics_paramspec_components.py:35: error: ParamSpec components are not allowed here  [valid-type]
+generics_paramspec_components.py:36: error: ParamSpec components are not allowed here  [valid-type]
+generics_paramspec_components.py:38: error: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"  [valid-type]
+generics_paramspec_components.py:41: error: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"  [valid-type]
 generics_paramspec_components.py:49: error: Argument 1 has incompatible type "*P.kwargs"; expected "P.args"  [arg-type]
 generics_paramspec_components.py:49: error: Argument 2 has incompatible type "**P.args"; expected "P.kwargs"  [arg-type]
 generics_paramspec_components.py:51: error: Argument 1 has incompatible type "int"; expected "P.args"  [arg-type]
+generics_paramspec_components.py:60: error: Arguments not allowed after ParamSpec.args  [valid-type]
 generics_paramspec_components.py:70: error: Argument 1 has incompatible type "*P.args"; expected "int"  [arg-type]
 generics_paramspec_components.py:70: error: Argument 2 has incompatible type "int"; expected "P.args"  [arg-type]
 generics_paramspec_components.py:72: error: Argument 1 has incompatible type "*P.args"; expected "int"  [arg-type]
@@ -21,14 +29,6 @@ generics_paramspec_components.py:83: error: Argument 3 to "foo" has incompatible
 generics_paramspec_components.py:98: error: Argument 2 to "twice" has incompatible type "str"; expected "int"  [arg-type]
 generics_paramspec_components.py:98: error: Argument 3 to "twice" has incompatible type "int"; expected "str"  [arg-type]
 """
-conformance_automated = "Fail"
+conformance_automated = "Pass"
 errors_diff = """
-Line 20: Expected 1 errors
-Line 26: Expected 1 errors
-Line 30: Expected 1 errors
-Line 35: Expected 1 errors
-Line 36: Expected 1 errors
-Line 38: Expected 1 errors
-Line 41: Expected 1 errors
-Line 60: Expected 1 errors
 """
diff --git a/conformance/results/mypy/version.toml b/conformance/results/mypy/version.toml
index 3030c9f..42ef13b 100644
--- a/conformance/results/mypy/version.toml
+++ b/conformance/results/mypy/version.toml
@@ -1,2 +1,2 @@
-version = "mypy 1.14.0+dev.14974072c0a70f8ca29c17c740475187b800e714"
-test_duration = 8.4
+version = "mypy 1.14.0+dev.e580045ce62e3c8ccd17fb575f60ec7ede65c01b"
+test_duration = 8.2
diff --git a/conformance/results/results.html b/conformance/results/results.html
index e6ef533..1278a53 100644
--- a/conformance/results/results.html
+++ b/conformance/results/results.html
@@ -158,8 +158,8 @@
         </header>
         <div class="table_container"><table><tbody>
 <tr><th class="col1">&nbsp;</th>
-<th class='tc-header'><div class='tc-name'>mypy 1.14.0+dev.14974072c0a70f8ca29c17c740475187b800e714</div>
-<div class='tc-time'>8.4sec</div>
+<th class='tc-header'><div class='tc-name'>mypy 1.14.0+dev.e580045ce62e3c8ccd17fb575f60ec7ede65c01b</div>
+<div class='tc-time'>8.2sec</div>
 </th>
 </tr>
 <tr><th class="column" colspan="2">

This comment has been minimized.

This comment has been minimized.

@sterliakov sterliakov marked this pull request as ready for review December 11, 2024 05:06
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm glad to see several bugs fixed. Looks good!

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 20, 2024

Actually, the home-assistant error might be false positive. Can you look at it?

@sterliakov
Copy link
Contributor Author

Amazing. I wasn't able to reproduce the problem on a local clone, and indeed it was apparently a glitch: another primer run did not produce an error there.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

altair (https://github.com/vega/altair)
+ altair/theme.py:250: error: ParamSpec "P" is unbound  [valid-type]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/_internal/compatibility/async_dispatch.py:77: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/flows.py:1259: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/flows.py:1268: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/flows.py:1276: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/tasks.py:970: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/tasks.py:978: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/tasks.py:1041: error: Arguments not allowed after ParamSpec.args  [valid-type]
+ src/prefect/tasks.py:1050: error: Arguments not allowed after ParamSpec.args  [valid-type]

pwndbg (https://github.com/pwndbg/pwndbg)
+ pwndbg/gdblib/events.py: note: In function "connect":
+ pwndbg/gdblib/events.py:277: error: ParamSpec "P" is unbound  [valid-type]

@cdce8p
Copy link
Collaborator

cdce8p commented Dec 20, 2024

Actually, the home-assistant error might be false positive. Can you look at it?

Amazing. I wasn't able to reproduce the problem on a local clone, and indeed it was apparently a glitch: another primer run did not produce an error there.

Not it wasn't a glitch. I just fixed it already after seeing your PR 😉
home-assistant/core#133044

_P wasn't bound by the outer function scope.

@sterliakov
Copy link
Contributor Author

Ough! I was moderately certain that I reviewed primer hits and found them to be correct, but did not expect a fix in between... Please comment next time you do that!:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants