Skip to content

Commit

Permalink
Add __hash__ for a bunch of types that set it to None (#13286)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungol authored Dec 24, 2024
1 parent 1f0a86c commit 3944c78
Show file tree
Hide file tree
Showing 32 changed files with 69 additions and 27 deletions.
3 changes: 3 additions & 0 deletions stdlib/_collections_abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from typing import ( # noqa: Y022,Y038
AsyncIterator as AsyncIterator,
Awaitable as Awaitable,
Callable as Callable,
ClassVar,
Collection as Collection,
Container as Container,
Coroutine as Coroutine,
Expand Down Expand Up @@ -74,6 +75,7 @@ _VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
def __eq__(self, value: object, /) -> bool: ...
def __reversed__(self) -> Iterator[_KT_co]: ...
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 13):
def isdisjoint(self, other: Iterable[_KT_co], /) -> bool: ...
if sys.version_info >= (3, 10):
Expand All @@ -91,6 +93,7 @@ class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented
class dict_items(ItemsView[_KT_co, _VT_co]): # undocumented
def __eq__(self, value: object, /) -> bool: ...
def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 13):
def isdisjoint(self, other: Iterable[tuple[_KT_co, _VT_co]], /) -> bool: ...
if sys.version_info >= (3, 10):
Expand Down
2 changes: 2 additions & 0 deletions stdlib/_contextvars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Token(Generic[_T]):
@property
def old_value(self) -> Any: ... # returns either _T or MISSING, but that's hard to express
MISSING: ClassVar[object]
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...

Expand All @@ -55,6 +56,7 @@ class Context(Mapping[ContextVar[Any], Any]):
def get(self, key: ContextVar[_T], default: _D, /) -> _T | _D: ...
def run(self, callable: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: ...
def copy(self) -> Context: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __getitem__(self, key: ContextVar[_T], /) -> _T: ...
def __iter__(self) -> Iterator[ContextVar[Any]]: ...
def __len__(self) -> int: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/_frozen_importlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import types
from _typeshed.importlib import LoaderProtocol
from collections.abc import Mapping, Sequence
from types import ModuleType
from typing import Any
from typing import Any, ClassVar

# Signature of `builtins.__import__` should be kept identical to `importlib.__import__`
def __import__(
Expand Down Expand Up @@ -43,6 +43,7 @@ class ModuleSpec:
def parent(self) -> str | None: ...
has_location: bool
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]

class BuiltinImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
# MetaPathFinder
Expand Down
3 changes: 2 additions & 1 deletion stdlib/_ssl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from ssl import (
SSLWantWriteError as SSLWantWriteError,
SSLZeroReturnError as SSLZeroReturnError,
)
from typing import Any, Literal, TypedDict, final, overload
from typing import Any, ClassVar, Literal, TypedDict, final, overload
from typing_extensions import NotRequired, Self, TypeAlias

_PasswordType: TypeAlias = Callable[[], str | bytes | bytearray] | str | bytes | bytearray
Expand Down Expand Up @@ -119,6 +119,7 @@ class MemoryBIO:

@final
class SSLSession:
__hash__: ClassVar[None] # type: ignore[assignment]
@property
def has_ticket(self) -> bool: ...
@property
Expand Down
3 changes: 2 additions & 1 deletion stdlib/_weakrefset.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from collections.abc import Iterable, Iterator, MutableSet
from typing import Any, TypeVar, overload
from typing import Any, ClassVar, TypeVar, overload
from typing_extensions import Self

if sys.version_info >= (3, 9):
Expand All @@ -21,6 +21,7 @@ class WeakSet(MutableSet[_T]):
def copy(self) -> Self: ...
def remove(self, item: _T) -> None: ...
def update(self, other: Iterable[_T]) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __contains__(self, item: object) -> bool: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[_T]: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/argparse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
from _typeshed import sentinel
from collections.abc import Callable, Generator, Iterable, Sequence
from re import Pattern
from typing import IO, Any, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
from typing_extensions import Self, TypeAlias, deprecated

__all__ = [
Expand Down Expand Up @@ -456,6 +456,7 @@ class Namespace(_AttributeHolder):
def __setattr__(self, name: str, value: Any, /) -> None: ...
def __contains__(self, key: str) -> bool: ...
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]

class FileType:
# undocumented
Expand Down
3 changes: 2 additions & 1 deletion stdlib/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
from collections.abc import Iterable

# pytype crashes if array inherits from collections.abc.MutableSequence instead of typing.MutableSequence
from typing import Any, Literal, MutableSequence, SupportsIndex, TypeVar, overload # noqa: Y022
from typing import Any, ClassVar, Literal, MutableSequence, SupportsIndex, TypeVar, overload # noqa: Y022
from typing_extensions import Self, TypeAlias

if sys.version_info >= (3, 12):
Expand Down Expand Up @@ -64,6 +64,7 @@ class array(MutableSequence[_T]):
def fromstring(self, buffer: str | ReadableBuffer, /) -> None: ...
def tostring(self) -> bytes: ...

__hash__: ClassVar[None] # type: ignore[assignment]
def __len__(self) -> int: ...
@overload
def __getitem__(self, key: SupportsIndex, /) -> _T: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import SupportsItems, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from typing import Any, Generic, NoReturn, SupportsIndex, TypeVar, final, overload
from typing import Any, ClassVar, Generic, NoReturn, SupportsIndex, TypeVar, final, overload
from typing_extensions import Self

if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -119,6 +119,7 @@ class UserList(MutableSequence[_T]):
def __init__(self, initlist: None = None) -> None: ...
@overload
def __init__(self, initlist: Iterable[_T]) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __lt__(self, other: list[_T] | UserList[_T]) -> bool: ...
def __le__(self, other: list[_T] | UserList[_T]) -> bool: ...
def __gt__(self, other: list[_T] | UserList[_T]) -> bool: ...
Expand Down Expand Up @@ -254,6 +255,7 @@ class deque(MutableSequence[_T]):
def rotate(self, n: int = 1, /) -> None: ...
def __copy__(self) -> Self: ...
def __len__(self) -> int: ...
__hash__: ClassVar[None] # type: ignore[assignment]
# These methods of deque don't take slices, unlike MutableSequence, hence the type: ignores
def __getitem__(self, key: SupportsIndex, /) -> _T: ... # type: ignore[override]
def __setitem__(self, key: SupportsIndex, value: _T, /) -> None: ... # type: ignore[override]
Expand Down
3 changes: 2 additions & 1 deletion stdlib/email/charset.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Callable, Iterator
from email.message import Message
from typing import Final, overload
from typing import ClassVar, Final, overload

__all__ = ["Charset", "add_alias", "add_charset", "add_codec"]

Expand All @@ -24,6 +24,7 @@ class Charset:
def body_encode(self, string: None) -> None: ...
@overload
def body_encode(self, string: str | bytes) -> str: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...
def __ne__(self, value: object, /) -> bool: ...

Expand Down
3 changes: 2 additions & 1 deletion stdlib/email/header.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Iterable
from email.charset import Charset
from typing import Any
from typing import Any, ClassVar

__all__ = ["Header", "decode_header", "make_header"]

Expand All @@ -16,6 +16,7 @@ class Header:
) -> None: ...
def append(self, s: bytes | bytearray | str, charset: Charset | str | None = None, errors: str = "strict") -> None: ...
def encode(self, splitchars: str = ";, \t", maxlinelen: int | None = None, linesep: str = "\n") -> str: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...
def __ne__(self, value: object, /) -> bool: ...

Expand Down
2 changes: 2 additions & 0 deletions stdlib/email/headerregistry.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Address:
def __init__(
self, display_name: str = "", username: str | None = "", domain: str | None = "", addr_spec: str | None = None
) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...

class Group:
Expand All @@ -175,4 +176,5 @@ class Group:
@property
def addresses(self) -> tuple[Address, ...]: ...
def __init__(self, display_name: str | None = None, addresses: Iterable[Address] | None = None) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...
2 changes: 1 addition & 1 deletion stdlib/fractions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Fraction(Rational):
def __round__(self, ndigits: None = None) -> int: ...
@overload
def __round__(self, ndigits: int) -> Fraction: ...
def __hash__(self) -> int: ...
def __hash__(self) -> int: ... # type: ignore[override]
def __eq__(a, b: object) -> bool: ...
def __lt__(a, b: _ComparableNum) -> bool: ...
def __gt__(a, b: _ComparableNum) -> bool: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ class BoundArguments:
def __init__(self, signature: Signature, arguments: OrderedDict[str, Any]) -> None: ...
def apply_defaults(self) -> None: ...
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]

#
# Classes and functions
Expand Down
3 changes: 2 additions & 1 deletion stdlib/lib2to3/pgen2/pgen.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from _typeshed import Incomplete, StrPath
from collections.abc import Iterable, Iterator
from typing import IO, NoReturn, overload
from typing import IO, ClassVar, NoReturn, overload

from . import grammar
from .tokenize import _TokenInfo
Expand Down Expand Up @@ -46,5 +46,6 @@ class DFAState:
def addarc(self, next: DFAState, label: str) -> None: ...
def unifystate(self, old: DFAState, new: DFAState) -> None: ...
def __eq__(self, other: DFAState) -> bool: ... # type: ignore[override]
__hash__: ClassVar[None] # type: ignore[assignment]

def generate_grammar(filename: StrPath = "Grammar.txt") -> PgenGrammar: ...
3 changes: 2 additions & 1 deletion stdlib/lib2to3/pytree.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from _typeshed import Incomplete, SupportsGetItem, SupportsLenAndGetItem, Unused
from abc import abstractmethod
from collections.abc import Iterable, Iterator, MutableSequence
from typing import Final
from typing import ClassVar, Final
from typing_extensions import Self, TypeAlias

from .fixer_base import BaseFix
Expand All @@ -24,6 +24,7 @@ class Base:
was_changed: bool
was_checked: bool
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
@abstractmethod
def _eq(self, other: Base) -> bool: ...
@abstractmethod
Expand Down
3 changes: 2 additions & 1 deletion stdlib/numbers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from _typeshed import Incomplete
from abc import ABCMeta, abstractmethod
from typing import Literal, Protocol, overload
from typing import ClassVar, Literal, Protocol, overload

__all__ = ["Number", "Complex", "Real", "Rational", "Integral"]

Expand Down Expand Up @@ -102,6 +102,7 @@ class Complex(Number, _ComplexLike):
def conjugate(self) -> _ComplexLike: ...
@abstractmethod
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]

# See comment at the top of the file
# for why some of these return types are purposefully vague
Expand Down
3 changes: 2 additions & 1 deletion stdlib/optparse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import builtins
from _typeshed import Incomplete, MaybeNone
from abc import abstractmethod
from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import IO, Any, AnyStr, Literal, NoReturn, overload
from typing import IO, Any, AnyStr, ClassVar, Literal, NoReturn, overload
from typing_extensions import Self

__all__ = [
Expand Down Expand Up @@ -216,6 +216,7 @@ class Values:
def ensure_value(self, attr: str, value): ...
def read_file(self, filename: str, mode: str = "careful") -> None: ...
def read_module(self, modname: str, mode: str = "careful") -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
# __getattr__ doesn't exist, but anything passed as a default to __init__
# is set on the instance.
def __getattr__(self, name: str): ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/parser.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from _typeshed import StrOrBytesPath
from collections.abc import Sequence
from types import CodeType
from typing import Any, final
from typing import Any, ClassVar, final

def expr(source: str) -> STType: ...
def suite(source: str) -> STType: ...
Expand All @@ -17,6 +17,7 @@ class ParserError(Exception): ...

@final
class STType:
__hash__: ClassVar[None] # type: ignore[assignment]
def compile(self, filename: StrOrBytesPath = ...) -> CodeType: ...
def isexpr(self) -> bool: ...
def issuite(self) -> bool: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/plistlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from _typeshed import ReadableBuffer
from collections.abc import Mapping, MutableMapping
from datetime import datetime
from enum import Enum
from typing import IO, Any
from typing import IO, Any, ClassVar
from typing_extensions import Self

__all__ = ["InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID"]
Expand Down Expand Up @@ -100,6 +100,7 @@ if sys.version_info < (3, 9):
class Data:
data: bytes
def __init__(self, data: bytes) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]

class UID:
data: int
Expand Down
5 changes: 3 additions & 2 deletions stdlib/sched.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from collections.abc import Callable
from typing import Any, NamedTuple, type_check_only
from typing import Any, ClassVar, NamedTuple, type_check_only
from typing_extensions import TypeAlias

__all__ = ["scheduler"]
Expand All @@ -25,7 +25,8 @@ else:
argument: tuple[Any, ...]
kwargs: dict[str, Any]

class Event(_EventBase): ...
class Event(_EventBase):
__hash__: ClassVar[None] # type: ignore[assignment]

class scheduler:
timefunc: Callable[[], float]
Expand Down
3 changes: 2 additions & 1 deletion stdlib/select.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
from _typeshed import FileDescriptorLike
from collections.abc import Iterable
from types import TracebackType
from typing import Any, final
from typing import Any, ClassVar, final
from typing_extensions import Self

if sys.platform != "win32":
Expand Down Expand Up @@ -53,6 +53,7 @@ if sys.platform != "linux" and sys.platform != "win32":
data: Any = ...,
udata: Any = ...,
) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]

# BSD only
@final
Expand Down
3 changes: 2 additions & 1 deletion stdlib/tkinter/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from collections.abc import Callable, Iterable, Mapping, Sequence
from tkinter.constants import *
from tkinter.font import _FontDescription
from types import TracebackType
from typing import Any, Generic, Literal, NamedTuple, Protocol, TypedDict, TypeVar, overload, type_check_only
from typing import Any, ClassVar, Generic, Literal, NamedTuple, Protocol, TypedDict, TypeVar, overload, type_check_only
from typing_extensions import TypeAlias, TypeVarTuple, Unpack, deprecated

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -330,6 +330,7 @@ class Variable:
def trace_vinfo(self): ...
def __eq__(self, other: object) -> bool: ...
def __del__(self) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]

class StringVar(Variable):
def __init__(self, master: Misc | None = None, value: str | None = None, name: str | None = None) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/tkinter/font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Font:
underline: bool = ...,
overstrike: bool = ...,
) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __setitem__(self, key: str, value: Any) -> None: ...
@overload
def cget(self, option: Literal["family"]) -> str: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/traceback.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sys
from _typeshed import SupportsWrite, Unused
from collections.abc import Generator, Iterable, Iterator, Mapping
from types import FrameType, TracebackType
from typing import Any, Literal, overload
from typing import Any, ClassVar, Literal, overload
from typing_extensions import Self, TypeAlias, deprecated

__all__ = [
Expand Down Expand Up @@ -229,6 +229,7 @@ class TracebackException:
) -> Self: ...

def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 11):
def format(self, *, chain: bool = True, _ctx: _ExceptionPrintContext | None = None) -> Generator[str, None, None]: ...
else:
Expand Down Expand Up @@ -292,6 +293,7 @@ class FrameSummary:
def __iter__(self) -> Iterator[Any]: ...
def __eq__(self, other: object) -> bool: ...
def __len__(self) -> Literal[4]: ...
__hash__: ClassVar[None] # type: ignore[assignment]

class StackSummary(list[FrameSummary]):
@classmethod
Expand Down
Loading

0 comments on commit 3944c78

Please sign in to comment.