Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
60 lines (39 loc) · 1.61 KB

File metadata and controls

60 lines (39 loc) · 1.61 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from abc import ABCMeta, abstractmethod
from classes import typeclass
@typeclass
def my_typeclass(instance) -> int:
"""Example typeclass."""
class _MyABC(object, metaclass=ABCMeta):
@abstractmethod
def get_number(self) -> int:
"""Example abstract method."""
class _MyConcrete(_MyABC):
def get_number(self) -> int:
"""Concrete method."""
return 1
class _MyRegistered(object):
def get_number(self) -> int:
"""Would be registered in ``_MyABC`` later."""
return 2
def _my_int(instance: int) -> int:
return instance
def _my_abc(instance: _MyABC) -> int:
return instance.get_number()
def test_cache_concrete(clear_cache) -> None: # noqa: WPS218
"""Ensures that cache invalidation for ABC types work correctly."""
with clear_cache(my_typeclass):
assert not my_typeclass._dispatch_cache # noqa: WPS437
my_typeclass.instance(_MyABC)(_my_abc)
assert not my_typeclass._dispatch_cache # noqa: WPS437
assert my_typeclass(_MyConcrete()) == 1
assert _MyConcrete in my_typeclass._dispatch_cache # noqa: WPS437
_MyABC.register(_MyRegistered)
assert my_typeclass(_MyRegistered()) == 2 # type: ignore
assert _MyRegistered in my_typeclass._dispatch_cache # noqa: WPS437
def test_cached_calls(clear_cache) -> None:
"""Ensures that regular types trigger cache."""
with clear_cache(my_typeclass):
my_typeclass.instance(int)(_my_int)
assert not my_typeclass._dispatch_cache # noqa: WPS437
assert my_typeclass(1)
assert my_typeclass._dispatch_cache # noqa: WPS437
Morty Proxy This is a proxified and sanitized view of the page, visit original site.