pyrefly-shape-extensions 1.4.0.dev3__tar.gz → 1.5.0.dev1__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.5.0.dev1
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.5.0.dev1"
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
@@ -50,6 +52,7 @@ __all__ = [
50
52
  "RegularNestedList",
51
53
  "Shaped",
52
54
  "SymbolicArithExpr",
55
+ "TypeVar",
53
56
  "assert_shape",
54
57
  "assert_raises",
55
58
  "broadcast",
@@ -70,7 +73,79 @@ def _return_int(cls, params):
70
73
  return int
71
74
 
72
75
 
73
- def _patch_torch_if_available() -> None:
76
+ class _PostImportHook:
77
+ """A one-shot `sys.meta_path` finder that calls `callback` after a module loads.
78
+
79
+ The hook wraps the loader that the finders after it return for `name`, so
80
+ `callback` runs once the module body has finished executing, while the
81
+ import system still holds the module's import lock. The import that loaded
82
+ the module, and any concurrent import of it that reaches the hook, therefore
83
+ return only after `callback` has run. An import already resolving the module
84
+ when the hook is installed can bypass it. A failed load leaves the hook
85
+ registered, so a retried import still runs `callback`.
86
+ """
87
+
88
+ def __init__(self, name, callback):
89
+ self.name = name
90
+ self.callback = callback
91
+
92
+ def find_spec(self, fullname, path, target=None):
93
+ if fullname != self.name:
94
+ return None
95
+ meta_path = sys.meta_path
96
+ # An import still iterating the list this hook was removed from can
97
+ # call it after removal; the finders after it are consulted anyway.
98
+ if self not in meta_path:
99
+ return None
100
+ for finder in meta_path[meta_path.index(self) + 1 :]:
101
+ find_spec = getattr(finder, "find_spec", None)
102
+ if find_spec is None:
103
+ continue
104
+ spec = find_spec(fullname, path, target)
105
+ if spec is None:
106
+ continue
107
+ # A legacy loader without `exec_module` loads without the callback.
108
+ if hasattr(spec.loader, "exec_module"):
109
+ spec.loader = _PostImportLoader(spec, self)
110
+ else:
111
+ self.remove()
112
+ return spec
113
+ return None
114
+
115
+ def remove(self) -> None:
116
+ # Replace the list rather than mutating it: the import system iterates
117
+ # `sys.meta_path` without a lock, and an in-place removal would make a
118
+ # concurrent import skip the finder after this one.
119
+ sys.meta_path = [finder for finder in sys.meta_path if finder is not self]
120
+
121
+
122
+ class _PostImportLoader:
123
+ """Delegates to a module's real loader, then runs its `_PostImportHook`."""
124
+
125
+ def __init__(self, spec, hook):
126
+ self.spec = spec
127
+ self.loader = spec.loader
128
+ self.hook = hook
129
+
130
+ def __getattr__(self, name):
131
+ return getattr(self.loader, name)
132
+
133
+ def create_module(self, spec):
134
+ return self.loader.create_module(spec)
135
+
136
+ def exec_module(self, module):
137
+ # The module body sees its real loader, and keeps it afterwards.
138
+ self.spec.loader = self.loader
139
+ if getattr(module, "__loader__", None) is self:
140
+ module.__loader__ = self.loader
141
+ self.loader.exec_module(module)
142
+ self.hook.remove()
143
+ self.hook.callback()
144
+
145
+
146
+ def _patch_torch() -> None:
147
+ """Make Torch classes subscriptable at runtime. Calling it again is a no-op."""
148
+
74
149
  try:
75
150
  import torch # @manual
76
151
  import torch.nn as nn # @manual
@@ -111,7 +186,50 @@ def _patch_torch_if_available() -> None:
111
186
  cls.__class_getitem__ = classmethod(_return_class)
112
187
 
113
188
 
114
- _patch_torch_if_available()
189
+ def _patch_torch_safely() -> None:
190
+ """Report incompatible Torch classes without breaking the Torch import."""
191
+
192
+ try:
193
+ _patch_torch()
194
+ except (AttributeError, TypeError) as error:
195
+ warnings.warn(
196
+ f"Could not patch torch for shape annotations: {error}",
197
+ RuntimeWarning,
198
+ stacklevel=2,
199
+ )
200
+
201
+
202
+ def _is_initializing(module) -> bool:
203
+ """Whether the import system is still executing `module`'s body."""
204
+
205
+ return getattr(getattr(module, "__spec__", None), "_initializing", False)
206
+
207
+
208
+ def _patch_torch_when_imported() -> None:
209
+ """Patch Torch now if it has been imported, and otherwise once it is.
210
+
211
+ Importing `shape_extensions` alone never imports Torch.
212
+ """
213
+
214
+ torch = sys.modules.get("torch")
215
+ if _is_initializing(torch):
216
+ # Another thread may be importing Torch, in which case `import torch`
217
+ # waits for that import to finish. If Torch's own initialization
218
+ # imported this module, it instead returns the same partially
219
+ # initialized module, which cannot be patched yet. A failed import
220
+ # leaves `sys.modules`, so the hook below patches a retried import.
221
+ with contextlib.suppress(ImportError):
222
+ import torch # @manual
223
+ torch = sys.modules.get("torch")
224
+ if _is_initializing(torch):
225
+ return
226
+ if torch is None:
227
+ sys.meta_path.insert(0, _PostImportHook("torch", _patch_torch_safely))
228
+ return
229
+ _patch_torch_safely()
230
+
231
+
232
+ _patch_torch_when_imported()
115
233
 
116
234
 
117
235
  def _patch_jax_if_available() -> None:
@@ -571,6 +689,51 @@ def broadcast(left: IntTuple, right: IntTuple) -> IntTuple:
571
689
  return gufunc_broadcast(spec, shapes)
572
690
 
573
691
 
692
+ class TypeVar:
693
+ """Runtime-safe legacy type variable for variadic shape dimensions.
694
+
695
+ This accepts the same arguments as ``typing.TypeVar`` on Python 3.12 and is
696
+ additionally iterable, so a shape variable can be unpacked as ``*S`` when
697
+ an annotation is evaluated at runtime.
698
+ """
699
+
700
+ __class__ = typing.TypeVar
701
+
702
+ def __init__(
703
+ self,
704
+ name: str,
705
+ *constraints,
706
+ bound=None,
707
+ covariant=False,
708
+ contravariant=False,
709
+ infer_variance=False,
710
+ ):
711
+ self.__name__ = name
712
+ self.__constraints__ = constraints
713
+ self.__bound__ = bound
714
+ self.__covariant__ = covariant
715
+ self.__contravariant__ = contravariant
716
+ self.__infer_variance__ = infer_variance
717
+
718
+ def __repr__(self):
719
+ return self.__name__
720
+
721
+ def __hash__(self):
722
+ return id(self)
723
+
724
+ def __eq__(self, other):
725
+ return self is other
726
+
727
+ def __iter__(self):
728
+ yield self
729
+
730
+ def __typing_subst__(self, arg):
731
+ return arg
732
+
733
+ def has_default(self):
734
+ return False
735
+
736
+
574
737
  class IntVar:
575
738
  """Symbolic variable with arithmetic support for tensor shape dimensions.
576
739
 
@@ -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