Skip to content

Commit

Permalink
New test cases in generics_basic.
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen152 committed Dec 3, 2024
1 parent 2b60460 commit a65f949
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 18 deletions.
12 changes: 8 additions & 4 deletions conformance/results/mypy/generics_basic.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ generics_basic.py:69: error: Value of type variable "AnyStr" of "concat" cannot
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]
generics_basic.py:158: error: Invalid index type "int" for "MyMap2[int, str]"; expected type "str" [index]
generics_basic.py:191: error: Dynamic metaclass not supported for "GenericMetaInstance" [misc]
generics_basic.py:191: error: Type variable "generics_basic.T" is unbound [valid-type]
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)
generics_basic.py:162: error: Free type variable expected in Generic[...] [misc]
generics_basic.py:163: error: Free type variable expected in Protocol[...] [misc]
generics_basic.py:171: error: If Generic[...] or Protocol[...] is present it should list all type variables [misc]
generics_basic.py:172: error: If Generic[...] or Protocol[...] is present it should list all type variables [misc]
generics_basic.py:208: error: Dynamic metaclass not supported for "GenericMetaInstance" [misc]
generics_basic.py:208: error: Type variable "generics_basic.T" is unbound [valid-type]
generics_basic.py:208: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
generics_basic.py:208: note: (Hint: Use "T" in function signature to bind "T" inside a function)
"""
conformance_automated = "Fail"
errors_diff = """
Expand Down
7 changes: 6 additions & 1 deletion conformance/results/pyre/generics_basic.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ False positives in examples using constrained type variables.
False negative for constraint parameterized by a type variable.
False negative in custom map example.
False positive using `iter`.
False negative for bad type arguments to Generic/Protocol.
False negative for generic metaclass.
"""
output = """
Expand All @@ -16,10 +17,14 @@ generics_basic.py:69:14 Incompatible parameter type [6]: In call `concat`, for 2
generics_basic.py:121:0 Duplicate type variables [59]: Duplicate type variable `T` in Generic[...].
generics_basic.py:157:7 Incompatible parameter type [6]: In call `typing.Mapping.__getitem__`, for 1st positional argument, expected `str` but got `int`.
generics_basic.py:158:7 Incompatible parameter type [6]: In call `typing.Mapping.__getitem__`, for 1st positional argument, expected `str` but got `int`.
generics_basic.py:171:11 Invalid type variable [34]: The current class isn't generic with respect to the type variable `Variable[T_co]`. To reference the type variable, you can modify the class to inherit from `typing.Generic[T_co]`.
generics_basic.py:172:11 Invalid type variable [34]: The current class isn't generic with respect to the type variable `Variable[T_co]`. To reference the type variable, you can modify the class to inherit from `typing.Generic[T_co]`.
"""
conformance_automated = "Fail"
errors_diff = """
Line 55: Expected 1 errors
Line 191: Expected 1 errors
Line 162: Expected 1 errors
Line 163: Expected 1 errors
Line 208: Expected 1 errors
Line 34: Unexpected errors ['generics_basic.py:34:4 Incompatible return type [7]: Expected `Variable[AnyStr <: [str, bytes]]` but got `str`.', 'generics_basic.py:34:15 Incompatible parameter type [6]: In call `str.__add__`, for 1st positional argument, expected `str` but got `Variable[AnyStr <: [str, bytes]]`.']
"""
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.23"
test_duration = 4.4
test_duration = 4.3
8 changes: 7 additions & 1 deletion conformance/results/pyright/generics_basic.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ generics_basic.py:157:5 - error: Argument of type "Literal[0]" cannot be assigne
  "Literal[0]" is not assignable to "str" (reportArgumentType)
generics_basic.py:158:5 - error: Argument of type "Literal[0]" cannot be assigned to parameter "key" of type "str" in function "__getitem__"
  "Literal[0]" is not assignable to "str" (reportArgumentType)
generics_basic.py:191:37 - error: Metaclass cannot be generic (reportGeneralTypeIssues)
generics_basic.py:162:20 - error: Type argument for "Generic" must be a type variable (reportInvalidTypeForm)
generics_basic.py:163:21 - error: Type argument for "Protocol" must be a type parameter (reportInvalidTypeForm)
generics_basic.py:171:7 - error: Generic[] or Protocol[] must include all type variables
  Missing type variables: "T_co" (reportGeneralTypeIssues)
generics_basic.py:172:7 - error: Generic[] or Protocol[] must include all type variables
  Missing type variables: "T_co" (reportGeneralTypeIssues)
generics_basic.py:208:37 - error: Metaclass cannot be generic (reportGeneralTypeIssues)
"""
conformance_automated = "Pass"
errors_diff = """
Expand Down
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.389"
test_duration = 2.6
test_duration = 2.5
32 changes: 29 additions & 3 deletions conformance/results/pytype/generics_basic.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
conformant = "Partial"
notes = """
False positives in examples using constrained type variables.
Argument 'covariant' to TypeVar not supported.
False negative for type variables missing from Generic/Protocol base class.
False negative for generic metaclass.
"""
output = """
Expand Down Expand Up @@ -66,17 +68,41 @@ generics_basic.py:158:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in test_my
m2[0] # E
\u001b[1m\u001b[31m~~~~~\u001b[39m\u001b[0m
generics_basic.py:182:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in test_my_iterable_any: Iterator[nothing] [assert-type]
generics_basic.py:162:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in <module>: Invalid type annotation 'Generic' [invalid-annotation]
class Bad1(Generic[int]): ... # E
\u001b[1m\u001b[31m~~~~~~~~~~~~\u001b[39m\u001b[0m
generics_basic.py:163:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in <module>: Invalid type annotation 'Protocol' [invalid-annotation]
class Bad2(Protocol[int]): ... # E
\u001b[1m\u001b[31m~~~~~~~~~~~~~\u001b[39m\u001b[0m
generics_basic.py:168:8: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in <module>: argument "covariant" to TypeVar not supported yet [not-supported-yet]
T_co = TypeVar("T_co", covariant=True)
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
generics_basic.py:169:8: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in <module>: argument "covariant" to TypeVar not supported yet [not-supported-yet]
S_co = TypeVar("S_co", covariant=True)
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
generics_basic.py:199:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in test_my_iterable_any: Iterator[nothing] [assert-type]
assert_type(iter(m), Iterator[Any])
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
"""
conformance_automated = "Fail"
errors_diff = """
Line 191: Expected 1 errors
Line 171: Expected 1 errors
Line 172: Expected 1 errors
Line 208: Expected 1 errors
Line 34: Unexpected errors ['generics_basic.py:34:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in concat: bad return type [bad-return-type]']
Line 67: Unexpected errors ['generics_basic.py:67:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in test_concat_subtype: MyStr [assert-type]']
Line 68: Unexpected errors ['generics_basic.py:68:17: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in test_concat_subtype: Function concat was called with the wrong arguments [wrong-arg-types]', 'generics_basic.py:68:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in test_concat_subtype: Any [assert-type]']
Line 182: Unexpected errors ['generics_basic.py:182:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in test_my_iterable_any: Iterator[nothing] [assert-type]']
Line 168: Unexpected errors ['generics_basic.py:168:8: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in <module>: argument "covariant" to TypeVar not supported yet [not-supported-yet]']
Line 169: Unexpected errors ['generics_basic.py:169:8: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in <module>: argument "covariant" to TypeVar not supported yet [not-supported-yet]']
Line 199: Unexpected errors ['generics_basic.py:199:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in test_my_iterable_any: Iterator[nothing] [assert-type]']
"""
2 changes: 1 addition & 1 deletion conformance/results/pytype/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pytype 2024.10.11"
test_duration = 45.1
test_duration = 45.2
10 changes: 5 additions & 5 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ <h3>Python Type System Conformance Test Results</h3>
<div class='tc-time'>2.5sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyright 1.1.389</div>
<div class='tc-time'>2.6sec</div>
<div class='tc-time'>2.5sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.23</div>
<div class='tc-time'>4.4sec</div>
<div class='tc-time'>4.3sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pytype 2024.10.11</div>
<div class='tc-time'>45.1sec</div>
<div class='tc-time'>45.2sec</div>
</th>
</tr>
<tr><th class="column" colspan="5">
Expand Down Expand Up @@ -249,8 +249,8 @@ <h3>Python Type System Conformance Test Results</h3>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_basic</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not reject the use of a constraint parameterized by another type variable.</p></span></div></th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>False positives in examples using constrained type variables.</p><p>False negative for constraint parameterized by a type variable.</p><p>False negative in custom map example.</p><p>False positive using `iter`.</p><p>False negative for generic metaclass.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>False positives in examples using constrained type variables.</p><p>False negative for generic metaclass.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>False positives in examples using constrained type variables.</p><p>False negative for constraint parameterized by a type variable.</p><p>False negative in custom map example.</p><p>False positive using `iter`.</p><p>False negative for bad type arguments to Generic/Protocol.</p><p>False negative for generic metaclass.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>False positives in examples using constrained type variables.</p><p>Argument 'covariant' to TypeVar not supported.</p><p>False negative for type variables missing from Generic/Protocol base class.</p><p>False negative for generic metaclass.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_defaults</th>
<th class="column col2 not-conformant">Unsupported</th>
Expand Down
19 changes: 18 additions & 1 deletion conformance/tests/generics_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import Any, Generic, TypeVar, assert_type
from typing import Any, Generic, Protocol, TypeVar, assert_type

T = TypeVar("T")

Expand Down Expand Up @@ -157,6 +157,23 @@ def test_my_map(m1: MyMap1[str, int], m2: MyMap2[int, str]):
m1[0] # E
m2[0] # E

# > All arguments to ``Generic`` or ``Protocol`` must be type variables.

class Bad1(Generic[int]): ... # E
class Bad2(Protocol[int]): ... # E

# > All type parameters for the class must appear within the ``Generic`` or
# > ``Protocol`` type argument list.

T_co = TypeVar("T_co", covariant=True)
S_co = TypeVar("S_co", covariant=True)

class Bad3(Iterable[T_co], Generic[S_co]): ... # E
class Bad4(Iterable[T_co], Protocol[S_co]): ... # E

# > The above rule does not apply to a bare ``Protocol`` base class.

class MyIterator(Iterator[T_co], Protocol): ... # OK

# > You can use multiple inheritance with ``Generic``

Expand Down

0 comments on commit a65f949

Please sign in to comment.