From dc60875f1b42599c63ca23cb677fbb9c67b196c8 Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Tue, 8 Nov 2022 08:55:59 +0000 Subject: [PATCH 1/7] 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 Closes #9129 --- stdlib/_ast.pyi | 4 ++-- stdlib/ast.pyi | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/_ast.pyi b/stdlib/_ast.pyi index b7d081f6acb2..f723b7eff8bb 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 56c86c950f0c..b2cff5b00264 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 From 34e634cc0dafb2052933aef13be35c302ed99963 Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Tue, 8 Nov 2022 09:14:56 +0000 Subject: [PATCH 2/7] Fix flake8 --- stdlib/_ast.pyi | 4 ++-- stdlib/ast.pyi | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/_ast.pyi b/stdlib/_ast.pyi index f723b7eff8bb..267de77b91c2 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: int | float | complex + n: int | float | complex # noqa: Y041 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: int | float | complex + n: int | float | complex # noqa: Y041 if sys.version_info >= (3, 8): class NamedExpr(expr): diff --git a/stdlib/ast.pyi b/stdlib/ast.pyi index b2cff5b00264..9c1701f1f10d 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: int | float | complex + value: int | float | complex # noqa: Y041 class Str(Constant, metaclass=_ABC): value: str From 3f71e94fcd8a116afc05a387e3d86d28109555aa Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Tue, 8 Nov 2022 10:03:06 +0000 Subject: [PATCH 3/7] More --- .flake8 | 4 +++- stdlib/_ast.pyi | 4 ++-- stdlib/ast.pyi | 2 +- stdlib/random.pyi | 4 +--- stdlib/turtle.pyi | 4 ++-- stubs/typed-ast/typed_ast/ast27.pyi | 2 +- stubs/typed-ast/typed_ast/ast3.pyi | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.flake8 b/.flake8 index 67fa99361621..a516be29b556 100644 --- a/.flake8 +++ b/.flake8 @@ -26,6 +26,8 @@ # 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) +# Type promotions was changed with mypy 0.990 so all possible types must be listed [flake8] per-file-ignores = @@ -35,7 +37,7 @@ per-file-ignores = # Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself. # https://github.com/PyCQA/flake8/issues/1079 # F811 redefinition of unused '...' - stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F811, F822, Y037 + stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F811, F822, Y037, Y041 # Generated protobuf files include docstrings *_pb2.pyi: B, E301, E302, E305, E501, E701, NQA102, Y021, Y026 diff --git a/stdlib/_ast.pyi b/stdlib/_ast.pyi index 267de77b91c2..f723b7eff8bb 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: int | float | complex # noqa: Y041 + 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: int | float | complex # noqa: Y041 + n: int | float | complex if sys.version_info >= (3, 8): class NamedExpr(expr): diff --git a/stdlib/ast.pyi b/stdlib/ast.pyi index 9c1701f1f10d..b2cff5b00264 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: int | float | complex # noqa: Y041 + value: int | float | complex class Str(Constant, metaclass=_ABC): value: str diff --git a/stdlib/random.pyi b/stdlib/random.pyi index a2a1d956e78f..af60b98995d7 100644 --- a/stdlib/random.pyi +++ b/stdlib/random.pyi @@ -41,10 +41,8 @@ class Random(_random.Random): VERSION: ClassVar[int] def __init__(self, x: Any = ...) -> None: ... # Using other `seed` types is deprecated since 3.9 and removed in 3.11 - # Ignore Y041, since random.seed doesn't treat int like a float subtype. Having an explicit - # int better documents conventional usage of random.seed. if sys.version_info >= (3, 9): - def seed(self, a: int | float | str | bytes | bytearray | None = ..., version: int = ...) -> None: ... # type: ignore[override] # noqa: Y041 + def seed(self, a: int | float | str | bytes | bytearray | None = ..., version: int = ...) -> None: ... # type: ignore[override] else: def seed(self, a: Any = ..., version: int = ...) -> None: ... diff --git a/stdlib/turtle.pyi b/stdlib/turtle.pyi index 13197c336e5e..b4d2ded5ddbb 100644 --- a/stdlib/turtle.pyi +++ b/stdlib/turtle.pyi @@ -420,8 +420,8 @@ class _Screen(TurtleScreen): # Note int and float are interpreted differently, hence the Union instead of just float def setup( self, - width: int | float = ..., # noqa: Y041 - height: int | float = ..., # noqa: Y041 + width: int | float = ..., + height: int | float = ..., startx: int | None = ..., starty: int | None = ..., ) -> None: ... diff --git a/stubs/typed-ast/typed_ast/ast27.pyi b/stubs/typed-ast/typed_ast/ast27.pyi index ff3ccdcc86d8..3a4d19938483 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 02abd13c5a5b..e4b5d0e12b2e 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 From 8bfc5ce793ce2ab9ccf730f215db0b60a79150e4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Nov 2022 10:04:51 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/turtle.pyi | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/stdlib/turtle.pyi b/stdlib/turtle.pyi index b4d2ded5ddbb..ed460a7ab95f 100644 --- a/stdlib/turtle.pyi +++ b/stdlib/turtle.pyi @@ -419,11 +419,7 @@ class _Screen(TurtleScreen): def __init__(self) -> None: ... # Note int and float are interpreted differently, hence the Union instead of just float def setup( - self, - width: int | float = ..., - height: int | float = ..., - startx: int | None = ..., - starty: int | None = ..., + self, width: int | float = ..., height: int | float = ..., startx: int | None = ..., starty: int | None = ... ) -> None: ... def title(self, titlestring: str) -> None: ... def bye(self) -> None: ... From 24551cb50be4fc5ce2c295c0d56b9dc073a228e1 Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Tue, 8 Nov 2022 11:00:48 +0000 Subject: [PATCH 5/7] Fix --- .flake8 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index a516be29b556..1ec50ef2a691 100644 --- a/.flake8 +++ b/.flake8 @@ -32,12 +32,12 @@ [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 # F811 redefinition of unused '...' - stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F811, F822, Y037, Y041 + stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, NQA102, F401, F403, F405, F811, F822, Y037 # Generated protobuf files include docstrings *_pb2.pyi: B, E301, E302, E305, E501, E701, NQA102, Y021, Y026 From f06b3cfd5416437f9384afca7b86be8b3c7595e3 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 14 Nov 2022 09:03:07 +0000 Subject: [PATCH 6/7] Revert changes to `turtle` and `random` --- stdlib/random.pyi | 4 +++- stdlib/turtle.pyi | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/random.pyi b/stdlib/random.pyi index af60b98995d7..a2a1d956e78f 100644 --- a/stdlib/random.pyi +++ b/stdlib/random.pyi @@ -41,8 +41,10 @@ class Random(_random.Random): VERSION: ClassVar[int] def __init__(self, x: Any = ...) -> None: ... # Using other `seed` types is deprecated since 3.9 and removed in 3.11 + # Ignore Y041, since random.seed doesn't treat int like a float subtype. Having an explicit + # int better documents conventional usage of random.seed. if sys.version_info >= (3, 9): - def seed(self, a: int | float | str | bytes | bytearray | None = ..., version: int = ...) -> None: ... # type: ignore[override] + def seed(self, a: int | float | str | bytes | bytearray | None = ..., version: int = ...) -> None: ... # type: ignore[override] # noqa: Y041 else: def seed(self, a: Any = ..., version: int = ...) -> None: ... diff --git a/stdlib/turtle.pyi b/stdlib/turtle.pyi index ed460a7ab95f..13197c336e5e 100644 --- a/stdlib/turtle.pyi +++ b/stdlib/turtle.pyi @@ -419,7 +419,11 @@ class _Screen(TurtleScreen): def __init__(self) -> None: ... # Note int and float are interpreted differently, hence the Union instead of just float def setup( - self, width: int | float = ..., height: int | float = ..., startx: int | None = ..., starty: int | None = ... + self, + width: int | float = ..., # noqa: Y041 + height: int | float = ..., # noqa: Y041 + startx: int | None = ..., + starty: int | None = ..., ) -> None: ... def title(self, titlestring: str) -> None: ... def bye(self) -> None: ... From 3253b05fe87f9a0a4816a1eb5973f429a4c4cace Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 14 Nov 2022 09:07:40 +0000 Subject: [PATCH 7/7] Also revert changes to pyaudio and protobuf, improve config-file comment --- .flake8 | 2 +- stubs/protobuf/google/protobuf/internal/containers.pyi | 2 +- stubs/pyaudio/pyaudio.pyi | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index 1ec50ef2a691..87d3a985e0ad 100644 --- a/.flake8 +++ b/.flake8 @@ -27,7 +27,7 @@ # 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) -# Type promotions was changed with mypy 0.990 so all possible types must be listed +# TODO this check is somewhat broken, see https://github.com/PyCQA/flake8-pyi/issues/299 [flake8] per-file-ignores = diff --git a/stubs/protobuf/google/protobuf/internal/containers.pyi b/stubs/protobuf/google/protobuf/internal/containers.pyi index ff2baea04f59..80da52a6bf94 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 d773029dfd58..a08d6cbf4cc6 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: ...