pure-interface 8.3.0__tar.gz → 8.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {pure_interface-8.3.0 → pure_interface-8.5.0}/PKG-INFO +1 -1
  2. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/__init__.py +1 -1
  3. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/adaption.py +6 -0
  4. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/interface.py +29 -4
  5. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface.egg-info/PKG-INFO +1 -1
  6. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface.egg-info/SOURCES.txt +1 -0
  7. {pure_interface-8.3.0 → pure_interface-8.5.0}/pyproject.toml +2 -2
  8. pure_interface-8.5.0/tests/test_adapter_cache.py +199 -0
  9. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_no_content_checks.py +6 -0
  10. {pure_interface-8.3.0 → pure_interface-8.5.0}/LICENSE +0 -0
  11. {pure_interface-8.3.0 → pure_interface-8.5.0}/README.rst +0 -0
  12. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/_sub_interface.py +0 -0
  13. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/delegation.py +0 -0
  14. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/errors.py +0 -0
  15. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/mypy_plugin.py +0 -0
  16. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface/py.typed +0 -0
  17. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface.egg-info/dependency_links.txt +0 -0
  18. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface.egg-info/requires.txt +0 -0
  19. {pure_interface-8.3.0 → pure_interface-8.5.0}/pure_interface.egg-info/top_level.txt +0 -0
  20. {pure_interface-8.3.0 → pure_interface-8.5.0}/setup.cfg +0 -0
  21. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_adapt_args_anno.py +0 -0
  22. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_adapt_args_no_anno.py +0 -0
  23. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_adapt_call.py +0 -0
  24. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_adaption.py +0 -0
  25. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_dataclass_support.py +0 -0
  26. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_delegate.py +0 -0
  27. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_func_sigs3.py +0 -0
  28. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_func_sigs_po.py +0 -0
  29. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_function_sigs.py +0 -0
  30. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_generic_support.py +0 -0
  31. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_implementation_checks.py +0 -0
  32. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_inheritance.py +0 -0
  33. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_isinstance.py +0 -0
  34. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_meta_classes.py +0 -0
  35. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_module_funcs.py +0 -0
  36. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_singledispatch.py +0 -0
  37. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_sub_interfaces.py +0 -0
  38. {pure_interface-8.3.0 → pure_interface-8.5.0}/tests/test_tracker.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pure-interface
3
- Version: 8.3.0
3
+ Version: 8.5.0
4
4
  Summary: A Python interface library that disallows function body content on interfaces and supports adaption.
5
5
  Author-email: Tim Mitchell <tim.mitchell@seequent.com>
6
6
  License-Expression: MIT
@@ -19,4 +19,4 @@ from .interface import (
19
19
  type_is_interface,
20
20
  )
21
21
 
22
- __version__ = "8.3.0" # Don't change this manually - run `bump-my-version bump [major|minor|patch]` instead
22
+ __version__ = "8.5.0" # Don't change this manually - run `bump-my-version bump [major|minor|patch]` instead
@@ -16,6 +16,7 @@ from .interface import (
16
16
  AnInterface,
17
17
  Interface,
18
18
  InterfaceType,
19
+ clear_adapter_caches,
19
20
  get_pi_attribute,
20
21
  get_type_interfaces,
21
22
  type_is_interface,
@@ -81,6 +82,11 @@ def register_adapter(
81
82
  raise AdaptionError("{} already has an adapter to {}".format(from_type, to_interface))
82
83
 
83
84
  adapters[from_type] = adapter
85
+ # Clears all cached adapter lookups for to_interface and its ancestors.
86
+ # This is safe because registration is expected to happen at startup.
87
+ # Calling register_adapter at runtime will cause all cached adapters for
88
+ # the interface to be re-resolved on next use.
89
+ clear_adapter_caches(to_interface)
84
90
 
85
91
 
86
92
  class AdapterTracker(object):
@@ -32,6 +32,7 @@ from typing import (
32
32
  Tuple,
33
33
  Type,
34
34
  TypeVar,
35
+ cast,
35
36
  )
36
37
 
37
38
  from .errors import AdaptionError, InterfaceError
@@ -63,6 +64,17 @@ AnInterface = TypeVar("AnInterface", bound="Interface")
63
64
  AnInterfaceType = TypeVar("AnInterfaceType", bound="InterfaceType")
64
65
 
65
66
 
67
+ def clear_adapter_caches(interface_cls: type) -> None:
68
+ """Clear adapter caches for the given interface, all its ancestor interfaces,
69
+ and any interfaces that have registered this interface via .register()."""
70
+ affected = [interface_cls] + list(get_pi_attribute(interface_cls, "registered_by", ()))
71
+ for affected_cls in affected:
72
+ for cls in affected_cls.__mro__:
73
+ cache = get_pi_attribute(cls, "adapter_cache")
74
+ if cache is not None:
75
+ cache.clear()
76
+
77
+
66
78
  def _unique_list(items: Iterable[Any]) -> List[Any]:
67
79
  seen = set()
68
80
  unique_items = []
@@ -91,10 +103,12 @@ class _PIAttributes:
91
103
  # keep an ordered list for dataclass
92
104
  self.interface_attribute_names: List[str] = _unique_list(interface_attribute_names)
93
105
  self.interface_method_signatures = interface_method_signatures
94
- self.adapters = weakref.WeakKeyDictionary() # type: ignore
95
- self.registered_types = weakref.WeakSet() # type: ignore
106
+ self.adapters: weakref.WeakKeyDictionary[type, Callable] = weakref.WeakKeyDictionary()
107
+ self.registered_types: weakref.WeakSet[type] = weakref.WeakSet()
108
+ self.registered_by: weakref.WeakSet[type] = weakref.WeakSet() # interfaces that have registered this one
96
109
  self.structural_subclasses: Set[type] = set()
97
110
  self.impl_wrapper_type: Optional[type] = None
111
+ self.adapter_cache: weakref.WeakKeyDictionary[type, Optional[Callable]] = weakref.WeakKeyDictionary()
98
112
 
99
113
  @property
100
114
  def interface_names(self) -> FrozenSet[str]:
@@ -168,6 +182,8 @@ def _type_is_interface(cls: type) -> bool:
168
182
  """Return True if cls is a pure interface or an empty ABC class"""
169
183
  if cls is object:
170
184
  return False
185
+ if cls.__module__ == "collections.abc":
186
+ return True
171
187
  if hasattr(cls, "_pi"):
172
188
  return cls._pi.type_is_interface
173
189
  if cls is Generic:
@@ -574,7 +590,7 @@ def _get_adapter(cls: AnInterfaceType, obj_type: Type) -> Optional[Callable]:
574
590
  candidate_interfaces.reverse() # prefer this class over sub-class adapters
575
591
  for subcls in candidate_interfaces:
576
592
  if type_is_interface(subcls):
577
- adapters.update(subcls._pi.adapters)
593
+ adapters.update(cast(AnInterfaceType, subcls)._pi.adapters)
578
594
  if not adapters:
579
595
  return None
580
596
 
@@ -753,7 +769,13 @@ class InterfaceType(abc.ABCMeta):
753
769
  if InterfaceType._provided_by(cls, obj, allow_implicit=allow_implicit):
754
770
  adapter = no_adaption
755
771
  else:
756
- adapter = _get_adapter(cls, type(obj))
772
+ obj_type = type(obj)
773
+ cache = cls._pi.adapter_cache
774
+ try:
775
+ adapter = cache[obj_type]
776
+ except KeyError:
777
+ cache[obj_type] = _get_adapter(cls, obj_type)
778
+ adapter = cache[obj_type]
757
779
  if adapter is None:
758
780
  raise AdaptionError("Cannot adapt {} to {}".format(obj, cls.__name__))
759
781
 
@@ -793,6 +815,9 @@ class InterfaceType(abc.ABCMeta):
793
815
  def register(cls, subclass: Type[_T]) -> Type[_T]:
794
816
  if type_is_interface(cls):
795
817
  cls._pi.registered_types.add(subclass) # type: ignore[attr-defined]
818
+ if type_is_interface(subclass):
819
+ subclass._pi.registered_by.add(cls) # type: ignore[attr-defined]
820
+ clear_adapter_caches(cls)
796
821
  return super().register(subclass)
797
822
 
798
823
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pure-interface
3
- Version: 8.3.0
3
+ Version: 8.5.0
4
4
  Summary: A Python interface library that disallows function body content on interfaces and supports adaption.
5
5
  Author-email: Tim Mitchell <tim.mitchell@seequent.com>
6
6
  License-Expression: MIT
@@ -17,6 +17,7 @@ pure_interface.egg-info/top_level.txt
17
17
  tests/test_adapt_args_anno.py
18
18
  tests/test_adapt_args_no_anno.py
19
19
  tests/test_adapt_call.py
20
+ tests/test_adapter_cache.py
20
21
  tests/test_adaption.py
21
22
  tests/test_dataclass_support.py
22
23
  tests/test_delegate.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pure-interface"
3
- version = "8.3.0"
3
+ version = "8.5.0"
4
4
  description = "A Python interface library that disallows function body content on interfaces and supports adaption."
5
5
  keywords = ["interface", "protocol", "adaption", "structural", "dataclass"]
6
6
  authors = [
@@ -43,7 +43,7 @@ line-length = 120
43
43
  profile = "black"
44
44
 
45
45
  [tool.bumpversion]
46
- current_version = "8.3.0"
46
+ current_version = "8.5.0"
47
47
  parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
48
48
  serialize = ["{major}.{minor}.{patch}"]
49
49
  search = "{current_version}"
@@ -0,0 +1,199 @@
1
+ # --------------------------------------------------------------------------------------------
2
+ # Copyright (c) 2024 Bentley Systems, Incorporated. All rights reserved.
3
+ # --------------------------------------------------------------------------------------------
4
+
5
+ import unittest
6
+ from unittest import mock
7
+
8
+ import pure_interface
9
+ from pure_interface import Interface, interface
10
+ from pure_interface.adaption import register_adapter
11
+
12
+
13
+ class IAnimal(Interface):
14
+ def speak(self):
15
+ pass
16
+
17
+
18
+ class IDog(IAnimal, Interface):
19
+ def fetch(self):
20
+ pass
21
+
22
+
23
+ class ICreature(Interface):
24
+ """Separate interface used to test cache invalidation via .register()."""
25
+
26
+ def speak(self):
27
+ pass
28
+
29
+
30
+ class Cat:
31
+ pass
32
+
33
+
34
+ class CatToIAnimal(IAnimal):
35
+ def __init__(self, obj):
36
+ pass
37
+
38
+ def speak(self):
39
+ return "meow"
40
+
41
+
42
+ class Hawk:
43
+ pass
44
+
45
+
46
+ class HawkToIDog(IDog):
47
+ def __init__(self, obj):
48
+ pass
49
+
50
+ def speak(self):
51
+ return "screech"
52
+
53
+ def fetch(self):
54
+ return "prey"
55
+
56
+
57
+ class Fish:
58
+ pass # used to test None caching and late adapter registration
59
+
60
+
61
+ class FishToIAnimal(IAnimal):
62
+ def __init__(self, obj):
63
+ pass
64
+
65
+ def speak(self):
66
+ return "blub"
67
+
68
+
69
+ class FishToIDog(IDog):
70
+ def __init__(self, obj):
71
+ pass
72
+
73
+ def speak(self):
74
+ return "blub"
75
+
76
+ def fetch(self):
77
+ return "swim"
78
+
79
+
80
+ class Iguana:
81
+ pass # used for register() test
82
+
83
+
84
+ ICreature.register(IAnimal)
85
+
86
+
87
+ def _register_adapters():
88
+ register_adapter(CatToIAnimal, Cat, IAnimal)
89
+ register_adapter(HawkToIDog, Hawk, IDog)
90
+
91
+
92
+ class TestAdapterCache(unittest.TestCase):
93
+ @classmethod
94
+ def setUpClass(cls):
95
+ pure_interface.set_is_development(True)
96
+
97
+ def setUp(self):
98
+ _register_adapters()
99
+
100
+ def tearDown(self):
101
+ IAnimal._pi.adapter_cache.clear()
102
+ IDog._pi.adapter_cache.clear()
103
+ ICreature._pi.adapter_cache.clear()
104
+ IAnimal._pi.adapters.clear()
105
+ IDog._pi.adapters.clear()
106
+
107
+ def test_adapter_cached_after_adapt(self):
108
+ IAnimal.adapt(Cat(), interface_only=False)
109
+ self.assertIn(Cat, IAnimal._pi.adapter_cache)
110
+ self.assertIs(IAnimal._pi.adapter_cache[Cat], CatToIAnimal)
111
+
112
+ def test_get_adapter_called_only_once_for_repeated_adapts(self):
113
+ cat = Cat()
114
+ with mock.patch("pure_interface.interface._get_adapter", wraps=interface._get_adapter) as mock_get:
115
+ IAnimal.adapt(cat, interface_only=False)
116
+ IAnimal.adapt(cat, interface_only=False)
117
+ mock_get.assert_called_once()
118
+
119
+ def test_none_cached_when_no_adapter_exists(self):
120
+ IAnimal.adapt_or_none(Fish(), interface_only=False)
121
+ self.assertIn(Fish, IAnimal._pi.adapter_cache)
122
+ self.assertIsNone(IAnimal._pi.adapter_cache[Fish])
123
+
124
+ def test_get_adapter_called_only_once_for_repeated_none_results(self):
125
+ fish = Fish()
126
+ with mock.patch("pure_interface.interface._get_adapter", wraps=interface._get_adapter) as mock_get:
127
+ IAnimal.adapt_or_none(fish, interface_only=False)
128
+ IAnimal.adapt_or_none(fish, interface_only=False)
129
+ mock_get.assert_called_once()
130
+
131
+ def test_clear_adapter_caches_clears_interface_cache(self):
132
+ IAnimal._pi.adapter_cache[Cat] = CatToIAnimal
133
+ interface.clear_adapter_caches(IAnimal)
134
+ self.assertEqual(len(IAnimal._pi.adapter_cache), 0)
135
+
136
+ def test_clear_adapter_caches_on_child_clears_parent_cache(self):
137
+ # IAnimal's cache should be cleared when IDog's adapters change,
138
+ # because _get_adapter(IAnimal, ...) collects adapters from IAnimal.__subclasses__()
139
+ IAnimal._pi.adapter_cache[Hawk] = None
140
+ interface.clear_adapter_caches(IDog)
141
+ self.assertNotIn(Hawk, IAnimal._pi.adapter_cache)
142
+
143
+ def test_parent_interface_finds_adapter_registered_on_child(self):
144
+ adapted = IAnimal.adapt(Hawk(), interface_only=False)
145
+ self.assertIsInstance(adapted, HawkToIDog)
146
+
147
+ def test_parent_caches_adapter_found_via_child(self):
148
+ IAnimal.adapt(Hawk(), interface_only=False)
149
+ self.assertIn(Hawk, IAnimal._pi.adapter_cache)
150
+ self.assertIs(IAnimal._pi.adapter_cache[Hawk], HawkToIDog)
151
+
152
+ def test_register_type_clears_cache(self):
153
+ IAnimal._pi.adapter_cache[Iguana] = None
154
+ IAnimal.register(Iguana)
155
+ self.assertNotIn(Iguana, IAnimal._pi.adapter_cache)
156
+
157
+ def test_register_adapter_clears_entire_cache(self):
158
+ # Pre-populate cache with an unrelated entry
159
+ IAnimal._pi.adapter_cache[Fish] = None
160
+ # Registering any adapter should wipe the whole cache
161
+ register_adapter(lambda obj: CatToIAnimal(obj), Iguana, IAnimal)
162
+ self.assertEqual(len(IAnimal._pi.adapter_cache), 0)
163
+
164
+ def test_stale_none_on_parent_cleared_when_adapter_registered_on_child(self):
165
+ # Prime a stale None on both parent and child: no adapter for Fish yet
166
+ self.assertIsNone(IAnimal.adapt_or_none(Fish(), interface_only=False))
167
+ self.assertIsNone(IDog.adapt_or_none(Fish(), interface_only=False))
168
+ self.assertIsNone(IAnimal._pi.adapter_cache[Fish])
169
+ self.assertIsNone(IDog._pi.adapter_cache[Fish])
170
+
171
+ # Registering on the child must evict the stale entry from both caches
172
+ register_adapter(FishToIDog, Fish, IDog)
173
+ self.assertNotIn(Fish, IAnimal._pi.adapter_cache)
174
+ self.assertNotIn(Fish, IDog._pi.adapter_cache)
175
+
176
+ # Parent can now adapt Fish via the child's adapter
177
+ adapted = IAnimal.adapt(Fish(), interface_only=False)
178
+ self.assertIsInstance(adapted, FishToIDog)
179
+
180
+ def test_stale_none_cleared_when_adapter_added_to_registered_interface(self):
181
+ # IAnimal is registered as a virtual subclass of ICreature at module level.
182
+ # _get_adapter(ICreature, T) therefore reads IAnimal._pi.adapters.
183
+ # If a stale None is cached on ICreature and a new adapter is later added
184
+ # to IAnimal, the stale entry must be evicted so ICreature can find the adapter.
185
+
186
+ # Prime ICreature's cache with None — no adapter for Fish exists yet
187
+ self.assertIsNone(ICreature.adapt_or_none(Fish(), interface_only=False))
188
+ self.assertIn(Fish, ICreature._pi.adapter_cache)
189
+ self.assertIsNone(ICreature._pi.adapter_cache[Fish])
190
+
191
+ # Register an adapter for Fish on IAnimal
192
+ register_adapter(FishToIAnimal, Fish, IAnimal)
193
+
194
+ # ICreature's stale None should have been evicted
195
+ self.assertNotIn(Fish, ICreature._pi.adapter_cache)
196
+
197
+ # ICreature should now successfully adapt Fish via IAnimal's adapter
198
+ adapted = ICreature.adapt(Fish(), interface_only=False)
199
+ self.assertIsInstance(adapted, FishToIAnimal)
@@ -3,6 +3,7 @@
3
3
  # --------------------------------------------------------------------------------------------
4
4
 
5
5
  import abc
6
+ import collections.abc
6
7
  import unittest
7
8
 
8
9
  import pure_interface
@@ -106,3 +107,8 @@ class TestNoContentChecks(unittest.TestCase):
106
107
  @property
107
108
  def height(self):
108
109
  return self
110
+
111
+ def test_std_lib_abc_passes(self):
112
+
113
+ class ITest(collections.abc.Sized, pure_interface.Interface):
114
+ pass
File without changes
File without changes