Skip to content

Commit

Permalink
Update pos-only unit tests for Python 3.10.7 (#13660)
Browse files Browse the repository at this point in the history
The CI has started to sporadically fail depending on whether 3.10.6 is
picked up by GitHub Actions (okay) or 3.10.7 (not okay). For example:

- https://github.com/python/mypy/actions/runs/3046671132/jobs/4909772702
- https://github.com/python/mypy/actions/runs/3046723692/jobs/4909887963

On Python 3.10.7 (but not on Python 3.10.6), mypy correctly rejects
using PEP 570 syntax unless `--python-version` is set to 3.8 or higher
(this is due to python/cpython#95935). However,
this makes several unit tests fail.

This PR updates those unit tests so that the CI passes on both 3.10.6
and 3.10.7
  • Loading branch information
AlexWaygood committed Sep 14, 2022
1 parent a784b48 commit 1df4ac2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
14 changes: 9 additions & 5 deletions test-data/unit/check-parameter-specification.test
Expand Up @@ -573,7 +573,7 @@ reveal_type(f(n)) # N: Revealed type is "def (builtins.int, builtins.bytes) ->
[builtins fixtures/paramspec.pyi]

[case testParamSpecConcatenateNamedArgs]
# flags: --strict-concatenate
# flags: --python-version 3.8 --strict-concatenate
# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
Expand All @@ -595,12 +595,14 @@ def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/paramspec.pyi]
[out]
main:10: error: invalid syntax
main:10: error: invalid syntax; you likely need to run mypy using Python 3.8 or newer
[out version>=3.8]
main:17: error: Incompatible return value type (got "Callable[[Arg(int, 'x'), **P], R]", expected "Callable[[int, **P], R]")
main:17: note: This may be because "result" has arguments named: "x"

[case testNonStrictParamSpecConcatenateNamedArgs]
# flags: --python-version 3.8

# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
Expand All @@ -622,7 +624,7 @@ def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/paramspec.pyi]
[out]
main:9: error: invalid syntax
main:11: error: invalid syntax; you likely need to run mypy using Python 3.8 or newer
[out version>=3.8]

[case testParamSpecConcatenateWithTypeVar]
Expand All @@ -644,6 +646,8 @@ reveal_type(n(42)) # N: Revealed type is "None"
[builtins fixtures/paramspec.pyi]

[case testCallablesAsParameters]
# flags: --python-version 3.8

# credits to https://github.com/microsoft/pyright/issues/2705
from typing_extensions import ParamSpec, Concatenate
from typing import Generic, Callable, Any
Expand All @@ -661,9 +665,9 @@ reveal_type(abc)
bar(abc)
[builtins fixtures/paramspec.pyi]
[out]
main:11: error: invalid syntax
main:13: error: invalid syntax; you likely need to run mypy using Python 3.8 or newer
[out version>=3.8]
main:14: note: Revealed type is "__main__.Foo[[builtins.int, b: builtins.str]]"
main:16: note: Revealed type is "__main__.Foo[[builtins.int, b: builtins.str]]"

[case testSolveParamSpecWithSelfType]
from typing_extensions import ParamSpec, Concatenate
Expand Down
66 changes: 43 additions & 23 deletions test-data/unit/check-python38.test
Expand Up @@ -115,28 +115,33 @@ def g(x: int): ...
) # type: ignore # E: Unused "type: ignore" comment

[case testPEP570ArgTypesMissing]
# flags: --disallow-untyped-defs
# flags: --python-version 3.8 --disallow-untyped-defs
def f(arg, /) -> None: ... # E: Function is missing a type annotation for one or more arguments

[case testPEP570ArgTypesBadDefault]
# flags: --python-version 3.8
def f(arg: int = "ERROR", /) -> None: ... # E: Incompatible default for argument "arg" (default has type "str", argument has type "int")

[case testPEP570ArgTypesDefault]
# flags: --python-version 3.8
def f(arg: int = 0, /) -> None:
reveal_type(arg) # N: Revealed type is "builtins.int"

[case testPEP570ArgTypesRequired]
# flags: --python-version 3.8
def f(arg: int, /) -> None:
reveal_type(arg) # N: Revealed type is "builtins.int"

[case testPEP570Required]
# flags: --python-version 3.8
def f(arg: int, /) -> None: ... # N: "f" defined here
f(1)
f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"

[case testPEP570Default]
# flags: --python-version 3.8
def f(arg: int = 0, /) -> None: ... # N: "f" defined here
f()
f(1)
Expand All @@ -145,6 +150,7 @@ f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"

[case testPEP570Calls]
# flags: --python-version 3.8 --no-strict-optional
from typing import Any, Dict
def f(p, /, p_or_kw, *, kw) -> None: ... # N: "f" defined here
d = None # type: Dict[Any, Any]
Expand All @@ -157,42 +163,49 @@ f(**d) # E: Missing positional argument "p_or_kw" in call to "f"
[builtins fixtures/dict.pyi]

[case testPEP570Signatures1]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /, p_or_kw: int, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
reveal_type(kw) # N: Revealed type is "builtins.str"

[case testPEP570Signatures2]
# flags: --python-version 3.8
def f(p1: bytes, p2: float = 0.0, /, p_or_kw: int = 0, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
reveal_type(kw) # N: Revealed type is "builtins.str"

[case testPEP570Signatures3]
# flags: --python-version 3.8
def f(p1: bytes, p2: float = 0.0, /, *, kw: int) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(kw) # N: Revealed type is "builtins.int"

[case testPEP570Signatures4]
# flags: --python-version 3.8
def f(p1: bytes, p2: int = 0, /) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.int"

[case testPEP570Signatures5]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /, p_or_kw: int) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"

[case testPEP570Signatures6]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"

[case testPEP570Unannotated]
# flags: --python-version 3.8
def f(arg, /): ... # N: "f" defined here
g = lambda arg, /: arg
def h(arg=0, /): ... # N: "h" defined here
Expand Down Expand Up @@ -561,6 +574,7 @@ def foo() -> None:
[builtins fixtures/dict.pyi]

[case testOverloadWithPositionalOnlySelf]
# flags: --python-version 3.8
from typing import overload, Optional

class Foo:
Expand All @@ -585,6 +599,7 @@ class Bar:
[builtins fixtures/bool.pyi]

[case testOverloadPositionalOnlyErrorMessage]
# flags: --python-version 3.8
from typing import overload

@overload
Expand All @@ -595,12 +610,13 @@ def foo(a): ...

foo(a=1)
[out]
main:9: error: No overload variant of "foo" matches argument type "int"
main:9: note: Possible overload variants:
main:9: note: def foo(int, /) -> Any
main:9: note: def foo(a: str) -> Any
main:10: error: No overload variant of "foo" matches argument type "int"
main:10: note: Possible overload variants:
main:10: note: def foo(int, /) -> Any
main:10: note: def foo(a: str) -> Any

[case testOverloadPositionalOnlyErrorMessageAllTypes]
# flags: --python-version 3.8
from typing import overload

@overload
Expand All @@ -611,12 +627,13 @@ def foo(a, b, *, c): ...

foo(a=1)
[out]
main:9: error: No overload variant of "foo" matches argument type "int"
main:9: note: Possible overload variants:
main:9: note: def foo(int, /, b: int, *, c: int) -> Any
main:9: note: def foo(a: str, b: int, *, c: int) -> Any
main:10: error: No overload variant of "foo" matches argument type "int"
main:10: note: Possible overload variants:
main:10: note: def foo(int, /, b: int, *, c: int) -> Any
main:10: note: def foo(a: str, b: int, *, c: int) -> Any

[case testOverloadPositionalOnlyErrorMessageMultiplePosArgs]
# flags: --python-version 3.8
from typing import overload

@overload
Expand All @@ -627,12 +644,13 @@ def foo(a, b, c, d): ...

foo(a=1)
[out]
main:9: error: No overload variant of "foo" matches argument type "int"
main:9: note: Possible overload variants:
main:9: note: def foo(int, int, int, /, d: str) -> Any
main:9: note: def foo(a: str, b: int, c: int, d: str) -> Any
main:10: error: No overload variant of "foo" matches argument type "int"
main:10: note: Possible overload variants:
main:10: note: def foo(int, int, int, /, d: str) -> Any
main:10: note: def foo(a: str, b: int, c: int, d: str) -> Any

[case testOverloadPositionalOnlyErrorMessageMethod]
# flags: --python-version 3.8
from typing import overload

class Some:
Expand All @@ -646,13 +664,14 @@ class Some:

Some().foo(a=1)
[out]
main:12: error: No overload variant of "foo" of "Some" matches argument type "int"
main:12: note: Possible overload variants:
main:12: note: def foo(self, int, /) -> Any
main:12: note: def foo(self, float, /) -> Any
main:12: note: def foo(self, a: str) -> Any
main:13: error: No overload variant of "foo" of "Some" matches argument type "int"
main:13: note: Possible overload variants:
main:13: note: def foo(self, int, /) -> Any
main:13: note: def foo(self, float, /) -> Any
main:13: note: def foo(self, a: str) -> Any

[case testOverloadPositionalOnlyErrorMessageClassMethod]
# flags: --python-version 3.8
from typing import overload

class Some:
Expand All @@ -671,13 +690,14 @@ class Some:
Some.foo(a=1)
[builtins fixtures/classmethod.pyi]
[out]
main:16: error: No overload variant of "foo" of "Some" matches argument type "int"
main:16: note: Possible overload variants:
main:16: note: def foo(cls, int, /) -> Any
main:16: note: def foo(cls, float, /) -> Any
main:16: note: def foo(cls, a: str) -> Any
main:17: error: No overload variant of "foo" of "Some" matches argument type "int"
main:17: note: Possible overload variants:
main:17: note: def foo(cls, int, /) -> Any
main:17: note: def foo(cls, float, /) -> Any
main:17: note: def foo(cls, a: str) -> Any

[case testUnpackWithDuplicateNamePositionalOnly]
# flags: --python-version 3.8
from typing_extensions import Unpack, TypedDict

class Person(TypedDict):
Expand Down

0 comments on commit 1df4ac2

Please sign in to comment.