pyrefly-shape-extensions 1.1.1__tar.gz → 1.2.0.dev2__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.1.1
3
+ Version: 1.2.0.dev2
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]`, `SymVar("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]`, `SymVar("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.1.1"
11
+ version = "1.2.0.dev2"
12
12
  keywords = ["typechecker", "typechecking", "shapes", "tensor", "pyrefly"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -16,6 +16,14 @@ import typing
16
16
  from dataclasses import dataclass
17
17
 
18
18
 
19
+ def _return_class(cls, params):
20
+ return cls
21
+
22
+
23
+ def _return_int(cls, params):
24
+ return int
25
+
26
+
19
27
  def _patch_torch_if_available() -> None:
20
28
  try:
21
29
  import torch # @manual
@@ -54,12 +62,50 @@ def _patch_torch_if_available() -> None:
54
62
  ]
55
63
  for cls in subscriptable_classes:
56
64
  if not hasattr(cls, "__class_getitem__"):
57
- cls.__class_getitem__ = classmethod(lambda cls, params: cls)
65
+ cls.__class_getitem__ = classmethod(_return_class)
58
66
 
59
67
 
60
68
  _patch_torch_if_available()
61
69
 
62
70
 
71
+ class SizeTuple:
72
+ """Integer tuple carrier for shape-like type parameters.
73
+
74
+ At runtime this is a no-op marker class. Pyrefly treats `SizeTuple` in type
75
+ positions as the shape carrier corresponding to `tuple[int, ...]`.
76
+ """
77
+
78
+ def __class_getitem__(cls, params):
79
+ return cls
80
+
81
+
82
+ class Elements:
83
+ """Inverse of ``tuple[Unpack[S]]``: extracts the element sequence from a SizeTuple carrier.
84
+
85
+ In the Python typing spec, ``tuple[Unpack[Ts]]`` wraps a ``TypeVarTuple`` into a
86
+ concrete tuple type. ``Elements[S]`` is the conceptual inverse: given a ``SizeTuple``
87
+ carrier ``S``, ``*Elements[S]`` splices its element sequence into a shape position,
88
+ e.g. ``Array[[*Elements[S], OUT], DType]``.
89
+
90
+ This fills a gap in the current typing spec — there is no standard mechanism to
91
+ decompose a variadic carrier without a ``TypeVarTuple``. Pyrefly uses the ``.pyi``
92
+ stub for type inference; this class exists so annotations evaluate without crashing
93
+ at runtime.
94
+ """
95
+
96
+ def __init__(self, carrier):
97
+ self.carrier = carrier
98
+
99
+ def __class_getitem__(cls, carrier):
100
+ return cls(carrier)
101
+
102
+ def __iter__(self):
103
+ yield self
104
+
105
+ def __repr__(self):
106
+ return f"Elements[{self.carrier!r}]"
107
+
108
+
63
109
  class Dim[T]:
64
110
  """Symbolic integer type for dimension values.
65
111
 
@@ -70,6 +116,22 @@ class Dim[T]:
70
116
  pass
71
117
 
72
118
 
119
+ class ProxyMethod[T]:
120
+ """Type-checker marker for method forwarding annotations."""
121
+
122
+ pass
123
+
124
+
125
+ def enable_torchscript_runtime_compat() -> None:
126
+ """Erase shape-only runtime annotations to types TorchScript understands.
127
+
128
+ This is a one-way, process-global compatibility mode for legacy TorchScript
129
+ paths. It intentionally has no disable API for production callers.
130
+ """
131
+
132
+ Dim.__class_getitem__ = classmethod(_return_int)
133
+
134
+
73
135
  @dataclass(frozen=True)
74
136
  class SymbolicArithExpr:
75
137
  """Runtime representation of symbolic dimension arithmetic."""
@@ -191,23 +253,24 @@ def shaped_array(*, shape: str) -> typing.Callable[[type], type]:
191
253
  return decorator
192
254
 
193
255
 
194
- class TypeVar:
195
- """TypeVar with arithmetic support for tensor shape dimensions.
256
+ class SymVar:
257
+ """Symbolic variable with arithmetic support for tensor shape dimensions.
196
258
 
197
259
  Like typing.TypeVar but arithmetic operations (N + 1, N * 2, etc.)
198
260
  return self instead of raising TypeError. Setting
199
261
  __class__ = typing.TypeVar makes isinstance(x, typing.TypeVar)
200
262
  return True, so Generic[N] and TypedDict + Generic[N] both work.
201
263
 
202
- In pyrefly, shape_extensions.TypeVar is treated identically to
264
+ In pyrefly, shape_extensions.SymVar is treated identically to
203
265
  typing.TypeVar.
204
266
  """
205
267
 
206
268
  __class__ = typing.TypeVar
207
269
 
208
- def __init__(self, name: str):
270
+ def __init__(self, name: str, *, bound=None):
209
271
  self.__name__ = name
210
272
  self.name = name
273
+ self.__bound__ = bound
211
274
 
212
275
  def __repr__(self):
213
276
  return self.name