From d9fd3bf4d4bcfcc8fd7b25083245ac9114184bee Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 10 Feb 2023 21:13:51 -0500 Subject: [PATCH 1/4] Type and mark as final module-level dunders not meant to be overwritten --- stdlib/_csv.pyi | 4 ++-- stdlib/_decimal.pyi | 6 +++--- stdlib/_heapq.pyi | 3 ++- stdlib/cgitb.pyi | 3 ++- stdlib/heapq.pyi | 3 ++- stdlib/pydoc.pyi | 10 +++++----- stdlib/sys.pyi | 11 ++++++----- stdlib/unittest/mock.pyi | 4 ++-- 8 files changed, 24 insertions(+), 20 deletions(-) diff --git a/stdlib/_csv.pyi b/stdlib/_csv.pyi index c3f1dd58cd3c..7e9b9e4e7a79 100644 --- a/stdlib/_csv.pyi +++ b/stdlib/_csv.pyi @@ -1,9 +1,9 @@ from _typeshed import SupportsWrite from collections.abc import Iterable, Iterator from typing import Any -from typing_extensions import Literal, TypeAlias +from typing_extensions import Final, Literal, TypeAlias -__version__: str +__version__: Final[str] QUOTE_ALL: Literal[1] QUOTE_MINIMAL: Literal[0] diff --git a/stdlib/_decimal.pyi b/stdlib/_decimal.pyi index 3f997e7d9a37..b8208fe180a1 100644 --- a/stdlib/_decimal.pyi +++ b/stdlib/_decimal.pyi @@ -3,14 +3,14 @@ import sys from collections.abc import Container, Sequence from types import TracebackType from typing import Any, ClassVar, NamedTuple, overload -from typing_extensions import Literal, Self, TypeAlias +from typing_extensions import Final, Literal, Self, TypeAlias _Decimal: TypeAlias = Decimal | int _DecimalNew: TypeAlias = Decimal | float | str | tuple[int, Sequence[int], int] _ComparableNum: TypeAlias = Decimal | float | numbers.Rational -__version__: str -__libmpdec_version__: str +__version__: Final[str] +__libmpdec_version__: Final[str] class DecimalTuple(NamedTuple): sign: int diff --git a/stdlib/_heapq.pyi b/stdlib/_heapq.pyi index 90dc28deb71f..8d6c3e88103e 100644 --- a/stdlib/_heapq.pyi +++ b/stdlib/_heapq.pyi @@ -1,8 +1,9 @@ from typing import Any, TypeVar +from typing_extensions import Final _T = TypeVar("_T") -__about__: str +__about__: Final[str] def heapify(__heap: list[Any]) -> None: ... def heappop(__heap: list[_T]) -> _T: ... diff --git a/stdlib/cgitb.pyi b/stdlib/cgitb.pyi index 04bcbfb0d13d..4c315bf6ca39 100644 --- a/stdlib/cgitb.pyi +++ b/stdlib/cgitb.pyi @@ -2,8 +2,9 @@ from _typeshed import OptExcInfo, StrOrBytesPath from collections.abc import Callable from types import FrameType, TracebackType from typing import IO, Any +from typing_extensions import Final -__UNDEF__: object # undocumented sentinel +__UNDEF__: Final[object] # undocumented sentinel def reset() -> str: ... # undocumented def small(text: str) -> str: ... # undocumented diff --git a/stdlib/heapq.pyi b/stdlib/heapq.pyi index 9d7815507ea5..61418b3704d6 100644 --- a/stdlib/heapq.pyi +++ b/stdlib/heapq.pyi @@ -2,12 +2,13 @@ from _heapq import * from _typeshed import SupportsRichComparison from collections.abc import Callable, Iterable from typing import Any, TypeVar +from typing_extensions import Final __all__ = ["heappush", "heappop", "heapify", "heapreplace", "merge", "nlargest", "nsmallest", "heappushpop"] _S = TypeVar("_S") -__about__: str +__about__: Final[str] def merge( *iterables: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None, reverse: bool = False diff --git a/stdlib/pydoc.pyi b/stdlib/pydoc.pyi index 9bcd8659fc8c..c6893d50c66a 100644 --- a/stdlib/pydoc.pyi +++ b/stdlib/pydoc.pyi @@ -6,16 +6,16 @@ from collections.abc import Callable, Container, Mapping, MutableMapping from reprlib import Repr from types import MethodType, ModuleType, TracebackType from typing import IO, Any, AnyStr, NoReturn, TypeVar -from typing_extensions import TypeGuard +from typing_extensions import Final, TypeGuard __all__ = ["help"] _T = TypeVar("_T") -__author__: str -__date__: str -__version__: str -__credits__: str +__author__: Final[str] +__date__: Final[str] +__version__: Final[str] +__credits__: Final[str] def pathdirs() -> list[str]: ... def getdoc(object: object) -> str: ... diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 725f66794cf6..b63e970f4cd6 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -7,7 +7,7 @@ from importlib.machinery import ModuleSpec from io import TextIOWrapper from types import FrameType, ModuleType, TracebackType from typing import Any, NoReturn, Protocol, TextIO, TypeVar, overload -from typing_extensions import Literal, TypeAlias, final +from typing_extensions import Final, Literal, TypeAlias, final _T = TypeVar("_T") @@ -62,9 +62,10 @@ stdout: TextIO stderr: TextIO if sys.version_info >= (3, 10): stdlib_module_names: frozenset[str] -__stdin__: TextIOWrapper -__stdout__: TextIOWrapper -__stderr__: TextIOWrapper + +__stdin__: Final[TextIOWrapper] # Contains the original value of stdin +__stdout__: Final[TextIOWrapper] # Contains the original value of stdout +__stderr__: Final[TextIOWrapper] # Contains the original value of stderr tracebacklimit: int version: str api_version: int @@ -278,7 +279,7 @@ if sys.platform == "win32": def intern(__string: str) -> str: ... def is_finalizing() -> bool: ... -__breakpointhook__: Any # contains the original value of breakpointhook +__breakpointhook__: Final = breakpointhook # Contains the original value of breakpointhook def breakpointhook(*args: Any, **kwargs: Any) -> Any: ... diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index 3b1989edca9e..12ce2d2c78aa 100644 --- a/stdlib/unittest/mock.pyi +++ b/stdlib/unittest/mock.pyi @@ -3,7 +3,7 @@ from collections.abc import Awaitable, Callable, Coroutine, Iterable, Mapping, S from contextlib import _GeneratorContextManager from types import TracebackType from typing import Any, Generic, TypeVar, overload -from typing_extensions import Literal, Self, TypeAlias +from typing_extensions import Final, Literal, Self, TypeAlias _T = TypeVar("_T") _TT = TypeVar("_TT", bound=type[Any]) @@ -47,7 +47,7 @@ else: "seal", ) -__version__: str +__version__: Final[str] FILTER_DIR: Any From 7aa50288dace853a3ba6b1581ded2764f84ad38d Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 10 Feb 2023 21:30:34 -0500 Subject: [PATCH 2/4] Fix pytype error --- stdlib/sys.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index b63e970f4cd6..25a9cb714934 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -278,9 +278,7 @@ if sys.platform == "win32": def intern(__string: str) -> str: ... def is_finalizing() -> bool: ... - -__breakpointhook__: Final = breakpointhook # Contains the original value of breakpointhook - +def __breakpointhook__(*args: Any, **kwargs: Any) -> Any: ... # Contains the original value of breakpointhook def breakpointhook(*args: Any, **kwargs: Any) -> Any: ... if sys.platform != "win32": From 61d547911b38cc0eadc5a1b9ae95b4fc8fe2c701 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 10 Feb 2023 21:41:35 -0500 Subject: [PATCH 3/4] Fix pytype error --- stdlib/sys.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 25a9cb714934..065680634828 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -278,7 +278,9 @@ if sys.platform == "win32": def intern(__string: str) -> str: ... def is_finalizing() -> bool: ... -def __breakpointhook__(*args: Any, **kwargs: Any) -> Any: ... # Contains the original value of breakpointhook + +__breakpointhook__ = breakpointhook # Contains the original value of breakpointhook + def breakpointhook(*args: Any, **kwargs: Any) -> Any: ... if sys.platform != "win32": From 23744af15f473e3d40b6f2cfa55338eaa5f2f2fc Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 12 Feb 2023 09:36:03 -0500 Subject: [PATCH 4/4] PR comment --- stdlib/sys.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 065680634828..e12881599b4a 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -278,11 +278,10 @@ if sys.platform == "win32": def intern(__string: str) -> str: ... def is_finalizing() -> bool: ... +def breakpointhook(*args: Any, **kwargs: Any) -> Any: ... __breakpointhook__ = breakpointhook # Contains the original value of breakpointhook -def breakpointhook(*args: Any, **kwargs: Any) -> Any: ... - if sys.platform != "win32": def setdlopenflags(__flags: int) -> None: ...