Skip to content

Commit

Permalink
Make syntax of generic overload variants in messages close to PEP 695 (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
JukkaL and ilevkivskyi committed Jun 20, 2024
1 parent 7cb733a commit 6877d6f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
)
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- --------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6877d6f

Please sign in to comment.