Skip to content

Add @final to many unsubclassable stdlib classes #6299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/_thread.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import sys
from threading import Thread
from types import TracebackType
from typing import Any, Callable, NoReturn, Optional, Tuple, Type
from typing_extensions import final

error = RuntimeError

def _count() -> int: ...

_dangling: Any

@final
class LockType:
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
def release(self) -> None: ...
Expand All @@ -29,6 +31,7 @@ TIMEOUT_MAX: float

if sys.version_info >= (3, 8):
def get_native_id() -> int: ... # only available on some platforms
@final
class _ExceptHookArgs(Tuple[Type[BaseException], Optional[BaseException], Optional[TracebackType], Optional[Thread]]):
@property
def exc_type(self) -> Type[BaseException]: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/_tkinter.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any
from typing_extensions import Literal
from typing_extensions import Literal, final

# _tkinter is meant to be only used internally by tkinter, but some tkinter
# functions e.g. return _tkinter.Tcl_Obj objects. Tcl_Obj represents a Tcl
Expand All @@ -14,6 +14,7 @@ from typing_extensions import Literal
# >>> text.tag_add('foo', '1.0', 'end')
# >>> text.tag_ranges('foo')
# (<textindex object: '1.0'>, <textindex object: '2.0'>)
@final
class Tcl_Obj:
string: str # str(tclobj) returns this
typename: str
Expand All @@ -37,6 +38,7 @@ class TclError(Exception): ...
#
# eval always returns str because _tkinter_tkapp_eval_impl in _tkinter.c calls
# Tkapp_UnicodeResult, and it returns a string when it succeeds.
@final
class TkappType:
# Please keep in sync with tkinter.Tk
def call(self, __command: Any, *args: Any) -> Any: ...
Expand Down
3 changes: 3 additions & 0 deletions stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import sys
from typing import Any, Callable, Generic, TypeVar, overload
from typing_extensions import final

if sys.version_info >= (3, 9):
from types import GenericAlias

_C = TypeVar("_C", bound=Callable[..., Any])
_T = TypeVar("_T")

@final
class CallableProxyType(Generic[_C]): # "weakcallableproxy"
def __getattr__(self, attr: str) -> Any: ...

@final
class ProxyType(Generic[_T]): # "weakproxy"
def __getattr__(self, attr: str) -> Any: ...

Expand Down
4 changes: 2 additions & 2 deletions stdlib/_winapi.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from typing import Any, NoReturn, Sequence, overload
from typing_extensions import Literal
from typing_extensions import Literal, final

CREATE_NEW_CONSOLE: int
CREATE_NEW_PROCESS_GROUP: int
Expand Down Expand Up @@ -126,7 +126,7 @@ def WriteFile(handle: int, buffer: bytes, overlapped: Literal[True]) -> tuple[Ov
def WriteFile(handle: int, buffer: bytes, overlapped: Literal[False] = ...) -> tuple[int, int]: ...
@overload
def WriteFile(handle: int, buffer: bytes, overlapped: int | bool) -> tuple[Any, int]: ...

@final
class Overlapped:
event: int
def GetOverlappedResult(self, __wait: bool) -> tuple[int, int]: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/bz2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sys
from _compression import BaseStream
from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer
from typing import IO, Any, Iterable, Protocol, TextIO, TypeVar, overload
from typing_extensions import Literal, SupportsIndex
from typing_extensions import Literal, SupportsIndex, final

# The following attributes and methods are optional:
# def fileno(self) -> int: ...
Expand Down Expand Up @@ -118,11 +118,13 @@ class BZ2File(BaseStream, IO[bytes]):
def write(self, data: ReadableBuffer) -> int: ...
def writelines(self, seq: Iterable[ReadableBuffer]) -> None: ...

@final
class BZ2Compressor(object):
def __init__(self, compresslevel: int = ...) -> None: ...
def compress(self, __data: bytes) -> bytes: ...
def flush(self) -> bytes: ...

@final
class BZ2Decompressor(object):
def decompress(self, data: bytes, max_length: int = ...) -> bytes: ...
@property
Expand Down
4 changes: 4 additions & 0 deletions stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sys
from _typeshed import Self
from builtins import _dict_items, _dict_keys, _dict_values
from typing import Any, Dict, Generic, NoReturn, Tuple, Type, TypeVar, overload
from typing_extensions import final

if sys.version_info >= (3, 10):
from typing import Callable, Iterable, Iterator, Mapping, MutableMapping, MutableSequence, Reversible, Sequence
Expand Down Expand Up @@ -238,12 +239,15 @@ class Counter(Dict[_T, int], Generic[_T]):
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... # type: ignore

@final
class _OrderedDictKeysView(_dict_keys[_KT_co, _VT_co], Reversible[_KT_co]):
def __reversed__(self) -> Iterator[_KT_co]: ...

@final
class _OrderedDictItemsView(_dict_items[_KT_co, _VT_co], Reversible[Tuple[_KT_co, _VT_co]]):
def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...

@final
class _OrderedDictValuesView(_dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]):
def __reversed__(self) -> Iterator[_VT_co]: ...

Expand Down
2 changes: 2 additions & 0 deletions stdlib/datetime.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from time import struct_time
from typing import ClassVar, NamedTuple, SupportsAbs, Type, TypeVar, overload
from typing_extensions import final

_S = TypeVar("_S")

Expand All @@ -16,6 +17,7 @@ class tzinfo:
# Alias required to avoid name conflicts with date(time).tzinfo.
_tzinfo = tzinfo

@final
class timezone(tzinfo):
utc: ClassVar[timezone]
min: ClassVar[timezone]
Expand Down
3 changes: 2 additions & 1 deletion stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
import types
from _typeshed import SupportsItems, SupportsLessThan
from typing import Any, Callable, Generic, Hashable, Iterable, NamedTuple, Sequence, Set, Sized, Tuple, Type, TypeVar, overload
from typing_extensions import ParamSpec
from typing_extensions import ParamSpec, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -24,6 +24,7 @@ class _CacheInfo(NamedTuple):
maxsize: int
currsize: int

@final
class _lru_cache_wrapper(Generic[_P, _T]): # type: ignore
__wrapped__: Callable[_P, _T] # type: ignore
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ... # type: ignore
Expand Down
4 changes: 3 additions & 1 deletion stdlib/lzma.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
from _typeshed import ReadableBuffer, Self, StrOrBytesPath
from typing import IO, Any, Mapping, Sequence, TextIO, Union, overload
from typing_extensions import Literal
from typing_extensions import Literal, final

_OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"]
_OpenTextWritingMode = Literal["wt", "xt", "at"]
Expand Down Expand Up @@ -40,6 +40,7 @@ PRESET_DEFAULT: int
PRESET_EXTREME: int

# from _lzma.c
@final
class LZMADecompressor(object):
def __init__(self, format: int | None = ..., memlimit: int | None = ..., filters: _FilterChain | None = ...) -> None: ...
def decompress(self, data: bytes, max_length: int = ...) -> bytes: ...
Expand All @@ -53,6 +54,7 @@ class LZMADecompressor(object):
def needs_input(self) -> bool: ...

# from _lzma.c
@final
class LZMACompressor(object):
def __init__(
self, format: int | None = ..., check: int = ..., preset: int | None = ..., filters: _FilterChain | None = ...
Expand Down
5 changes: 4 additions & 1 deletion stdlib/operator.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from typing import (
TypeVar,
overload,
)
from typing_extensions import final

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
Expand Down Expand Up @@ -115,7 +116,7 @@ def __setitem__(a: MutableSequence[_T], b: slice, c: Sequence[_T]) -> None: ...
@overload
def __setitem__(a: MutableMapping[_K, _V], b: _K, c: _V) -> None: ...
def length_hint(__obj: Any, __default: int = ...) -> int: ...

@final
class attrgetter(Generic[_T_co]):
@overload
def __new__(cls, attr: str) -> attrgetter[Any]: ...
Expand All @@ -129,6 +130,7 @@ class attrgetter(Generic[_T_co]):
def __new__(cls, attr: str, *attrs: str) -> attrgetter[Tuple[Any, ...]]: ...
def __call__(self, obj: Any) -> _T_co: ...

@final
class itemgetter(Generic[_T_co]):
@overload
def __new__(cls, item: Any) -> itemgetter[Any]: ...
Expand All @@ -142,6 +144,7 @@ class itemgetter(Generic[_T_co]):
def __new__(cls, item: Any, *items: Any) -> itemgetter[Tuple[Any, ...]]: ...
def __call__(self, obj: Any) -> _T_co: ...

@final
class methodcaller:
def __init__(self, __name: str, *args: Any, **kwargs: Any) -> None: ...
def __call__(self, obj: Any) -> Any: ...
Expand Down
6 changes: 5 additions & 1 deletion stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ from typing import (
overload,
runtime_checkable,
)
from typing_extensions import Literal
from typing_extensions import Literal, final

from . import path as _path

Expand Down Expand Up @@ -262,6 +262,7 @@ if sys.platform != "win32":
TMP_MAX: int # Undocumented, but used by tempfile

# ----- os classes (structures) -----
@final
class stat_result:
# For backward compatibility, the return value of stat() is also
# accessible as a tuple of at least 10 integers giving the most important
Expand Down Expand Up @@ -314,6 +315,7 @@ class PathLike(Protocol[_AnyStr_co]):

_FdOrAnyPath = Union[int, StrOrBytesPath]

@final
class DirEntry(Generic[AnyStr]):
# This is what the scandir iterator yields
# The constructor is hidden
Expand All @@ -334,6 +336,7 @@ if sys.platform != "win32":
_Tuple11Int = Tuple[int, int, int, int, int, int, int, int, int, int, int]
if sys.version_info >= (3, 7):
# f_fsid was added in https://github.com/python/cpython/pull/4571
@final
class statvfs_result(_Tuple10Int): # Unix only
def __new__(cls, seq: _Tuple10Int | _Tuple11Int, dict: dict[str, int] = ...) -> statvfs_result: ...
n_fields: int
Expand Down Expand Up @@ -566,6 +569,7 @@ if sys.platform != "win32":
def readv(__fd: int, __buffers: Sequence[bytearray]) -> int: ...
def writev(__fd: int, __buffers: Sequence[bytes]) -> int: ...

@final
class terminal_size(Tuple[int, int]):
columns: int
lines: int
Expand Down
2 changes: 2 additions & 0 deletions stdlib/pickle.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from typing import Any, Callable, ClassVar, Iterable, Iterator, Mapping, Optional, Protocol, Tuple, Type, Union
from typing_extensions import final

HIGHEST_PROTOCOL: int
DEFAULT_PROTOCOL: int
Expand All @@ -15,6 +16,7 @@ class _WritableFileobj(Protocol):

if sys.version_info >= (3, 8):
# TODO: holistic design for buffer interface (typing.Buffer?)
@final
class PickleBuffer:
# buffer must be a buffer-providing object
def __init__(self, buffer: Any) -> None: ...
Expand Down
3 changes: 3 additions & 0 deletions stdlib/posix.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import sys
from _typeshed import StrOrBytesPath
from os import PathLike, _ExecEnv, _ExecVArgs, stat_result as stat_result
from typing import Any, Iterable, NamedTuple, Sequence, Tuple, overload
from typing_extensions import final

@final
class uname_result(NamedTuple):
sysname: str
nodename: str
release: str
version: str
machine: str

@final
class times_result(NamedTuple):
user: float
system: float
Expand Down
2 changes: 2 additions & 0 deletions stdlib/pyexpat/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import pyexpat.errors as errors
import pyexpat.model as model
from _typeshed import SupportsRead
from typing import Any, Callable, Optional, Tuple
from typing_extensions import final

EXPAT_VERSION: str # undocumented
version_info: tuple[int, int, int] # undocumented
Expand All @@ -21,6 +22,7 @@ XML_PARAM_ENTITY_PARSING_ALWAYS: int

_Model = Tuple[int, int, Optional[str], Tuple[Any, ...]]

@final
class XMLParserType(object):
def Parse(self, __data: str | bytes, __isfinal: bool = ...) -> int: ...
def ParseFile(self, __file: SupportsRead[bytes]) -> int: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/time.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from types import SimpleNamespace
from typing import Any, NamedTuple, Tuple
from typing_extensions import final

_TimeTuple = Tuple[int, int, int, int, int, int, int, int, int]

Expand Down Expand Up @@ -48,6 +49,7 @@ class _struct_time(NamedTuple):
@property
def n_unnamed_fields(self) -> int: ...

@final
class struct_time(_struct_time):
def __init__(
self,
Expand Down
4 changes: 3 additions & 1 deletion stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986
import sys
from abc import ABCMeta, abstractmethod
from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType
from typing_extensions import Literal as _Literal, ParamSpec as _ParamSpec
from typing_extensions import Literal as _Literal, ParamSpec as _ParamSpec, final as _final

if sys.version_info >= (3, 7):
from types import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType
Expand Down Expand Up @@ -545,6 +545,7 @@ class TextIO(IO[str]):

class ByteString(Sequence[int], metaclass=ABCMeta): ...

@_final
class Match(Generic[AnyStr]):
pos: int
endpos: int
Expand Down Expand Up @@ -588,6 +589,7 @@ class Match(Generic[AnyStr]):
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

@_final
class Pattern(Generic[AnyStr]):
flags: int
groupindex: Mapping[str, int]
Expand Down
2 changes: 2 additions & 0 deletions stdlib/winreg.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _typeshed import Self
from types import TracebackType
from typing import Any, Type, Union
from typing_extensions import final

_KeyType = Union[HKEYType, int]

Expand Down Expand Up @@ -88,6 +89,7 @@ REG_WHOLE_HIVE_VOLATILE: int # undocumented
error = OSError

# Though this class has a __name__ of PyHKEY, it's exposed as HKEYType for some reason
@final
class HKEYType:
def __bool__(self) -> bool: ...
def __int__(self) -> int: ...
Expand Down