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

Disallow TypeVar constraints parameterized by type variables #18186

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

Conversation

brianschubert
Copy link
Collaborator

Emit an error for this test case from the conformance tests:

class Test(Generic[T]):
    BadConstraint2 = TypeVar("BadConstraint2", str, list[T])  # E

@brianschubert
Copy link
Collaborator Author

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

diff --git a/conformance/results/mypy/generics_basic.toml b/conformance/results/mypy/generics_basic.toml
index 00c8244..b15fe50 100644
--- a/conformance/results/mypy/generics_basic.toml
+++ b/conformance/results/mypy/generics_basic.toml
@@ -6,6 +6,7 @@ output = """
 generics_basic.py:40: error: Value of type variable "AnyStr" of "concat" cannot be "Sequence[object]"  [type-var]
 generics_basic.py:41: error: Value of type variable "AnyStr" of "concat" cannot be "Sequence[object]"  [type-var]
 generics_basic.py:49: error: Type variable must have at least two constrained types  [misc]
+generics_basic.py:55: error: TypeVar constraint type cannot be parametrized by type variables  [misc]
 generics_basic.py:69: error: Value of type variable "AnyStr" of "concat" cannot be "Sequence[object]"  [type-var]
 generics_basic.py:121: error: Duplicate type variables in Generic[...] or Protocol[...]  [misc]
 generics_basic.py:157: error: Invalid index type "int" for "MyMap1[str, int]"; expected type "str"  [index]
@@ -15,7 +16,6 @@ generics_basic.py:191: error: Type variable "generics_basic.T" is unbound  [vali
 generics_basic.py:191: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
 generics_basic.py:191: note: (Hint: Use "T" in function signature to bind "T" inside a function)
 """
-conformance_automated = "Fail"
+conformance_automated = "Pass"
 errors_diff = """
-Line 55: Expected 1 errors
 """
diff --git a/conformance/results/mypy/version.toml b/conformance/results/mypy/version.toml
index 51d97b4..59e0dd2 100644
--- a/conformance/results/mypy/version.toml
+++ b/conformance/results/mypy/version.toml
@@ -1,2 +1,2 @@
-version = "mypy 1.14.0+dev.499adaed8adbded1a180e30d071438fef81779ec"
-test_duration = 2.4
+version = "mypy 1.14.0+dev.841096633fe6255acb7840e4a7b0fce975b8ca24"
+test_duration = 4.4

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@brianschubert brianschubert force-pushed the conformance-typevar-constraint-generic branch from c467949 to 8cc1aee Compare November 25, 2024 17:33

This comment has been minimized.

@brianschubert
Copy link
Collaborator Author

Two approaches, not sure which is better:

  1. Original approach (8410966), uses modified FindTypeVarVisitor, more complex, robust to placeholder types
  2. Second approach (a8171c6), uses has_no_typevars, simpler, but may have false negatives if a constraint type includes both a placeholder type and a type variable.

Copy link
Contributor

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

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

Successfully merging this pull request may close these issues.

1 participant