Commit ace5814f authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Use Self from PEP 673 instead of TypeVars

parent 230439b7
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -27,8 +27,9 @@ __all__ = [


if TYPE_CHECKING:
	from typing_extensions import Self

	T = TypeVar("T")
	C = TypeVar("C", bound="PatternEnum")


	class PatternConverter(Protocol):
@@ -112,5 +113,5 @@ class PatternEnum(enum.Enum, metaclass=EnumMeta):
	"""

	@classmethod
	def _missing_(cls: type[C], key: Any) -> C:
	def _missing_(cls, key: Any) -> Self:
		return cls[key]
+8 −13
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ from typing import TYPE_CHECKING
from typing import Any
from typing import NewType
from typing import Protocol
from typing import TypeVar
from typing import Union
from typing import cast
from urllib.parse import urlparse
@@ -55,6 +54,9 @@ from .proc import MutableArguments
from .proc import exec_io
from .utils import wait

if TYPE_CHECKING:
	from typing_extensions import Self

LOCALHOST = ipaddress.IPv4Address(0x7f000001)

ShaID = NewType("ShaID", str)
@@ -152,10 +154,8 @@ class IPv4Address(ipaddress.IPv4Address):
	Subclass of IPv4Address that handle's docker idiosyncratic tendency to add a mask suffix
	"""

	T = TypeVar("T", bound="IPv4Address")

	@classmethod
	def with_suffix(cls: type[T], address: str) -> T:
	def with_suffix(cls, address: str) -> Self:
		"""
		Construct an instance with a suffixed bitmask size
		"""
@@ -190,13 +190,11 @@ class Image:

	_cache = dict[str, ShaID]()

	T = TypeVar('T', bound='Image')

	def __init__(self, iid: ShaID):
		self.iid = iid

	@classmethod
	def build(cls: type[T], context: Path, target: str = "", **build_args: str|None) -> T:
	def build(cls, context: Path, target: str = "", **build_args: str|None) -> Self:
		"""
		Build an image from the given context

@@ -217,7 +215,7 @@ class Image:
		return cls(iid)

	@classmethod
	def pull(cls: type[T], repository: str) -> T:
	def pull(cls, repository: str) -> Self:
		"""
		Pull an image from a registry
		"""
@@ -253,9 +251,6 @@ class Container:
	exiting the context.
	"""

	if TYPE_CHECKING:
		T = TypeVar('T', bound='Container')

	DEFAULT_ALIASES = tuple[str]()

	def __init__(
@@ -286,7 +281,7 @@ class Container:
		if network:
			self.connect(network, *self.DEFAULT_ALIASES)

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

	def __exit__(self, etype: type[BaseException], exc: BaseException, tb: TracebackType) -> None:
@@ -298,7 +293,7 @@ class Container:
			logging.getLogger(__name__).exception("ignoring exception while stopping")

	@contextmanager
	def started(self: T) -> Iterator[T]:
	def started(self) -> Iterator[Self]:
		"""
		Return a context manager that ensures the container is started when the context is entered
		"""
+6 −6
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ from __future__ import annotations

from collections.abc import Callable
from types import GenericAlias
from typing import TYPE_CHECKING
from typing import Any
from typing import TypeVar
from typing import overload
@@ -19,6 +20,9 @@ from typing import overload
import orjson
from jsonpath import JSONPath

if TYPE_CHECKING:
	from typing_extensions import Self

__all__ = [
	"JSONObject",
	"JSONArray",
@@ -57,10 +61,8 @@ class JSONObject(JSONPathMixin, dict[str, Any]):
	A dict for JSON objects that implements `.path` for getting child items by a JSON path
	"""

	T = TypeVar("T", bound="JSONObject")

	@classmethod
	def from_string(cls: type[T], string: bytes) -> T:
	def from_string(cls, string: bytes) -> Self:
		"""
		Create a JSONObject from a JSON string
		"""
@@ -75,10 +77,8 @@ class JSONArray(JSONPathMixin, list[Any]):
	A list for JSON arrays that implements `.path` for getting child items by a JSON path
	"""

	T = TypeVar("T", bound="JSONArray")

	@classmethod
	def from_string(cls: type[T], string: bytes) -> T:
	def from_string(cls, string: bytes) -> Self:
		"""
		Create a JSONArray from a JSON string
		"""
+4 −4
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ from warnings import warn

import trio.abc

if TYPE_CHECKING:
	from typing_extensions import Self

T = TypeVar('T')
Deserialiser = Callable[[memoryview], T]

@@ -224,9 +227,6 @@ async def _passthru(in_stream: trio.abc.ReceiveStream, out_stream: IO[str]|IO[by

class _ExecutorBase(list[Argument]):

	if TYPE_CHECKING:
		E = TypeVar("E", bound="_ExecutorBase")

	def __init__(self, *cmd: Argument):
		self[:] = cmd

@@ -243,7 +243,7 @@ class _ExecutorBase(list[Argument]):
		"""
		return cmd

	def subcommand(self: E, *args: Argument) -> E:
	def subcommand(self, *args: Argument) -> Self:
		"""
		Return a new Executor instance of the same class with additional arguments appended