Skip to content

Commit a4bcd44

Browse files
added variance inference test with method scoped type variable and bound self type
1 parent 6a9f353 commit a4bcd44

8 files changed

Lines changed: 59 additions & 9 deletions

conformance/results/mypy/generics_variance_inference.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
conformant = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
polymorphic functions with bound self type influence variance inference,
4+
which is not part of the variance inference algorithm as described in the spec.
5+
See gh-2281 for details.
6+
"""
27
output = """
38
generics_variance_inference.py:24: error: Incompatible types in assignment (expression has type "ClassA[float, int, int]", variable has type "ClassA[int, int, int]") [assignment]
49
generics_variance_inference.py:25: error: Incompatible types in assignment (expression has type "ClassA[float, int, int]", variable has type "ClassA[float, float, int]") [assignment]
@@ -23,7 +28,10 @@ generics_variance_inference.py:169: error: Incompatible types in assignment (exp
2328
generics_variance_inference.py:170: error: Incompatible types in assignment (expression has type "ShouldBeInvariant6[int]", variable has type "ShouldBeInvariant6[float]") [assignment]
2429
generics_variance_inference.py:181: error: Incompatible types in assignment (expression has type "ShouldBeCovariant6[float]", variable has type "ShouldBeCovariant6[int]") [assignment]
2530
generics_variance_inference.py:194: error: Incompatible types in assignment (expression has type "ShouldBeContravariant2[int]", variable has type "ShouldBeContravariant2[float]") [assignment]
31+
generics_variance_inference.py:204: error: Incompatible types in assignment (expression has type "ShouldBeCovariant7[int]", variable has type "ShouldBeCovariant7[object]") [assignment]
32+
generics_variance_inference.py:205: error: Incompatible types in assignment (expression has type "ShouldBeCovariant7[object]", variable has type "ShouldBeCovariant7[int]") [assignment]
2633
"""
27-
conformance_automated = "Pass"
34+
conformance_automated = "Fail"
2835
errors_diff = """
36+
Line 204: Unexpected errors ['generics_variance_inference.py:204: error: Incompatible types in assignment (expression has type "ShouldBeCovariant7[int]", variable has type "ShouldBeCovariant7[object]") [assignment]']
2937
"""

conformance/results/pycroscope/generics_variance_inference.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ output = """
2525
./generics_variance_inference.py:170:0: Incompatible assignment: expected generics_variance_inference.ShouldBeInvariant6[float | int], got generics_variance_inference.ShouldBeInvariant6[int] [incompatible_assignment]
2626
./generics_variance_inference.py:181:0: Incompatible assignment: expected generics_variance_inference.ShouldBeCovariant6[int], got generics_variance_inference.ShouldBeCovariant6[float | int] [incompatible_assignment]
2727
./generics_variance_inference.py:194:0: Incompatible assignment: expected generics_variance_inference.ShouldBeContravariant2[float | int], got generics_variance_inference.ShouldBeContravariant2[int] [incompatible_assignment]
28+
./generics_variance_inference.py:205:0: Incompatible assignment: expected generics_variance_inference.ShouldBeCovariant7[int], got generics_variance_inference.ShouldBeCovariant7[object] [incompatible_assignment]
2829
"""

conformance/results/pyrefly/generics_variance_inference.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ ERROR generics_variance_inference.py:169:31-58: `ShouldBeInvariant6[float]` is n
2626
ERROR generics_variance_inference.py:170:33-58: `ShouldBeInvariant6[int]` is not assignable to `ShouldBeInvariant6[float]` [bad-assignment]
2727
ERROR generics_variance_inference.py:181:31-58: `ShouldBeCovariant6[float]` is not assignable to `ShouldBeCovariant6[int]` [bad-assignment]
2828
ERROR generics_variance_inference.py:194:37-66: `ShouldBeContravariant2[int]` is not assignable to `ShouldBeContravariant2[float]` [bad-assignment]
29+
ERROR generics_variance_inference.py:205:31-59: `ShouldBeCovariant7[object]` is not assignable to `ShouldBeCovariant7[int]` [bad-assignment]
2930
"""

conformance/results/pyright/generics_variance_inference.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ generics_variance_inference.py:194:37 - error: Type "ShouldBeContravariant2[int]
7979
  "ShouldBeContravariant2[int]" is not assignable to "ShouldBeContravariant2[float]"
8080
    Type parameter "T@ShouldBeContravariant2" is contravariant, but "int" is not a supertype of "float"
8181
      "float" is not assignable to "int" (reportAssignmentType)
82+
generics_variance_inference.py:205:31 - error: Type "ShouldBeCovariant7[object]" is not assignable to declared type "ShouldBeCovariant7[int]"
83+
  "ShouldBeCovariant7[object]" is not assignable to "ShouldBeCovariant7[int]"
84+
    Type parameter "T@ShouldBeCovariant7" is covariant, but "object" is not a subtype of "int"
85+
      "object" is not assignable to "int" (reportAssignmentType)
8286
"""
8387
conformance_automated = "Pass"
8488
errors_diff = """

conformance/results/results.html

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conformance/results/ty/generics_variance_inference.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ generics_variance_inference.py:169:31: error[invalid-assignment] Object of type
2525
generics_variance_inference.py:170:33: error[invalid-assignment] Object of type `ShouldBeInvariant6[int]` is not assignable to `ShouldBeInvariant6[int | float]`
2626
generics_variance_inference.py:181:31: error[invalid-assignment] Object of type `ShouldBeCovariant6[int | float]` is not assignable to `ShouldBeCovariant6[int]`
2727
generics_variance_inference.py:194:37: error[invalid-assignment] Object of type `ShouldBeContravariant2[int]` is not assignable to `ShouldBeContravariant2[int | float]`
28+
generics_variance_inference.py:205:31: error[invalid-assignment] Object of type `ShouldBeCovariant7[object]` is not assignable to `ShouldBeCovariant7[int]`
2829
"""

conformance/results/zuban/generics_variance_inference.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
conformance_automated = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
polymorphic functions with bound self type influence variance inference,
4+
which is not part of the variance inference algorithm as described in the spec.
5+
See gh-2281 for details.
6+
"""
7+
conformance_automated = "Fail"
28
errors_diff = """
9+
Line 200: Unexpected errors ['generics_variance_inference.py:200: error: Name "S" is not defined [name-defined]', 'generics_variance_inference.py:200: error: Name "S" is not defined [name-defined]']
310
"""
411
output = """
512
generics_variance_inference.py:24: error: Incompatible types in assignment (expression has type "ClassA[float, int, int]", variable has type "ClassA[int, int, int]") [assignment]
@@ -25,4 +32,7 @@ generics_variance_inference.py:169: error: Incompatible types in assignment (exp
2532
generics_variance_inference.py:170: error: Incompatible types in assignment (expression has type "ShouldBeInvariant6[int]", variable has type "ShouldBeInvariant6[float]") [assignment]
2633
generics_variance_inference.py:181: error: Incompatible types in assignment (expression has type "ShouldBeCovariant6[float]", variable has type "ShouldBeCovariant6[int]") [assignment]
2734
generics_variance_inference.py:194: error: Incompatible types in assignment (expression has type "ShouldBeContravariant2[int]", variable has type "ShouldBeContravariant2[float]") [assignment]
35+
generics_variance_inference.py:200: error: Name "S" is not defined [name-defined]
36+
generics_variance_inference.py:200: error: Name "S" is not defined [name-defined]
37+
generics_variance_inference.py:205: error: Incompatible types in assignment (expression has type "ShouldBeCovariant7[object]", variable has type "ShouldBeCovariant7[int]") [assignment]
2838
"""

conformance/tests/generics_variance_inference.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,14 @@ class ShouldBeContravariant2[T](Parent_Contravariant[T]):
192192

193193
c1: ShouldBeContravariant2[int] = ShouldBeContravariant2[float]() # OK
194194
c2: ShouldBeContravariant2[float] = ShouldBeContravariant2[int]() # E
195+
196+
class ShouldBeCovariant7[T]:
197+
def get(self) -> T:
198+
raise NotImplementedError
199+
200+
def add[S](self: "ShouldBeCovariant7[S]", other: list[S]) -> "ShouldBeCovariant7[S]":
201+
raise NotImplementedError
202+
203+
204+
d1: ShouldBeCovariant7[object] = ShouldBeCovariant7[int]() # OK
205+
d2: ShouldBeCovariant7[int] = ShouldBeCovariant7[object]() # E

0 commit comments

Comments
 (0)