From 6877d6fb668c24d984268b937c3eeed8c4cad296 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 20 Jun 2024 10:01:16 +0100 Subject: [PATCH] Make syntax of generic overload variants in messages close to PEP 695 (#17401) We used a custom syntax for type variable bounds and restrictions. Use PEP 695 syntax instead (or at least something closer to PEP 695 syntax). Co-authored-by: Ivan Levkivskyi --- mypy/messages.py | 4 ++-- test-data/unit/check-generic-subtyping.test | 2 +- test-data/unit/check-overloading.test | 12 ++++++------ test-data/unit/check-protocols.test | 4 ++-- test-data/unit/fine-grained.test | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index c3a34bd41aba..27f152413151 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2928,10 +2928,10 @@ def [T <: int] f(self, x: int, y: T) -> None isinstance(upper_bound, Instance) and upper_bound.type.fullname != "builtins.object" ): - tvars.append(f"{tvar.name} <: {format_type_bare(upper_bound, options)}") + tvars.append(f"{tvar.name}: {format_type_bare(upper_bound, options)}") elif tvar.values: tvars.append( - "{} in ({})".format( + "{}: ({})".format( tvar.name, ", ".join([format_type_bare(tp, options) for tp in tvar.values]), ) diff --git a/test-data/unit/check-generic-subtyping.test b/test-data/unit/check-generic-subtyping.test index fd40f128ff4a..90180e0f83f6 100644 --- a/test-data/unit/check-generic-subtyping.test +++ b/test-data/unit/check-generic-subtyping.test @@ -306,7 +306,7 @@ main:14: error: Signature of "f" incompatible with supertype "A" main:14: note: Superclass: main:14: note: def [S] f(self, x: int, y: S) -> None main:14: note: Subclass: -main:14: note: def [T1 <: str, S] f(self, x: T1, y: S) -> None +main:14: note: def [T1: str, S] f(self, x: T1, y: S) -> None -- Inheritance from generic types with implicit dynamic supertype -- -------------------------------------------------------------- diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 03863b2978ba..48d5996b226f 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -1276,7 +1276,7 @@ f('x')() # E: "str" not callable f(1)() # E: "bool" not callable f(1.1) # E: No overload variant of "f" matches argument type "float" \ # N: Possible overload variants: \ - # N: def [T <: str] f(x: T) -> T \ + # N: def [T: str] f(x: T) -> T \ # N: def f(x: int) -> bool f(mystr())() # E: "mystr" not callable [builtins fixtures/primitives.pyi] @@ -1298,8 +1298,8 @@ def g(x: U, y: V) -> None: f(x)() # E: "mystr" not callable f(y) # E: No overload variant of "f" matches argument type "V" \ # N: Possible overload variants: \ - # N: def [T <: str] f(x: T) -> T \ - # N: def [T <: str] f(x: List[T]) -> None + # N: def [T: str] f(x: T) -> T \ + # N: def [T: str] f(x: List[T]) -> None a = f([x]) reveal_type(a) # N: Revealed type is "None" f([y]) # E: Value of type variable "T" of "f" cannot be "V" @@ -1351,7 +1351,7 @@ f(b'1')() # E: "str" not callable f(1.0) # E: No overload variant of "f" matches argument type "float" \ # N: Possible overload variants: \ # N: def f(x: int) -> int \ - # N: def [AnyStr in (bytes, str)] f(x: AnyStr) -> str + # N: def [AnyStr: (bytes, str)] f(x: AnyStr) -> str @overload def g(x: AnyStr, *a: AnyStr) -> None: pass @@ -4949,7 +4949,7 @@ x: Any reveal_type(attr(x)) # N: Revealed type is "Any" attr("hi", 1) # E: No overload variant of "attr" matches argument types "str", "int" \ # N: Possible overload variants: \ - # N: def [T in (int, float)] attr(default: T, blah: int = ...) -> T \ + # N: def [T: (int, float)] attr(default: T, blah: int = ...) -> T \ # N: def attr(default: Any = ...) -> int [file lib.pyi] from typing import overload, Any, TypeVar @@ -4972,7 +4972,7 @@ x: Any reveal_type(attr(x)) # N: Revealed type is "Any" attr("hi", 1) # E: No overload variant of "attr" matches argument types "str", "int" \ # N: Possible overload variants: \ - # N: def [T <: int] attr(default: T = ..., blah: int = ...) -> T \ + # N: def [T: int] attr(default: T = ..., blah: int = ...) -> T \ # N: def attr(default: Any = ...) -> int [file lib.pyi] from typing import overload, TypeVar, Any diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index e73add454a67..ee7556461fd3 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2182,7 +2182,7 @@ main:11: note: Following member(s) of "B" have conflicts: main:11: note: Expected: main:11: note: def [T] f(self, x: T) -> None main:11: note: Got: -main:11: note: def [S <: int, T] f(self, x: S, y: T) -> None +main:11: note: def [S: int, T] f(self, x: S, y: T) -> None [case testProtocolIncompatibilityWithGenericRestricted] from typing import Protocol, TypeVar @@ -2202,7 +2202,7 @@ main:11: note: Following member(s) of "B" have conflicts: main:11: note: Expected: main:11: note: def [T] f(self, x: T) -> None main:11: note: Got: -main:11: note: def [S in (int, str), T] f(self, x: S, y: T) -> None +main:11: note: def [S: (int, str), T] f(self, x: S, y: T) -> None [case testProtocolIncompatibilityWithManyOverloads] from typing import Protocol, overload diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index a87f8ceca15c..2a652e50b1e6 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -7138,7 +7138,7 @@ T = TypeVar('T', bound=str) a.py:2: error: No overload variant of "f" matches argument type "int" a.py:2: note: Possible overload variants: a.py:2: note: def f(x: C) -> None -a.py:2: note: def [c.T <: str] f(x: c.T) -> c.T +a.py:2: note: def [c.T: str] f(x: c.T) -> c.T [case testOverloadsGenericToNonGeneric] import a