Skip to content

Commit

Permalink
Change method return types to Self where suitable (#3364)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Mar 17, 2024
1 parent 733f638 commit cc892b0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog/3363.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Consistently used ``typing.Self`` for return types representing copying actions.
6 changes: 3 additions & 3 deletions src/urllib3/_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def _copy_from(self, other: HTTPHeaderDict) -> None:
val = other.getlist(key)
self._container[key.lower()] = [key, *val]

def copy(self) -> HTTPHeaderDict:
def copy(self) -> Self:
clone = type(self)()
clone._copy_from(self)
return clone
Expand Down Expand Up @@ -462,7 +462,7 @@ def __ior__(self, other: object) -> HTTPHeaderDict:
self.extend(maybe_constructable)
return self

def __or__(self, other: object) -> HTTPHeaderDict:
def __or__(self, other: object) -> Self:
# Supports merging header dicts using operator |
# combining items with add instead of __setitem__
maybe_constructable = ensure_can_construct_http_header_dict(other)
Expand All @@ -472,7 +472,7 @@ def __or__(self, other: object) -> HTTPHeaderDict:
result.extend(maybe_constructable)
return result

def __ror__(self, other: object) -> HTTPHeaderDict:
def __ror__(self, other: object) -> Self:
# Supports merging header dicts using operator | when other is on left side
# combining items with add instead of __setitem__
maybe_constructable = ensure_can_construct_http_header_dict(other)
Expand Down
6 changes: 3 additions & 3 deletions src/urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
import ssl
from typing import Literal

from typing_extensions import Self

from ._base_connection import BaseHTTPConnection, BaseHTTPSConnection

log = logging.getLogger(__name__)

_TYPE_TIMEOUT = typing.Union[Timeout, float, _TYPE_DEFAULT, None]

_SelfT = typing.TypeVar("_SelfT")


# Pool objects
class ConnectionPool:
Expand Down Expand Up @@ -95,7 +95,7 @@ def __init__(self, host: str, port: int | None = None) -> None:
def __str__(self) -> str:
return f"{type(self).__name__}(host={self.host!r}, port={self.port!r})"

def __enter__(self: _SelfT) -> _SelfT:
def __enter__(self) -> Self:
return self

def __exit__(
Expand Down
6 changes: 3 additions & 3 deletions src/urllib3/poolmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import ssl
from typing import Literal

from typing_extensions import Self

__all__ = ["PoolManager", "ProxyManager", "proxy_from_url"]


Expand All @@ -51,8 +53,6 @@
# http.client.HTTPConnection & http.client.HTTPSConnection in Python 3.7
_DEFAULT_BLOCKSIZE = 16384

_SelfT = typing.TypeVar("_SelfT")


class PoolKey(typing.NamedTuple):
"""
Expand Down Expand Up @@ -214,7 +214,7 @@ def __init__(
self.pool_classes_by_scheme = pool_classes_by_scheme
self.key_fn_by_scheme = key_fn_by_scheme.copy()

def __enter__(self: _SelfT) -> _SelfT:
def __enter__(self) -> Self:
return self

def __exit__(
Expand Down
6 changes: 4 additions & 2 deletions src/urllib3/util/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from .util import reraise

if typing.TYPE_CHECKING:
from typing_extensions import Self

from ..connectionpool import ConnectionPool
from ..response import BaseHTTPResponse

Expand Down Expand Up @@ -240,7 +242,7 @@ def __init__(
)
self.backoff_jitter = backoff_jitter

def new(self, **kw: typing.Any) -> Retry:
def new(self, **kw: typing.Any) -> Self:
params = dict(
total=self.total,
connect=self.connect,
Expand Down Expand Up @@ -429,7 +431,7 @@ def increment(
error: Exception | None = None,
_pool: ConnectionPool | None = None,
_stacktrace: TracebackType | None = None,
) -> Retry:
) -> Self:
"""Return a new Retry object with incremented retry counters.
:param response: A response object, or None, if the server did not
Expand Down
5 changes: 3 additions & 2 deletions src/urllib3/util/ssltransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
if typing.TYPE_CHECKING:
from typing import Literal

from typing_extensions import Self

from .ssl_ import _TYPE_PEER_CERT_RET, _TYPE_PEER_CERT_RET_DICT


_SelfT = typing.TypeVar("_SelfT", bound="SSLTransport")
_WriteBuffer = typing.Union[bytearray, memoryview]
_ReturnValue = typing.TypeVar("_ReturnValue")

Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(
# Perform initial handshake.
self._ssl_io_loop(self.sslobj.do_handshake)

def __enter__(self: _SelfT) -> _SelfT:
def __enter__(self) -> Self:
return self

def __exit__(self, *_: typing.Any) -> None:
Expand Down

0 comments on commit cc892b0

Please sign in to comment.