hmr 0.4.2__tar.gz → 0.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hmr
3
- Version: 0.4.2
3
+ Version: 0.5.0
4
4
  Summary: Hot Module Reload for Python
5
5
  Project-URL: repository, https://github.com/promplate/pyth-on-line/tree/reactivity
6
6
  Requires-Python: >=3.12
@@ -6,7 +6,7 @@ description = "Hot Module Reload for Python"
6
6
  dependencies = [
7
7
  "watchfiles>=0.21,<2 ; sys_platform != 'emscripten'",
8
8
  ]
9
- version = "0.4.2"
9
+ version = "0.5.0"
10
10
 
11
11
  [project.scripts]
12
12
  hmr = "reactivity.hmr:cli"
@@ -112,11 +112,13 @@ class Reactive[K, V](Subscribable, MutableMapping[K, V]):
112
112
 
113
113
  def __iter__(self):
114
114
  self.track()
115
- return iter(self._signals)
115
+ unset = self.UNSET
116
+ return (key for key, signal in self._signals.items() if signal.get(track=False) is not unset)
116
117
 
117
118
  def __len__(self):
118
119
  self.track()
119
- return len(self._signals)
120
+ unset = self.UNSET
121
+ return sum(signal.get(track=False) is not unset for signal in self._signals.values())
120
122
 
121
123
  def __repr__(self):
122
124
  self.track()
@@ -15,21 +15,21 @@ from weakref import WeakValueDictionary
15
15
  from .. import Reactive, batch
16
16
  from ..functional import create_effect
17
17
  from ..helpers import DerivedMethod
18
- from ..primitives import BaseComputation, BaseDerived, Derived, Signal
18
+ from ..primitives import BaseDerived, Derived, Signal
19
19
  from .hooks import call_post_reload_hooks, call_pre_reload_hooks
20
20
 
21
21
 
22
- def is_called_in_this_file() -> bool:
23
- frame = currentframe() # this function
24
- assert frame is not None
22
+ def is_called_internally(*, extra_depth=0) -> bool:
23
+ """Protect private methods from being called from outside this package."""
25
24
 
26
- frame = frame.f_back # the function calling this function
25
+ frame = currentframe() # this function
27
26
  assert frame is not None
28
27
 
29
- frame = frame.f_back # the function calling the function calling this function
30
- assert frame is not None
28
+ for _ in range(extra_depth + 2):
29
+ frame = frame.f_back
30
+ assert frame is not None
31
31
 
32
- return frame.f_globals.get("__file__") == __file__
32
+ return frame.f_globals.get("__package__") == __package__
33
33
 
34
34
 
35
35
  class Name(Signal, BaseDerived):
@@ -83,7 +83,7 @@ class ReactiveModule(ModuleType):
83
83
 
84
84
  @property
85
85
  def file(self):
86
- if is_called_in_this_file():
86
+ if is_called_internally(extra_depth=1): # + 1 for `__getattribute__`
87
87
  return self.__file
88
88
  raise AttributeError("file")
89
89
 
@@ -103,21 +103,10 @@ class ReactiveModule(ModuleType):
103
103
  # unsubscribe it because we want invalidation to be fine-grained
104
104
  dep.subscribers.remove(load)
105
105
  load.dependencies.remove(dep)
106
- elif isinstance(dep, BaseComputation):
107
- visited = set() # cache visited nodes, maybe share cache between iteration too (?)
108
- to_visit: set[BaseComputation] = {dep}
109
- while to_visit and (current := to_visit.pop()) not in visited:
110
- visited.add(current)
111
- if load in current.dependencies:
112
- # unsubscribe it to avoid circular dependencies
113
- dep.subscribers.remove(load)
114
- load.dependencies.remove(dep)
115
- break
116
- to_visit.update(i for i in current.dependencies if isinstance(i, BaseComputation))
117
106
 
118
107
  @property
119
108
  def load(self):
120
- if is_called_in_this_file():
109
+ if is_called_internally(extra_depth=1): # + 1 for `__getattribute__`
121
110
  return self.__load
122
111
  raise AttributeError("load")
123
112
 
@@ -138,7 +127,7 @@ class ReactiveModule(ModuleType):
138
127
  raise AttributeError(*e.args) from e
139
128
 
140
129
  def __setattr__(self, name: str, value):
141
- if is_called_in_this_file():
130
+ if is_called_internally():
142
131
  return super().__setattr__(name, value)
143
132
  self.__namespace_proxy[name] = value
144
133
 
@@ -349,4 +338,4 @@ def cli():
349
338
  SyncReloader(entry).keep_watching_until_interrupt()
350
339
 
351
340
 
352
- __version__ = "0.4.2"
341
+ __version__ = "0.5.0"
@@ -61,3 +61,7 @@ def cache_across_reloads[T](func: Callable[[], T]) -> Callable[[], T]:
61
61
  class DictProxy(UserDict, dict): # type: ignore
62
62
  def __init__(self, data):
63
63
  self.data = data
64
+
65
+
66
+ def load(module: ReactiveModule):
67
+ return module.load()
@@ -212,4 +212,13 @@ class Derived[T](BaseDerived[T]):
212
212
 
213
213
 
214
214
  def _pulled(sub: Subscribable):
215
- return any(not isinstance(s, BaseDerived) or _pulled(s) for s in sub.subscribers)
215
+ visited = set()
216
+ to_visit: set[Subscribable] = {sub}
217
+ while to_visit:
218
+ visited.add(current := to_visit.pop())
219
+ for s in current.subscribers:
220
+ if not isinstance(s, BaseDerived):
221
+ return True
222
+ if s not in visited:
223
+ to_visit.add(s)
224
+ return False
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes