Skip to content

Stdlib: add many missing __hash__ and __eq__ methods #10464

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 4 commits into from
Jul 17, 2023
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
2 changes: 2 additions & 0 deletions stdlib/_collections_abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ _VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.

@final
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 10):
@property
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
Expand All @@ -81,6 +82,7 @@ class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented

@final
class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocumented
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 10):
@property
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
Expand Down
3 changes: 3 additions & 0 deletions stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ _T = TypeVar("_T")

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

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

class ReferenceType(Generic[_T]):
__callback__: Callable[[ReferenceType[_T]], Any]
def __new__(cls, __o: _T, __callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
def __call__(self) -> _T | None: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class array(MutableSequence[_T], Generic[_T]):
def __setitem__(self, __key: slice, __value: array[_T]) -> None: ...
def __delitem__(self, __key: SupportsIndex | slice) -> None: ...
def __add__(self, __value: array[_T]) -> array[_T]: ...
def __eq__(self, __value: object) -> bool: ...
def __ge__(self, __value: array[_T]) -> bool: ...
def __gt__(self, __value: array[_T]) -> bool: ...
def __iadd__(self, __value: array[_T]) -> Self: ... # type: ignore[override]
Expand Down
12 changes: 12 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ class memoryview(Sequence[int]):
def __contains__(self, __x: object) -> bool: ...
def __iter__(self) -> Iterator[int]: ...
def __len__(self) -> int: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
@overload
def __setitem__(self, __key: slice, __value: ReadableBuffer) -> None: ...
@overload
Expand Down Expand Up @@ -941,6 +943,7 @@ class slice:
def __init__(self, __stop: Any) -> None: ...
@overload
def __init__(self, __start: Any, __stop: Any, __step: Any = ...) -> None: ...
def __eq__(self, __value: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ...

Expand All @@ -957,6 +960,8 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
def __le__(self, __value: tuple[_T_co, ...]) -> bool: ...
def __gt__(self, __value: tuple[_T_co, ...]) -> bool: ...
def __ge__(self, __value: tuple[_T_co, ...]) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
@overload
def __add__(self, __value: tuple[_T_co, ...]) -> tuple[_T_co, ...]: ...
@overload
Expand Down Expand Up @@ -1045,6 +1050,7 @@ class list(MutableSequence[_T], Generic[_T]):
def __ge__(self, __value: list[_T]) -> bool: ...
def __lt__(self, __value: list[_T]) -> bool: ...
def __le__(self, __value: list[_T]) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

Expand Down Expand Up @@ -1097,6 +1103,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __setitem__(self, __key: _KT, __value: _VT) -> None: ...
def __delitem__(self, __key: _KT) -> None: ...
def __iter__(self) -> Iterator[_KT]: ...
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 8):
def __reversed__(self) -> Iterator[_KT]: ...
__hash__: ClassVar[None] # type: ignore[assignment]
Expand Down Expand Up @@ -1151,6 +1158,7 @@ class set(MutableSet[_T], Generic[_T]):
def __lt__(self, __value: AbstractSet[object]) -> bool: ...
def __ge__(self, __value: AbstractSet[object]) -> bool: ...
def __gt__(self, __value: AbstractSet[object]) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
Expand Down Expand Up @@ -1179,6 +1187,8 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]):
def __lt__(self, __value: AbstractSet[object]) -> bool: ...
def __ge__(self, __value: AbstractSet[object]) -> bool: ...
def __gt__(self, __value: AbstractSet[object]) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

Expand All @@ -1204,6 +1214,8 @@ class range(Sequence[int]):
def count(self, __value: int) -> int: ...
def index(self, __value: int) -> int: ... # type: ignore[override]
def __len__(self) -> int: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
def __contains__(self, __key: object) -> bool: ...
def __iter__(self) -> Iterator[int]: ...
@overload
Expand Down
3 changes: 3 additions & 0 deletions stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class UserString(Sequence[UserString]):
def __gt__(self, string: str | UserString) -> bool: ...
def __ge__(self, string: str | UserString) -> bool: ...
def __eq__(self, string: object) -> bool: ...
def __hash__(self) -> int: ...
def __contains__(self, char: object) -> bool: ...
def __len__(self) -> int: ...
def __getitem__(self, index: SupportsIndex | slice) -> Self: ...
Expand Down Expand Up @@ -257,6 +258,7 @@ class deque(MutableSequence[_T], Generic[_T]):
def __le__(self, __value: deque[_T]) -> bool: ...
def __gt__(self, __value: deque[_T]) -> bool: ...
def __ge__(self, __value: deque[_T]) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

Expand Down Expand Up @@ -365,6 +367,7 @@ class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def setdefault(self: OrderedDict[_KT, _T | None], key: _KT, default: None = None) -> _T | None: ...
@overload
def setdefault(self, key: _KT, default: _VT) -> _VT: ...
def __eq__(self, __value: object) -> bool: ...

class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]):
default_factory: Callable[[], _VT] | None
Expand Down
2 changes: 2 additions & 0 deletions stdlib/contextvars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ContextVar(Generic[_T]):
def __init__(self, name: str) -> None: ...
@overload
def __init__(self, name: str, *, default: _T) -> None: ...
def __hash__(self) -> int: ...
@property
def name(self) -> str: ...
@overload
Expand Down Expand Up @@ -60,3 +61,4 @@ class Context(Mapping[ContextVar[Any], Any]):
def __getitem__(self, __key: ContextVar[_T]) -> _T: ...
def __iter__(self) -> Iterator[ContextVar[Any]]: ...
def __len__(self) -> int: ...
def __eq__(self, __value: object) -> bool: ...
6 changes: 6 additions & 0 deletions stdlib/datetime.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class timezone(tzinfo):
def utcoffset(self, __dt: datetime | None) -> timedelta: ...
def dst(self, __dt: datetime | None) -> None: ...
def __hash__(self) -> int: ...
def __eq__(self, __value: object) -> bool: ...

if sys.version_info >= (3, 11):
UTC: timezone
Expand Down Expand Up @@ -87,6 +88,7 @@ class date:
def __lt__(self, __value: date) -> bool: ...
def __ge__(self, __value: date) -> bool: ...
def __gt__(self, __value: date) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
if sys.version_info >= (3, 8):
def __add__(self, __value: timedelta) -> Self: ...
def __radd__(self, __value: timedelta) -> Self: ...
Expand Down Expand Up @@ -145,6 +147,7 @@ class time:
def __lt__(self, __value: time) -> bool: ...
def __ge__(self, __value: time) -> bool: ...
def __gt__(self, __value: time) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
def isoformat(self, timespec: str = ...) -> str: ...
@classmethod
Expand Down Expand Up @@ -219,6 +222,7 @@ class timedelta:
def __lt__(self, __value: timedelta) -> bool: ...
def __ge__(self, __value: timedelta) -> bool: ...
def __gt__(self, __value: timedelta) -> bool: ...
def __eq__(self, __value: object) -> bool: ...
def __bool__(self) -> bool: ...
def __hash__(self) -> int: ...

Expand Down Expand Up @@ -310,6 +314,8 @@ class datetime(date):
def __lt__(self, __value: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, __value: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, __value: datetime) -> bool: ... # type: ignore[override]
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 8):
@overload # type: ignore[override]
def __sub__(self, __value: timedelta) -> Self: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/enum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class Enum(metaclass=EnumMeta):
# and in practice using `object` here has the same effect as using `Any`.
def __new__(cls, value: object) -> Self: ...
def __dir__(self) -> list[str]: ...
def __hash__(self) -> int: ...
def __format__(self, format_spec: str) -> str: ...
def __reduce_ex__(self, proto: Unused) -> tuple[Any, ...]: ...

Expand Down
1 change: 1 addition & 0 deletions stdlib/importlib/machinery.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,4 @@ class ExtensionFileLoader(importlib.abc.ExecutionLoader):
def exec_module(self, module: types.ModuleType) -> None: ...
def get_code(self, fullname: str) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
3 changes: 3 additions & 0 deletions stdlib/importlib/metadata/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class EntryPoint(_EntryPointBase):
extras: list[str] = ...,
) -> bool: ... # undocumented

def __hash__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...

if sys.version_info >= (3, 10):
class EntryPoints(list[EntryPoint]): # use as list is deprecated since 3.10
# int argument is deprecated since 3.10
Expand Down
2 changes: 2 additions & 0 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class Signature:
def from_callable(cls, obj: _IntrospectableCallable, *, follow_wrapped: bool = True) -> Self: ...

def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

if sys.version_info >= (3, 10):
def get_annotations(
Expand Down Expand Up @@ -413,6 +414,7 @@ class Parameter:
annotation: Any = ...,
) -> Self: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

class BoundArguments:
arguments: OrderedDict[str, Any]
Expand Down
13 changes: 11 additions & 2 deletions stdlib/ipaddress.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class _BaseNetwork(_IPAddressBase, Container[_A], Iterable[_A], Generic[_A]):
def __getitem__(self, n: int) -> _A: ...
def __iter__(self) -> Iterator[_A]: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
def __lt__(self, other: Self) -> bool: ...
if sys.version_info >= (3, 11):
def __ge__(self, other: Self) -> bool: ...
Expand Down Expand Up @@ -148,7 +149,10 @@ class _BaseV4:

class IPv4Address(_BaseV4, _BaseAddress): ...
class IPv4Network(_BaseV4, _BaseNetwork[IPv4Address]): ...
class IPv4Interface(IPv4Address, _BaseInterface[IPv4Address, IPv4Network]): ...

class IPv4Interface(IPv4Address, _BaseInterface[IPv4Address, IPv4Network]):
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

class _BaseV6:
@property
Expand All @@ -169,11 +173,16 @@ class IPv6Address(_BaseV6, _BaseAddress):
@property
def scope_id(self) -> str | None: ...

def __hash__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...

class IPv6Network(_BaseV6, _BaseNetwork[IPv6Address]):
@property
def is_site_local(self) -> bool: ...

class IPv6Interface(IPv6Address, _BaseInterface[IPv6Address, IPv6Network]): ...
class IPv6Interface(IPv6Address, _BaseInterface[IPv6Address, IPv6Network]):
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

def v4_int_to_packed(address: int) -> bytes: ...
def v6_int_to_packed(address: int) -> bytes: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/re.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class Pattern(Generic[AnyStr]):
def subn(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ...
def __copy__(self) -> Pattern[AnyStr]: ...
def __deepcopy__(self, __memo: Any) -> Pattern[AnyStr]: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

Expand Down
1 change: 1 addition & 0 deletions stdlib/ssl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class SSLSession:
def time(self) -> int: ...
@property
def timeout(self) -> int: ...
def __eq__(self, __value: object) -> bool: ...

class SSLErrorNumber(enum.IntEnum):
SSL_ERROR_EOF: int
Expand Down
5 changes: 5 additions & 0 deletions stdlib/tracemalloc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Statistic:
traceback: Traceback
def __init__(self, traceback: Traceback, size: int, count: int) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

class StatisticDiff:
count: int
Expand All @@ -46,6 +47,7 @@ class StatisticDiff:
traceback: Traceback
def __init__(self, traceback: Traceback, size: int, size_diff: int, count: int, count_diff: int) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

_FrameTuple: TypeAlias = tuple[str, int]

Expand All @@ -56,6 +58,7 @@ class Frame:
def lineno(self) -> int: ...
def __init__(self, frame: _FrameTuple) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
def __lt__(self, other: Frame) -> bool: ...
if sys.version_info >= (3, 11):
def __gt__(self, other: Frame) -> bool: ...
Expand All @@ -80,6 +83,7 @@ class Trace:
def traceback(self) -> Traceback: ...
def __init__(self, trace: _TraceTuple) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

class Traceback(Sequence[Frame]):
if sys.version_info >= (3, 9):
Expand All @@ -97,6 +101,7 @@ class Traceback(Sequence[Frame]):
def __contains__(self, frame: Frame) -> bool: ... # type: ignore[override]
def __len__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
def __lt__(self, other: Traceback) -> bool: ...
if sys.version_info >= (3, 11):
def __gt__(self, other: Traceback) -> bool: ...
Expand Down
13 changes: 13 additions & 0 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class _Cell:
if sys.version_info >= (3, 8):
def __init__(self, __contents: object = ...) -> None: ...

def __eq__(self, __value: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
cell_contents: Any

Expand Down Expand Up @@ -113,6 +114,8 @@ LambdaType = FunctionType

@final
class CodeType:
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
@property
def co_argcount(self) -> int: ...
if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -326,6 +329,7 @@ class MappingProxyType(Mapping[_KT, _VT_co], Generic[_KT, _VT_co]):
class SimpleNamespace:
__hash__: ClassVar[None] # type: ignore[assignment]
def __init__(self, **kwargs: Any) -> None: ...
def __eq__(self, __value: object) -> bool: ...
def __getattribute__(self, __name: str) -> Any: ...
def __setattr__(self, __name: str, __value: Any) -> None: ...
def __delattr__(self, __name: str) -> None: ...
Expand Down Expand Up @@ -442,6 +446,8 @@ class MethodType:
def __qualname__(self) -> str: ... # inherited from the added function
def __init__(self, __func: Callable[..., Any], __obj: object) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...

@final
class BuiltinFunctionType:
Expand All @@ -452,6 +458,8 @@ class BuiltinFunctionType:
@property
def __qualname__(self) -> str: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...

BuiltinMethodType = BuiltinFunctionType

Expand Down Expand Up @@ -479,6 +487,7 @@ class MethodWrapperType:
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __eq__(self, __value: object) -> bool: ...
def __ne__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...

@final
class MethodDescriptorType:
Expand Down Expand Up @@ -603,6 +612,8 @@ if sys.version_info >= (3, 9):
def __parameters__(self) -> tuple[Any, ...]: ...
def __init__(self, origin: type, args: Any) -> None: ...
def __getitem__(self, __typeargs: Any) -> GenericAlias: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 11):
@property
def __unpacked__(self) -> bool: ...
Expand All @@ -626,3 +637,5 @@ if sys.version_info >= (3, 10):
def __args__(self) -> tuple[Any, ...]: ...
def __or__(self, __value: Any) -> UnionType: ...
def __ror__(self, __value: Any) -> UnionType: ...
def __eq__(self, __value: object) -> bool: ...
def __hash__(self) -> int: ...
Loading