pyrefly-shape-extensions 1.2.0.dev1__tar.gz → 1.2.0.dev3__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.4
2
2
  Name: pyrefly-shape-extensions
3
- Version: 1.2.0.dev1
3
+ Version: 1.2.0.dev3
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]`, `TypeVar("B")`, and
43
+ typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
44
44
  `assert_shape(x, (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]`, `TypeVar("B")`, and
7
+ typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
8
8
  `assert_shape(x, (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.12"
10
10
  license = { file = "LICENSE" }
11
- version = "1.2.0.dev1"
11
+ version = "1.2.0.dev3"
12
12
  keywords = ["typechecker", "typechecking", "shapes", "tensor", "pyrefly"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -68,7 +68,49 @@ def _patch_torch_if_available() -> None:
68
68
  _patch_torch_if_available()
69
69
 
70
70
 
71
- class Dim[T]:
71
+ class IntTuple:
72
+ """Tuple-valued shape annotation surface.
73
+
74
+ In type positions, Pyrefly treats `IntTuple` as the shape carrier for
75
+ `tuple[int, ...]`. At runtime, calling it coerces any iterable to a plain
76
+ tuple.
77
+ """
78
+
79
+ def __new__(cls, iterable=()):
80
+ return tuple(iterable)
81
+
82
+ def __class_getitem__(cls, params):
83
+ return cls
84
+
85
+
86
+ class Elements:
87
+ """Inverse of ``tuple[Unpack[S]]``: extracts the element sequence from a IntTuple carrier.
88
+
89
+ In the Python typing spec, ``tuple[Unpack[Ts]]`` wraps a ``TypeVarTuple`` into a
90
+ concrete tuple type. ``Elements[S]`` is the conceptual inverse: given a ``IntTuple``
91
+ carrier ``S``, ``*Elements[S]`` splices its element sequence into a shape position,
92
+ e.g. ``Array[[*Elements[S], OUT], DType]``.
93
+
94
+ This fills a gap in the current typing spec — there is no standard mechanism to
95
+ decompose a variadic carrier without a ``TypeVarTuple``. Pyrefly uses the ``.pyi``
96
+ stub for type inference; this class exists so annotations evaluate without crashing
97
+ at runtime.
98
+ """
99
+
100
+ def __init__(self, carrier):
101
+ self.carrier = carrier
102
+
103
+ def __class_getitem__(cls, carrier):
104
+ return cls(carrier)
105
+
106
+ def __iter__(self):
107
+ yield self
108
+
109
+ def __repr__(self):
110
+ return f"Elements[{self.carrier!r}]"
111
+
112
+
113
+ class Int[T]:
72
114
  """Symbolic integer type for dimension values.
73
115
 
74
116
  At runtime this is a no-op generic class. The type checker uses the
@@ -78,6 +120,12 @@ class Dim[T]:
78
120
  pass
79
121
 
80
122
 
123
+ class ProxyMethod[T]:
124
+ """Type-checker marker for method forwarding annotations."""
125
+
126
+ pass
127
+
128
+
81
129
  def enable_torchscript_runtime_compat() -> None:
82
130
  """Erase shape-only runtime annotations to types TorchScript understands.
83
131
 
@@ -85,7 +133,7 @@ def enable_torchscript_runtime_compat() -> None:
85
133
  paths. It intentionally has no disable API for production callers.
86
134
  """
87
135
 
88
- Dim.__class_getitem__ = classmethod(_return_int)
136
+ Int.__class_getitem__ = classmethod(_return_int)
89
137
 
90
138
 
91
139
  @dataclass(frozen=True)
@@ -209,23 +257,29 @@ def shaped_array(*, shape: str) -> typing.Callable[[type], type]:
209
257
  return decorator
210
258
 
211
259
 
212
- class TypeVar:
213
- """TypeVar with arithmetic support for tensor shape dimensions.
260
+ def broadcast(*_args):
261
+ """Runtime placeholder for Pyrefly's native type-level broadcast intrinsic."""
262
+
263
+ return IntTuple()
264
+
265
+
266
+ class IntVar:
267
+ """Symbolic variable with arithmetic support for tensor shape dimensions.
214
268
 
215
269
  Like typing.TypeVar but arithmetic operations (N + 1, N * 2, etc.)
216
270
  return self instead of raising TypeError. Setting
217
271
  __class__ = typing.TypeVar makes isinstance(x, typing.TypeVar)
218
272
  return True, so Generic[N] and TypedDict + Generic[N] both work.
219
273
 
220
- In pyrefly, shape_extensions.TypeVar is treated identically to
221
- typing.TypeVar.
274
+ In pyrefly, shape_extensions.IntVar marks symbolic integer dimensions.
222
275
  """
223
276
 
224
277
  __class__ = typing.TypeVar
225
278
 
226
- def __init__(self, name: str):
279
+ def __init__(self, name: str, *, bound=None):
227
280
  self.__name__ = name
228
281
  self.name = name
282
+ self.__bound__ = bound
229
283
 
230
284
  def __repr__(self):
231
285
  return self.name