Commit 81cfe3c
authored
Simplify Model __new__ and metaclass (#7473)
* Type get_context correctly
get_context returns an instance of a Model, not a ContextMeta object
We don't need the typevar, since we don't use it for anything special
* Import from future to use delayed evaluation of annotations
All of these are supported on python>=3.9.
* New ModelManager class for managing model contexts
We create a global instance of it within this module, which is similar
to how it worked before, where a `context_class` attribute was attached
to the Model class.
We inherit from threading.local to ensure thread safety when working
with models on multiple threads. See #1552 for the reasoning. This is
already tested in `test_thread_safety`.
* Model class is now the context manager directly
* Fix type of UNSET in type definition
UNSET is the instance of the _UnsetType type.
We should be typing the latter here.
* Set model parent in init rather than in __new__
We use the new ModelManager.parent_context property to reliably set any
parent context, or else set it to None.
* Replace get_context in metaclass with classmethod
We set this directly on the class as a classmethod, which is clearer
than going via the metaclass.
* Remove get_contexts from metaclass
The original function does not behave as I expected.
In the following example I expected that it would return only the final
model, not root.
This method is not used anywhere in the pymc codebase, so I have dropped
it from the codebase. I originally included the following code to replace
it, but since it is not used anyway, it is better to remove it.
```python`
@classmethod
def get_contexts(cls) -> list[Model]:
"""Return a list of the currently active model contexts."""
return MODEL_MANAGER.active_contexts
```
Example for testing behaviour in current main branch:
```python
import pymc as pm
with pm.Model(name="root") as root:
print([c.name for c in pm.Model.get_contexts()])
with pm.Model(name="first") as first:
print([c.name for c in pm.Model.get_contexts()])
with pm.Model(name="m_with_model_None", model=None) as m_with_model_None:
# This one doesn't make much sense:
print([c.name for c in pm.Model.get_contexts()])
```
* Simplify ContextMeta
We only keep the __call__ method, which is necessary to keep the
model context itself active during that model's __init__.
* Type Model.register_rv for for downstream typing
In pymc/distributions/distribution.py, this change allows the type
checker to infer that `rv_out` can only be a TensorVariable.
Thanks to @ricardoV94 for type hint on rv_var.
* Include np.ndarray as possible type for coord values
I originally tried numpy's ArrayLike, replacing Sequence entirely, but then I realized
that ArrayLike also allows non-sequences like integers and floats.
I am not certain if `values="a string"` should be legal. With the type hint sequence, it is.
Might be more accurate, but verbose to use `list | tuple | set | np.ndarray | None`.
* Use function-scoped new_dims to handle type hint varying throughout function
We don't want to allow the user to pass a `dims=[None, None]` to our function, but current behaviour
set `dims=[None] * N` at the end of `determine_coords`.
To handle this, I created a `new_dims` with a larger type scope which matches
the return type of `dims` in `determine_coords`.
Then I did the same within def Data to support this new type hint.
* Fix case of dims = [None, None, ...]
The only case where dims=[None, ...] is when the user has passed dims=None. Since the user passed dims=None,
they shouldn't be expecting any coords to match that dimension. Thus we don't need to try to add any
more coords to the model.
* Remove unused hack1 parent e25a042 commit 81cfe3c
2 files changed
+74
-146
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
| 224 | + | |
225 | 225 | | |
226 | | - | |
| 226 | + | |
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| |||
268 | 268 | | |
269 | 269 | | |
270 | 270 | | |
271 | | - | |
272 | | - | |
273 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
274 | 275 | | |
275 | 276 | | |
276 | 277 | | |
| |||
366 | 367 | | |
367 | 368 | | |
368 | 369 | | |
369 | | - | |
| 370 | + | |
370 | 371 | | |
371 | 372 | | |
372 | 373 | | |
| |||
451 | 452 | | |
452 | 453 | | |
453 | 454 | | |
| 455 | + | |
454 | 456 | | |
455 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
456 | 460 | | |
457 | | - | |
| 461 | + | |
458 | 462 | | |
459 | 463 | | |
460 | | - | |
461 | | - | |
| 464 | + | |
| 465 | + | |
462 | 466 | | |
463 | 467 | | |
464 | 468 | | |
| |||
467 | 471 | | |
468 | 472 | | |
469 | 473 | | |
470 | | - | |
| 474 | + | |
471 | 475 | | |
472 | 476 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | | - | |
23 | 23 | | |
24 | | - | |
25 | 24 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
| |||
42 | 38 | | |
43 | 39 | | |
44 | 40 | | |
45 | | - | |
46 | 41 | | |
47 | 42 | | |
48 | 43 | | |
| |||
73 | 68 | | |
74 | 69 | | |
75 | 70 | | |
| 71 | + | |
76 | 72 | | |
77 | 73 | | |
78 | 74 | | |
| |||
92 | 88 | | |
93 | 89 | | |
94 | 90 | | |
95 | | - | |
| 91 | + | |
| 92 | + | |
96 | 93 | | |
| 94 | + | |
| 95 | + | |
97 | 96 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
110 | 100 | | |
111 | | - | |
112 | | - | |
| 101 | + | |
| 102 | + | |
113 | 103 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
119 | 108 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
127 | 113 | | |
128 | | - | |
129 | | - | |
130 | 114 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
204 | 118 | | |
205 | 119 | | |
206 | | - | |
| 120 | + | |
207 | 121 | | |
208 | 122 | | |
209 | 123 | | |
| |||
372 | 286 | | |
373 | 287 | | |
374 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
375 | 301 | | |
376 | 302 | | |
377 | 303 | | |
| |||
495 | 421 | | |
496 | 422 | | |
497 | 423 | | |
498 | | - | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
499 | 428 | | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
514 | 432 | | |
515 | 433 | | |
516 | 434 | | |
| |||
525 | 443 | | |
526 | 444 | | |
527 | 445 | | |
528 | | - | |
| 446 | + | |
529 | 447 | | |
530 | | - | |
531 | 448 | | |
532 | 449 | | |
| 450 | + | |
533 | 451 | | |
534 | 452 | | |
535 | 453 | | |
| |||
577 | 495 | | |
578 | 496 | | |
579 | 497 | | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
580 | 509 | | |
581 | 510 | | |
582 | 511 | | |
| |||
967 | 896 | | |
968 | 897 | | |
969 | 898 | | |
970 | | - | |
| 899 | + | |
971 | 900 | | |
972 | 901 | | |
973 | 902 | | |
| |||
1233 | 1162 | | |
1234 | 1163 | | |
1235 | 1164 | | |
1236 | | - | |
1237 | | - | |
| 1165 | + | |
| 1166 | + | |
1238 | 1167 | | |
1239 | 1168 | | |
1240 | 1169 | | |
1241 | 1170 | | |
1242 | 1171 | | |
1243 | 1172 | | |
1244 | 1173 | | |
1245 | | - | |
| 1174 | + | |
1246 | 1175 | | |
1247 | 1176 | | |
1248 | 1177 | | |
| |||
2074 | 2003 | | |
2075 | 2004 | | |
2076 | 2005 | | |
2077 | | - | |
2078 | | - | |
2079 | | - | |
2080 | | - | |
2081 | | - | |
2082 | 2006 | | |
2083 | 2007 | | |
2084 | 2008 | | |
| |||
0 commit comments