Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Apr 6, 2022
1 parent 4817595 commit cb93b35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
13 changes: 10 additions & 3 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,15 @@ def _check_import_or_attribute(
error_message = Y024

# Y037 errors
elif fullname in ("typing.Optional", "typing.Union"):
error_message = Y037.format(old_syntax=fullname)
elif fullname == "typing.Optional":
error_message = Y037.format(
old_syntax=fullname, example='"int | None" instead of "Optional[int]"'
)

elif fullname == "typing.Union":
error_message = Y037.format(
old_syntax=fullname, example='"int | str" instead of "Union[int, str]"'
)

else:
return
Expand Down Expand Up @@ -1484,4 +1491,4 @@ def parse_options(
Y034 = 'Y034 {methods} usually return "self" at runtime. Consider using "_typeshed.Self" in "{method_name}", e.g. "{suggested_syntax}"'
Y035 = 'Y035 "__all__" in a stub file must have a value, as it has the same semantics as "__all__" at runtime.'
Y036 = "Y036 Badly defined {method_name} method: {details}"
Y037 = "Y037 Use PEP 604 union types instead of {old_syntax}."
Y037 = "Y037 Use PEP 604 union types instead of {old_syntax} (e.g. {example})."
24 changes: 12 additions & 12 deletions tests/pep604_union_types.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import typing
from typing import Optional # Y037 Use PEP 604 union types instead of typing.Optional.
from typing import Union # Y037 Use PEP 604 union types instead of typing.Union.
from typing import Optional # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
from typing import Union # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").

x1: Optional[str]
x2: Optional
x3: Union[str, int]
x4: Union


y1: typing.Optional[str] # Y037 Use PEP 604 union types instead of typing.Optional.
y2: typing.Optional # Y037 Use PEP 604 union types instead of typing.Optional.
y3: typing.Union[str, int] # Y037 Use PEP 604 union types instead of typing.Union.
y4: typing.Union # Y037 Use PEP 604 union types instead of typing.Union.
y1: typing.Optional[str] # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
y2: typing.Optional # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
y3: typing.Union[str, int] # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").
y4: typing.Union # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").


def f1(x: Optional[str] = ...) -> None: ...
Expand All @@ -22,9 +22,9 @@ def f5(x: Optional) -> None: ...
def f6(x: Union) -> None: ...


def g1(x: typing.Optional[str] = ...) -> None: ... # Y037 Use PEP 604 union types instead of typing.Optional.
def g2() -> typing.Optional[str]: ... # Y037 Use PEP 604 union types instead of typing.Optional.
def g3() -> typing.Union[str, int]: ... # Y037 Use PEP 604 union types instead of typing.Union.
def g4(x: typing.Union[str, int] = ...) -> None: ... # Y037 Use PEP 604 union types instead of typing.Union.
def g5(x: typing.Optional) -> None: ... # Y037 Use PEP 604 union types instead of typing.Optional.
def g6(x: typing.Union) -> None: ... # Y037 Use PEP 604 union types instead of typing.Union.
def g1(x: typing.Optional[str] = ...) -> None: ... # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
def g2() -> typing.Optional[str]: ... # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
def g3() -> typing.Union[str, int]: ... # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").
def g4(x: typing.Union[str, int] = ...) -> None: ... # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").
def g5(x: typing.Optional) -> None: ... # Y037 Use PEP 604 union types instead of typing.Optional (e.g. "int | None" instead of "Optional[int]").
def g6(x: typing.Union) -> None: ... # Y037 Use PEP 604 union types instead of typing.Union (e.g. "int | str" instead of "Union[int, str]").

0 comments on commit cb93b35

Please sign in to comment.