From b4e97a190930efc97f92fec8167ac3cc99858326 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 2 Feb 2022 21:14:59 +0000 Subject: [PATCH] Improve `__enter__` & constructor methods (#7114) --- stubs/Pillow/PIL/Image.pyi | 4 ++-- .../aws_xray_sdk/core/models/trace_header.pyi | 3 ++- stubs/click-spinner/click_spinner/__init__.pyi | 3 ++- .../cryptography/hazmat/primitives/asymmetric/ec.pyi | 3 ++- stubs/cryptography/cryptography/x509/__init__.pyi | 11 ++++++----- stubs/dateparser/dateparser/conf.pyi | 3 ++- .../google/cloud/ndb/global_cache.pyi | 7 +++++-- stubs/google-cloud-ndb/google/cloud/ndb/model.pyi | 8 +++++--- stubs/mock/mock/mock.pyi | 12 +++++++++--- stubs/redis/redis/connection.pyi | 3 ++- stubs/urllib3/urllib3/response.pyi | 3 ++- 11 files changed, 39 insertions(+), 21 deletions(-) diff --git a/stubs/Pillow/PIL/Image.pyi b/stubs/Pillow/PIL/Image.pyi index 05d9b767f..d084d65ec 100644 --- a/stubs/Pillow/PIL/Image.pyi +++ b/stubs/Pillow/PIL/Image.pyi @@ -1,4 +1,4 @@ -from _typeshed import SupportsRead, SupportsWrite +from _typeshed import Self, SupportsRead, SupportsWrite from collections.abc import Iterable, Iterator, MutableMapping from pathlib import Path from typing import Any, Callable, ClassVar, Protocol, Sequence, SupportsBytes, Union @@ -118,7 +118,7 @@ class Image: def height(self) -> int: ... @property def size(self) -> tuple[int, int]: ... - def __enter__(self) -> Image: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... def close(self) -> None: ... def __eq__(self, other: object) -> bool: ... diff --git a/stubs/aws-xray-sdk/aws_xray_sdk/core/models/trace_header.pyi b/stubs/aws-xray-sdk/aws_xray_sdk/core/models/trace_header.pyi index 490baa42d..ba1d9b06a 100644 --- a/stubs/aws-xray-sdk/aws_xray_sdk/core/models/trace_header.pyi +++ b/stubs/aws-xray-sdk/aws_xray_sdk/core/models/trace_header.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from typing import Any log: Any @@ -12,7 +13,7 @@ class TraceHeader: self, root: str | None = ..., parent: str | None = ..., sampled: bool | None = ..., data: dict[str, Any] | None = ... ) -> None: ... @classmethod - def from_header_str(cls, header): ... + def from_header_str(cls: type[Self], header) -> Self: ... def to_header_str(self): ... @property def root(self): ... diff --git a/stubs/click-spinner/click_spinner/__init__.pyi b/stubs/click-spinner/click_spinner/__init__.pyi index 8abc4c7f7..d16f3f73f 100644 --- a/stubs/click-spinner/click_spinner/__init__.pyi +++ b/stubs/click-spinner/click_spinner/__init__.pyi @@ -1,4 +1,5 @@ import threading +from _typeshed import Self from types import TracebackType from typing import Iterator, Protocol from typing_extensions import Literal @@ -22,7 +23,7 @@ class Spinner(object): def start(self) -> None: ... def stop(self) -> None: ... def init_spin(self) -> None: ... - def __enter__(self) -> Spinner: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None ) -> Literal[False]: ... diff --git a/stubs/cryptography/cryptography/hazmat/primitives/asymmetric/ec.pyi b/stubs/cryptography/cryptography/hazmat/primitives/asymmetric/ec.pyi index 33fb4cb9a..a04af595f 100644 --- a/stubs/cryptography/cryptography/hazmat/primitives/asymmetric/ec.pyi +++ b/stubs/cryptography/cryptography/hazmat/primitives/asymmetric/ec.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from abc import ABCMeta, abstractmethod from typing import ClassVar @@ -174,7 +175,7 @@ class EllipticCurvePublicNumbers(object): def y(self) -> int: ... def __init__(self, x: int, y: int, curve: EllipticCurve) -> None: ... @classmethod - def from_encoded_point(cls, curve: EllipticCurve, data: bytes) -> EllipticCurvePublicNumbers: ... + def from_encoded_point(cls: type[Self], curve: EllipticCurve, data: bytes) -> Self: ... def public_key(self, backend: EllipticCurveBackend | None = ...) -> EllipticCurvePublicKey: ... class EllipticCurveSignatureAlgorithm(metaclass=ABCMeta): diff --git a/stubs/cryptography/cryptography/x509/__init__.pyi b/stubs/cryptography/cryptography/x509/__init__.pyi index 97875e1f1..e9db89a0f 100644 --- a/stubs/cryptography/cryptography/x509/__init__.pyi +++ b/stubs/cryptography/cryptography/x509/__init__.pyi @@ -1,4 +1,5 @@ import datetime +from _typeshed import Self from abc import ABCMeta, abstractmethod from enum import Enum from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network @@ -325,10 +326,10 @@ class AuthorityKeyIdentifier(ExtensionType): ) -> None: ... @classmethod def from_issuer_public_key( - cls, public_key: RSAPublicKey | DSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey - ) -> AuthorityKeyIdentifier: ... + cls: type[Self], public_key: RSAPublicKey | DSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey + ) -> Self: ... @classmethod - def from_issuer_subject_key_identifier(cls, ski: SubjectKeyIdentifier) -> AuthorityKeyIdentifier: ... + def from_issuer_subject_key_identifier(cls: type[Self], ski: SubjectKeyIdentifier) -> Self: ... class SubjectKeyIdentifier(ExtensionType): @property @@ -336,8 +337,8 @@ class SubjectKeyIdentifier(ExtensionType): def __init__(self, digest: bytes) -> None: ... @classmethod def from_public_key( - cls, public_key: RSAPublicKey | DSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey - ) -> SubjectKeyIdentifier: ... + cls: type[Self], public_key: RSAPublicKey | DSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey + ) -> Self: ... class AccessDescription: @property diff --git a/stubs/dateparser/dateparser/conf.pyi b/stubs/dateparser/dateparser/conf.pyi index 30d86dcff..7b816d9d6 100644 --- a/stubs/dateparser/dateparser/conf.pyi +++ b/stubs/dateparser/dateparser/conf.pyi @@ -1,7 +1,8 @@ +from _typeshed import Self from typing import Any class Settings: - def __new__(cls, *args, **kw) -> Settings: ... + def __new__(cls: type[Self], *args, **kw) -> Self: ... def __init__(self, settings: Any | None = ...) -> None: ... @classmethod def get_key(cls, settings: Any | None = ...): ... diff --git a/stubs/google-cloud-ndb/google/cloud/ndb/global_cache.pyi b/stubs/google-cloud-ndb/google/cloud/ndb/global_cache.pyi index bcc4aa155..48aeaeb14 100644 --- a/stubs/google-cloud-ndb/google/cloud/ndb/global_cache.pyi +++ b/stubs/google-cloud-ndb/google/cloud/ndb/global_cache.pyi @@ -1,4 +1,5 @@ import abc +from _typeshed import Self from typing import Any ConnectionError: Any @@ -37,7 +38,7 @@ class _InProcessGlobalCache(GlobalCache): class RedisCache(GlobalCache): transient_errors: Any @classmethod - def from_environment(cls, strict_read: bool = ..., strict_write: bool = ...): ... + def from_environment(cls: type[Self], strict_read: bool = ..., strict_write: bool = ...) -> Self: ... redis: Any strict_read: Any strict_write: Any @@ -59,7 +60,9 @@ class MemcacheCache(GlobalCache): def __eq__(self, other): ... transient_errors: Any @classmethod - def from_environment(cls, max_pool_size: int = ..., strict_read: bool = ..., strict_write: bool = ...): ... + def from_environment( + cls: type[Self], max_pool_size: int = ..., strict_read: bool = ..., strict_write: bool = ... + ) -> Self: ... client: Any strict_read: Any strict_write: Any diff --git a/stubs/google-cloud-ndb/google/cloud/ndb/model.pyi b/stubs/google-cloud-ndb/google/cloud/ndb/model.pyi index 48093d106..fda448231 100644 --- a/stubs/google-cloud-ndb/google/cloud/ndb/model.pyi +++ b/stubs/google-cloud-ndb/google/cloud/ndb/model.pyi @@ -1,4 +1,5 @@ import datetime +from _typeshed import Self from collections.abc import Iterable, Sequence from typing import Callable, NoReturn from typing_extensions import Literal @@ -26,7 +27,7 @@ class _NotEqualMixin: DirectionT = Literal["asc", "desc"] class IndexProperty(_NotEqualMixin): - def __new__(cls, name: str, direction: DirectionT) -> IndexProperty: ... + def __new__(cls: type[Self], name: str, direction: DirectionT) -> Self: ... @property def name(self) -> str: ... @property @@ -35,7 +36,7 @@ class IndexProperty(_NotEqualMixin): def __hash__(self) -> int: ... class Index(_NotEqualMixin): - def __new__(cls, kind: str, properties: list[IndexProperty], ancestor: bool) -> Index: ... + def __new__(cls: type[Self], kind: str, properties: list[IndexProperty], ancestor: bool) -> Self: ... @property def kind(self) -> str: ... @property @@ -57,7 +58,8 @@ class IndexState(_NotEqualMixin): def __hash__(self) -> int: ... class ModelAdapter: - def __new__(cls, *args, **kwargs) -> ModelAdapter: ... + # This actually returns NoReturn, but mypy can't handle that + def __new__(cls: type[Self], *args, **kwargs) -> Self: ... def make_connection(*args, **kwargs) -> NoReturn: ... diff --git a/stubs/mock/mock/mock.pyi b/stubs/mock/mock/mock.pyi index e18382b16..3d6c12253 100644 --- a/stubs/mock/mock/mock.pyi +++ b/stubs/mock/mock/mock.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from collections.abc import Callable, Mapping, Sequence from typing import Any, Generic, TypeVar, overload from typing_extensions import Literal @@ -33,8 +34,13 @@ DEFAULT: Any class _Call(tuple[Any, ...]): def __new__( - cls, value: Any = ..., name: Any | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ... - ) -> Any: ... + cls: type[Self], + value: Any = ..., + name: Any | None = ..., + parent: Any | None = ..., + two: bool = ..., + from_kall: bool = ..., + ) -> Self: ... name: Any parent: Any from_kall: Any @@ -60,7 +66,7 @@ class Base: def __init__(self, *args: Any, **kwargs: Any) -> None: ... class NonCallableMock(Base, Any): - def __new__(__cls, *args: Any, **kw: Any) -> NonCallableMock: ... + def __new__(__cls: type[Self], *args: Any, **kw: Any) -> Self: ... def __init__( self, spec: list[str] | object | type[object] | None = ..., diff --git a/stubs/redis/redis/connection.pyi b/stubs/redis/redis/connection.pyi index 0b30e20aa..b8e466f8c 100644 --- a/stubs/redis/redis/connection.pyi +++ b/stubs/redis/redis/connection.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from typing import Any, Mapping from .retry import Retry @@ -178,7 +179,7 @@ class UnixDomainSocketConnection(Connection): class ConnectionPool: @classmethod - def from_url(cls, url: str, *, db: int = ..., decode_components: bool = ..., **kwargs) -> ConnectionPool: ... + def from_url(cls: type[Self], url: str, *, db: int = ..., decode_components: bool = ..., **kwargs) -> Self: ... connection_class: Any connection_kwargs: Any max_connections: Any diff --git a/stubs/urllib3/urllib3/response.pyi b/stubs/urllib3/urllib3/response.pyi index ed6934fdd..ff63225cc 100644 --- a/stubs/urllib3/urllib3/response.pyi +++ b/stubs/urllib3/urllib3/response.pyi @@ -1,4 +1,5 @@ import io +from _typeshed import Self from http.client import HTTPMessage as _HttplibHTTPMessage, HTTPResponse as _HttplibHTTPResponse from typing import Any, Iterator, Mapping from typing_extensions import Literal @@ -83,7 +84,7 @@ class HTTPResponse(io.IOBase): def read(self, amt: int | None = ..., decode_content: bool | None = ..., cache_content: bool = ...) -> bytes: ... def stream(self, amt: int | None = ..., decode_content: bool | None = ...) -> Iterator[bytes]: ... @classmethod - def from_httplib(cls, r: _HttplibHTTPResponse, **response_kw: Any) -> HTTPResponse: ... + def from_httplib(cls: type[Self], r: _HttplibHTTPResponse, **response_kw: Any) -> Self: ... def getheaders(self) -> HTTPHeaderDict: ... def getheader(self, name, default=...) -> str | None: ... def info(self) -> HTTPHeaderDict: ...