From f9cd5ee859e481e2c3094804f68d5e888d2b6ee2 Mon Sep 17 00:00:00 2001 From: kasium <15907922+kasium@users.noreply.github.com> Date: Mon, 14 Nov 2022 10:41:23 +0100 Subject: [PATCH] Improve `ast` types; revert several "redundant numeric union" changes from #7906 (#9130) * Adapt number types in ast Since mypy 0.990 type promotions was limited. This means that complex is not longer promoted to int/float, therefore we should adapt the types to list all possible types Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: AlexWaygood --- .flake8 | 4 +++- stdlib/_ast.pyi | 4 ++-- stdlib/ast.pyi | 2 +- stubs/protobuf/google/protobuf/internal/containers.pyi | 2 +- stubs/pyaudio/pyaudio.pyi | 2 +- stubs/typed-ast/typed_ast/ast27.pyi | 2 +- stubs/typed-ast/typed_ast/ast3.pyi | 2 +- 7 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.flake8 b/.flake8 index 67fa99361..87d3a985e 100644 --- a/.flake8 +++ b/.flake8 @@ -26,11 +26,13 @@ # into the typeshed codebase to unblock flake8-pyi PRs, meaning these comments # have "no matching violations" since the relevant flake8-pyi checks haven't # yet been released. +# Y041 Use "complex" instead of "float | complex" (see "The numeric tower" in PEP 484) +# TODO this check is somewhat broken, see https://github.com/PyCQA/flake8-pyi/issues/299 [flake8] per-file-ignores = *.py: E203, E301, E302, E305, E501 - *.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F822, Y037 + *.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F822, Y037, Y041 # Since typing.pyi defines "overload" this is not recognized by flake8 as typing.overload. # Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself. # https://github.com/PyCQA/flake8/issues/1079 diff --git a/stdlib/_ast.pyi b/stdlib/_ast.pyi index b7d081f6a..f723b7eff 100644 --- a/stdlib/_ast.pyi +++ b/stdlib/_ast.pyi @@ -329,7 +329,7 @@ class JoinedStr(expr): if sys.version_info < (3, 8): class Num(expr): # Deprecated in 3.8; use Constant - n: complex + n: int | float | complex class Str(expr): # Deprecated in 3.8; use Constant s: str @@ -349,7 +349,7 @@ class Constant(expr): kind: str | None # Aliases for value, for backwards compatibility s: Any - n: complex + n: int | float | complex if sys.version_info >= (3, 8): class NamedExpr(expr): diff --git a/stdlib/ast.pyi b/stdlib/ast.pyi index 56c86c950..b2cff5b00 100644 --- a/stdlib/ast.pyi +++ b/stdlib/ast.pyi @@ -12,7 +12,7 @@ if sys.version_info >= (3, 8): def __init__(cls, *args: object) -> None: ... class Num(Constant, metaclass=_ABC): - value: complex + value: int | float | complex class Str(Constant, metaclass=_ABC): value: str diff --git a/stubs/protobuf/google/protobuf/internal/containers.pyi b/stubs/protobuf/google/protobuf/internal/containers.pyi index ff2baea04..80da52a6b 100644 --- a/stubs/protobuf/google/protobuf/internal/containers.pyi +++ b/stubs/protobuf/google/protobuf/internal/containers.pyi @@ -10,7 +10,7 @@ from google.protobuf.message import Message _T = TypeVar("_T") _K = TypeVar("_K", bound=bool | int | str) -_ScalarV = TypeVar("_ScalarV", bound=bool | float | str | bytes) +_ScalarV = TypeVar("_ScalarV", bound=bool | int | float | str | bytes) _MessageV = TypeVar("_MessageV", bound=Message) _M = TypeVar("_M") diff --git a/stubs/pyaudio/pyaudio.pyi b/stubs/pyaudio/pyaudio.pyi index d773029df..a08d6cbf4 100644 --- a/stubs/pyaudio/pyaudio.pyi +++ b/stubs/pyaudio/pyaudio.pyi @@ -69,7 +69,7 @@ paMacCoreStreamInfo: PaMacCoreStreamInfo # Auxiliary types _ChannelMap: TypeAlias = Sequence[int] _PaHostApiInfo: TypeAlias = Mapping[str, str | int] -_PaDeviceInfo: TypeAlias = Mapping[str, str | float] +_PaDeviceInfo: TypeAlias = Mapping[str, str | int | float] _StreamCallback: TypeAlias = Callable[[bytes | None, int, Mapping[str, float], int], tuple[bytes | None, int]] def get_format_from_width(width: int, unsigned: bool = ...) -> int: ... diff --git a/stubs/typed-ast/typed_ast/ast27.pyi b/stubs/typed-ast/typed_ast/ast27.pyi index ff3ccdcc8..3a4d19938 100644 --- a/stubs/typed-ast/typed_ast/ast27.pyi +++ b/stubs/typed-ast/typed_ast/ast27.pyi @@ -238,7 +238,7 @@ class Repr(expr): value: expr class Num(expr): - n: complex + n: int | float | complex class Str(expr): s: str | bytes diff --git a/stubs/typed-ast/typed_ast/ast3.pyi b/stubs/typed-ast/typed_ast/ast3.pyi index 02abd13c5..e4b5d0e12 100644 --- a/stubs/typed-ast/typed_ast/ast3.pyi +++ b/stubs/typed-ast/typed_ast/ast3.pyi @@ -257,7 +257,7 @@ class Call(expr): keywords: list[keyword] class Num(expr): - n: complex + n: int | float | complex class Str(expr): s: str