Skip to content

Commit 9582266

Browse files
Viicoszzzeek
authored andcommitted
Use typing.Self
Made use of pep-673 ``Self`` type for method chained methods such as :meth:`.CacheRegion.configure` and :meth:`.ProxyBackend.wrap`. Pull request courtesy Viicos. Fixes: #240 Closes: #241 Pull-request: #241 Pull-request-sha: 33ea2b3 Change-Id: Ic981aebde7b2082f4cac7889b79c9eaa9774770b
1 parent fd9d31a commit 9582266

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

docs/build/unreleased/240.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: bug, typing
3+
:tickets: 240
4+
5+
Made use of pep-673 ``Self`` type for method chained methods such as
6+
:meth:`.CacheRegion.configure` and :meth:`.ProxyBackend.wrap`. Pull request
7+
courtesy Viicos.

dogpile/cache/api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from typing import Sequence
1010
from typing import Union
1111

12+
from ..util.typing import Self
13+
1214

1315
class NoValue:
1416
"""Describe a missing cache value.
@@ -18,7 +20,7 @@ class NoValue:
1820
"""
1921

2022
@property
21-
def payload(self):
23+
def payload(self) -> Self:
2224
return self
2325

2426
def __repr__(self):
@@ -190,7 +192,9 @@ def __init__(self, arguments: BackendArguments): # pragma NO COVERAGE
190192
raise NotImplementedError()
191193

192194
@classmethod
193-
def from_config_dict(cls, config_dict, prefix):
195+
def from_config_dict(
196+
cls, config_dict: Mapping[str, Any], prefix: str
197+
) -> Self:
194198
prefix_len = len(prefix)
195199
return cls(
196200
dict(

dogpile/cache/proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
.. versionadded:: 0.5.0 Added support for the :class:`.ProxyBackend` class.
1010
1111
"""
12-
1312
from typing import Mapping
1413
from typing import Optional
1514
from typing import Sequence
@@ -20,6 +19,7 @@
2019
from .api import CacheMutex
2120
from .api import KeyType
2221
from .api import SerializedReturnType
22+
from ..util.typing import Self
2323

2424

2525
class ProxyBackend(CacheBackend):
@@ -67,7 +67,7 @@ def get(self, key):
6767
def __init__(self, *arg, **kw):
6868
pass
6969

70-
def wrap(self, backend: CacheBackend) -> "ProxyBackend":
70+
def wrap(self, backend: CacheBackend) -> Self:
7171
"""Take a backend as an argument and setup the self.proxied property.
7272
Return an object that be used as a backend by a :class:`.CacheRegion`
7373
object.

dogpile/cache/region.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from ..util import memoized_property
4747
from ..util import NameRegistry
4848
from ..util import PluginLoader
49+
from ..util.typing import Self
4950

5051
value_version = 2
5152
"""An integer placed in the :class:`.CachedValue`
@@ -426,7 +427,7 @@ def configure(
426427
wrap: Sequence[Union[ProxyBackend, Type[ProxyBackend]]] = (),
427428
replace_existing_backend: bool = False,
428429
region_invalidator: Optional[RegionInvalidationStrategy] = None,
429-
) -> "CacheRegion":
430+
) -> Self:
430431
"""Configure a :class:`.CacheRegion`.
431432
432433
The :class:`.CacheRegion` itself

dogpile/util/typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
if sys.version_info >= (3, 11):
4+
from typing import Self
5+
else:
6+
from typing_extensions import Self # noqa: F401

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ project_urls =
2424
install_requires =
2525
decorator>=4.0.0
2626
stevedore>=3.0.0
27+
typing_extensions>=4.0.1;python_version<'3.11'
2728
zip_safe = False
2829
packages = find:
2930
python_requires = >=3.6

0 commit comments

Comments
 (0)