pyrefly-shape-extensions 1.4.0.dev3__tar.gz → 1.4.1__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.5
2
2
  Name: pyrefly-shape-extensions
3
- Version: 1.4.0.dev3
3
+ Version: 1.4.1
4
4
  Summary: Shape typing primitives and DSL for Pyrefly tensor shape tracking
5
5
  Project-URL: homepage, https://pyrefly.org
6
6
  Project-URL: documentation, https://pyrefly.org/en/docs/
@@ -40,7 +40,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
40
40
 
41
41
  This package provides the lightweight `shape_extensions` module used by
42
42
  Pyrefly's tensor shape stubs. It defines runtime no-op versions of the shape
43
- typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
43
+ typing primitives so annotations such as `Tensor[[B, T]]`, `IntVar("B")`, and
44
44
  `assert_shape(x.shape, (2, 3))` can be evaluated by Python while Pyrefly uses the
45
45
  corresponding stubs for static shape checking.
46
46
 
@@ -4,7 +4,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
4
4
 
5
5
  This package provides the lightweight `shape_extensions` module used by
6
6
  Pyrefly's tensor shape stubs. It defines runtime no-op versions of the shape
7
- typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
7
+ typing primitives so annotations such as `Tensor[[B, T]]`, `IntVar("B")`, and
8
8
  `assert_shape(x.shape, (2, 3))` can be evaluated by Python while Pyrefly uses the
9
9
  corresponding stubs for static shape checking.
10
10
 
@@ -8,7 +8,7 @@ description = "Shape typing primitives and DSL for Pyrefly tensor shape tracking
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.10"
10
10
  license = { file = "LICENSE" }
11
- version = "1.4.0.dev3"
11
+ version = "1.4.1"
12
12
  keywords = ["typechecker", "typechecking", "shapes", "tensor", "pyrefly"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -13,7 +13,9 @@ safe to evaluate in runtime annotations.
13
13
 
14
14
  import contextlib
15
15
  import keyword
16
+ import sys
16
17
  import typing
18
+ import warnings
17
19
  from dataclasses import dataclass
18
20
 
19
21
  # A shape annotation carrying its shape as string metadata. To Pyrefly, the
@@ -70,7 +72,79 @@ def _return_int(cls, params):
70
72
  return int
71
73
 
72
74
 
73
- def _patch_torch_if_available() -> None:
75
+ class _PostImportHook:
76
+ """A one-shot `sys.meta_path` finder that calls `callback` after a module loads.
77
+
78
+ The hook wraps the loader that the finders after it return for `name`, so
79
+ `callback` runs once the module body has finished executing, while the
80
+ import system still holds the module's import lock. The import that loaded
81
+ the module, and any concurrent import of it that reaches the hook, therefore
82
+ return only after `callback` has run. An import already resolving the module
83
+ when the hook is installed can bypass it. A failed load leaves the hook
84
+ registered, so a retried import still runs `callback`.
85
+ """
86
+
87
+ def __init__(self, name, callback):
88
+ self.name = name
89
+ self.callback = callback
90
+
91
+ def find_spec(self, fullname, path, target=None):
92
+ if fullname != self.name:
93
+ return None
94
+ meta_path = sys.meta_path
95
+ # An import still iterating the list this hook was removed from can
96
+ # call it after removal; the finders after it are consulted anyway.
97
+ if self not in meta_path:
98
+ return None
99
+ for finder in meta_path[meta_path.index(self) + 1 :]:
100
+ find_spec = getattr(finder, "find_spec", None)
101
+ if find_spec is None:
102
+ continue
103
+ spec = find_spec(fullname, path, target)
104
+ if spec is None:
105
+ continue
106
+ # A legacy loader without `exec_module` loads without the callback.
107
+ if hasattr(spec.loader, "exec_module"):
108
+ spec.loader = _PostImportLoader(spec, self)
109
+ else:
110
+ self.remove()
111
+ return spec
112
+ return None
113
+
114
+ def remove(self) -> None:
115
+ # Replace the list rather than mutating it: the import system iterates
116
+ # `sys.meta_path` without a lock, and an in-place removal would make a
117
+ # concurrent import skip the finder after this one.
118
+ sys.meta_path = [finder for finder in sys.meta_path if finder is not self]
119
+
120
+
121
+ class _PostImportLoader:
122
+ """Delegates to a module's real loader, then runs its `_PostImportHook`."""
123
+
124
+ def __init__(self, spec, hook):
125
+ self.spec = spec
126
+ self.loader = spec.loader
127
+ self.hook = hook
128
+
129
+ def __getattr__(self, name):
130
+ return getattr(self.loader, name)
131
+
132
+ def create_module(self, spec):
133
+ return self.loader.create_module(spec)
134
+
135
+ def exec_module(self, module):
136
+ # The module body sees its real loader, and keeps it afterwards.
137
+ self.spec.loader = self.loader
138
+ if getattr(module, "__loader__", None) is self:
139
+ module.__loader__ = self.loader
140
+ self.loader.exec_module(module)
141
+ self.hook.remove()
142
+ self.hook.callback()
143
+
144
+
145
+ def _patch_torch() -> None:
146
+ """Make Torch classes subscriptable at runtime. Calling it again is a no-op."""
147
+
74
148
  try:
75
149
  import torch # @manual
76
150
  import torch.nn as nn # @manual
@@ -111,7 +185,50 @@ def _patch_torch_if_available() -> None:
111
185
  cls.__class_getitem__ = classmethod(_return_class)
112
186
 
113
187
 
114
- _patch_torch_if_available()
188
+ def _patch_torch_safely() -> None:
189
+ """Report incompatible Torch classes without breaking the Torch import."""
190
+
191
+ try:
192
+ _patch_torch()
193
+ except (AttributeError, TypeError) as error:
194
+ warnings.warn(
195
+ f"Could not patch torch for shape annotations: {error}",
196
+ RuntimeWarning,
197
+ stacklevel=2,
198
+ )
199
+
200
+
201
+ def _is_initializing(module) -> bool:
202
+ """Whether the import system is still executing `module`'s body."""
203
+
204
+ return getattr(getattr(module, "__spec__", None), "_initializing", False)
205
+
206
+
207
+ def _patch_torch_when_imported() -> None:
208
+ """Patch Torch now if it has been imported, and otherwise once it is.
209
+
210
+ Importing `shape_extensions` alone never imports Torch.
211
+ """
212
+
213
+ torch = sys.modules.get("torch")
214
+ if _is_initializing(torch):
215
+ # Another thread may be importing Torch, in which case `import torch`
216
+ # waits for that import to finish. If Torch's own initialization
217
+ # imported this module, it instead returns the same partially
218
+ # initialized module, which cannot be patched yet. A failed import
219
+ # leaves `sys.modules`, so the hook below patches a retried import.
220
+ with contextlib.suppress(ImportError):
221
+ import torch # @manual
222
+ torch = sys.modules.get("torch")
223
+ if _is_initializing(torch):
224
+ return
225
+ if torch is None:
226
+ sys.meta_path.insert(0, _PostImportHook("torch", _patch_torch_safely))
227
+ return
228
+ _patch_torch_safely()
229
+
230
+
231
+ _patch_torch_when_imported()
115
232
 
116
233
 
117
234
  def _patch_jax_if_available() -> None:
@@ -73,6 +73,19 @@ def concat(
73
73
  return _IntTupleSchema((*left, *right))
74
74
 
75
75
 
76
+ def axis_reduce(
77
+ shape: _IntTupleSchema,
78
+ axis: int | tuple[int, ...] | None,
79
+ keepdims: bool,
80
+ empty_means_all: bool,
81
+ scalar_axis_ok: bool,
82
+ scalar_tuple_axis_ok: bool,
83
+ /,
84
+ ) -> _IntTupleSchema:
85
+ """Reduce axes, preserving dimensions outside a symbolic-rank middle."""
86
+ return _IntTupleSchema()
87
+
88
+
76
89
  def prod(xs: _IntTupleSchema, /) -> _IntSchema: ...
77
90
 
78
91