pyrefly-shape-extensions 1.2.0.dev3__tar.gz → 1.3.0__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
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: pyrefly-shape-extensions
3
- Version: 1.2.0.dev3
3
+ Version: 1.3.0
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/
@@ -41,7 +41,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
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
43
  typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
44
- `assert_shape(x, (2, 3))` can be evaluated by Python while Pyrefly uses the
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
 
47
47
  The package is versioned in lockstep with Pyrefly.
@@ -5,7 +5,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
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
7
  typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
8
- `assert_shape(x, (2, 3))` can be evaluated by Python while Pyrefly uses the
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
 
11
11
  The package is versioned in lockstep with Pyrefly.
@@ -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.dev3"
11
+ version = "1.3.0"
12
12
  keywords = ["typechecker", "typechecking", "shapes", "tensor", "pyrefly"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -15,6 +15,26 @@ don't crash when evaluated by Python.
15
15
  import typing
16
16
  from dataclasses import dataclass
17
17
 
18
+ __all__ = [
19
+ "D",
20
+ "Elements",
21
+ "Int",
22
+ "IntTuple",
23
+ "IntTuples",
24
+ "IntVar",
25
+ "Index",
26
+ "MapIntTuples",
27
+ "ProxyMethod",
28
+ "SymbolicArithExpr",
29
+ "TypeVarTuple",
30
+ "assert_shape",
31
+ "broadcast",
32
+ "defines_assert_shape",
33
+ "gufunc_broadcast",
34
+ "index_shape",
35
+ "type_shape_dsl_function",
36
+ ]
37
+
18
38
 
19
39
  def _return_class(cls, params):
20
40
  return cls
@@ -68,12 +88,25 @@ def _patch_torch_if_available() -> None:
68
88
  _patch_torch_if_available()
69
89
 
70
90
 
91
+ def _patch_jax_if_available() -> None:
92
+ try:
93
+ import jax # @manual
94
+ except ImportError:
95
+ return
96
+
97
+ if hasattr(jax, "Array") and not hasattr(jax.Array, "__class_getitem__"):
98
+ jax.Array.__class_getitem__ = classmethod(_return_class)
99
+
100
+
101
+ _patch_jax_if_available()
102
+
103
+
71
104
  class IntTuple:
72
105
  """Tuple-valued shape annotation surface.
73
106
 
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.
107
+ In type positions, Pyrefly treats `IntTuple` as a whole shape whose runtime
108
+ representation is `tuple[int, ...]`. At runtime, calling it coerces any
109
+ iterable to a plain tuple.
77
110
  """
78
111
 
79
112
  def __new__(cls, iterable=()):
@@ -83,31 +116,38 @@ class IntTuple:
83
116
  return cls
84
117
 
85
118
 
119
+ class IntTuples:
120
+ """A tuple whose elements are `IntTuple` values."""
121
+
122
+ def __new__(cls, iterable=()):
123
+ return tuple(iterable)
124
+
125
+
86
126
  class Elements:
87
- """Inverse of ``tuple[Unpack[S]]``: extracts the element sequence from a IntTuple carrier.
127
+ """Inverse of ``tuple[Unpack[S]]``: extracts the dimensions from an ``IntTuple``.
88
128
 
89
129
  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,
130
+ concrete tuple type. ``Elements[S]`` is the conceptual inverse: given an ``IntTuple``
131
+ shape ``S``, ``*Elements[S]`` splices its dimensions into a shape position,
92
132
  e.g. ``Array[[*Elements[S], OUT], DType]``.
93
133
 
94
134
  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``
135
+ decompose a variadic shape without a ``TypeVarTuple``. Pyrefly uses the ``.pyi``
96
136
  stub for type inference; this class exists so annotations evaluate without crashing
97
137
  at runtime.
98
138
  """
99
139
 
100
- def __init__(self, carrier):
101
- self.carrier = carrier
140
+ def __init__(self, shape):
141
+ self.shape = shape
102
142
 
103
- def __class_getitem__(cls, carrier):
104
- return cls(carrier)
143
+ def __class_getitem__(cls, shape):
144
+ return cls(shape)
105
145
 
106
146
  def __iter__(self):
107
147
  yield self
108
148
 
109
149
  def __repr__(self):
110
- return f"Elements[{self.carrier!r}]"
150
+ return f"Elements[{self.shape!r}]"
111
151
 
112
152
 
113
153
  class Int[T]:
@@ -120,20 +160,22 @@ class Int[T]:
120
160
  pass
121
161
 
122
162
 
123
- class ProxyMethod[T]:
124
- """Type-checker marker for method forwarding annotations."""
163
+ class Flag[T]:
164
+ """Marker for a literal-preserving value that controls type-level evaluation."""
125
165
 
126
166
  pass
127
167
 
128
168
 
129
- def enable_torchscript_runtime_compat() -> None:
130
- """Erase shape-only runtime annotations to types TorchScript understands.
169
+ class Index:
170
+ """Marker for an index value retained for type-level shape evaluation."""
131
171
 
132
- This is a one-way, process-global compatibility mode for legacy TorchScript
133
- paths. It intentionally has no disable API for production callers.
134
- """
172
+ pass
135
173
 
136
- Int.__class_getitem__ = classmethod(_return_int)
174
+
175
+ class ProxyMethod[T]:
176
+ """Type-checker marker for method forwarding annotations."""
177
+
178
+ pass
137
179
 
138
180
 
139
181
  @dataclass(frozen=True)
@@ -213,54 +255,91 @@ def defines_assert_shape(fn: typing.Callable) -> typing.Callable:
213
255
  """
214
256
  Decorator that marks a function as an assert_shape helper.
215
257
 
216
- Used in order to allow custom assert_shape functions if necessary for
217
- different shape libraries; a default version that works for libraries
218
- like torch and numpy where `.shape` is a tuple field is defined in
219
- the `assert_shape` function of this library.
258
+ Used in order to allow custom assert_shape functions if necessary. A default
259
+ version that works for tuple-like shapes is defined in the `assert_shape`
260
+ function of this library.
220
261
  """
221
262
  return fn
222
263
 
223
264
 
224
265
  @defines_assert_shape
225
- def assert_shape(x, shape):
266
+ def assert_shape(actual, shape):
226
267
  """
227
- At runtime, assert that x has the expected runtime shape, assuming that `x` is
228
- some type like `torch.Tensor` or `np.ndarray` that has a tuple-valued
229
- field `shape` indicating the shape.
268
+ At runtime, assert that a tuple-like shape has the expected value.
230
269
 
231
- Pyrefly will validate that the shape modeled in Pyrefly's shaped array
232
- static analysis matches (similar to `assert_type`).
270
+ Pyrefly will validate that the statically modeled shape matches, similar to
271
+ `assert_type`.
233
272
 
234
273
  TODO(stroxler): for now, symbolic dimensions are skipped at runtime,
235
274
  so in the case of a symbolic `shape` the runtime validation is only checking
236
275
  the rank for those axes. But the static analysis will fully validate.
237
276
  """
238
277
 
239
- actual = tuple(x.shape)
278
+ # Preserve legacy calls that pass an array object rather than its shape.
279
+ if not isinstance(actual, tuple) and hasattr(actual, "shape"):
280
+ actual_tuple = tuple(actual.shape)
281
+ else:
282
+ actual_tuple = tuple(actual)
240
283
  expected = tuple(shape)
241
284
  if any(isinstance(dim, SymbolicArithExpr) for dim in expected):
242
- if len(actual) != len(expected):
285
+ if len(actual_tuple) != len(expected):
243
286
  raise AssertionError(
244
- f"expected rank {len(expected)} for shape {expected}, got shape {actual}"
287
+ f"expected rank {len(expected)} for shape {expected}, got shape {actual_tuple}"
245
288
  )
246
- elif actual != expected:
247
- raise AssertionError(f"expected shape {expected}, got {actual}")
248
- return x
289
+ elif actual_tuple != expected:
290
+ raise AssertionError(f"expected shape {expected}, got {actual_tuple}")
291
+ return actual
249
292
 
250
293
 
251
- def shaped_array(*, shape: str) -> typing.Callable[[type], type]:
252
- """Decorator that marks a class as carrying a shape TypeVarTuple."""
294
+ def index_shape(_shape: IntTuple, _index: typing.Any) -> IntTuple:
295
+ """Runtime placeholder for Pyrefly's native shape-indexing intrinsic."""
253
296
 
254
- def decorator(cls: type) -> type:
255
- return cls
297
+ return IntTuple()
256
298
 
257
- return decorator
258
299
 
300
+ class MapIntTuples:
301
+ """Map a unary type lambda over an ``IntTuples`` value.
259
302
 
260
- def broadcast(*_args):
261
- """Runtime placeholder for Pyrefly's native type-level broadcast intrinsic."""
303
+ A forward map preserves each source shape::
304
+
305
+ MapIntTuples[lambda S: Tensor[S], tuple[IntTuple[2], IntTuple[3, 4]]]
306
+
307
+ A map used directly as a parameter annotation reverses that relationship, so
308
+ passing ``(Tensor[IntTuple[2]], Tensor[IntTuple[3, 4]])`` infers the source
309
+ as ``tuple[IntTuple[2], IntTuple[3, 4]]``. Each symbolic source may occur in
310
+ only one parameter pattern, keeping inference unambiguous.
311
+
312
+ At runtime this is a placeholder that does not inspect its arguments,
313
+ because valid static sources such as ``Any`` and ``Never`` cannot be mapped
314
+ as Python values.
315
+ """
316
+
317
+ def __class_getitem__(cls, params):
318
+ return tuple
319
+
320
+
321
+ def type_shape_dsl_function[F: typing.Callable](fn: F) -> F:
322
+ """Runtime no-op for a user-defined type-level shape DSL function."""
323
+
324
+ return fn
262
325
 
263
- return IntTuple()
326
+
327
+ # `dsl` imports the public schema classes above, so defer this import until they exist.
328
+ from . import dsl as _dsl
329
+
330
+
331
+ @type_shape_dsl_function
332
+ def gufunc_broadcast(spec: str, shapes: IntTuples) -> IntTuple:
333
+ """Compute the output shape described by a generalized ufunc signature."""
334
+
335
+ return _dsl._gufunc_broadcast(spec, shapes)
336
+
337
+
338
+ @type_shape_dsl_function
339
+ def broadcast(left: IntTuple, right: IntTuple) -> IntTuple:
340
+ spec = "(),()->()"
341
+ shapes = _dsl.IntTuples((left, right))
342
+ return gufunc_broadcast(spec, shapes)
264
343
 
265
344
 
266
345
  class IntVar:
@@ -314,6 +393,9 @@ class IntVar:
314
393
  def __typing_subst__(self, arg):
315
394
  return arg
316
395
 
396
+ def has_default(self):
397
+ return False
398
+
317
399
 
318
400
  class TypeVarTuple:
319
401
  """TypeVarTuple with support for integer shape dimensions.
@@ -352,21 +434,5 @@ class TypeVarTuple:
352
434
  def __typing_is_unpacked_typevartuple__(self):
353
435
  return True
354
436
 
355
-
356
- def uses_shape_dsl(
357
- ir_fn: typing.Callable,
358
- *,
359
- capture_init: list[str] | None = None,
360
- ) -> typing.Callable[[typing.Callable], typing.Callable]:
361
- """Decorator that associates a shape DSL function with an API function.
362
-
363
- At runtime this is a no-op: the decorator arguments are ignored and the
364
- decorated function is returned unchanged. Pyrefly uses this decorator
365
- at type-checking time to route bound arguments through the shape DSL
366
- for return-type refinement.
367
- """
368
-
369
- def decorator(fn: typing.Callable) -> typing.Callable:
370
- return fn
371
-
372
- return decorator
437
+ def has_default(self):
438
+ return False
@@ -0,0 +1,89 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ # pyre-ignore-all-errors
7
+
8
+ """DSL internals for shape typing.
9
+
10
+ Only used inside DSL definition files (e.g. torch/_shapes.pyi), not in
11
+ normal stubs or user code.
12
+ """
13
+
14
+ # TODO(stroxler): Unquote `typing.TypeIs` when Python 3.13+ is the minimum version.
15
+ import typing
16
+
17
+ from . import (
18
+ Int as _IntSchema,
19
+ IntTuple as _IntTupleSchema,
20
+ IntTuples as _IntTuplesSchema,
21
+ )
22
+
23
+ # DSL builtins: these exist so that DSL definition files can import them
24
+ # and avoid unbound-name errors. The DSL compiler recognizes these names
25
+ # as builtins regardless of the Python-level definitions here.
26
+
27
+
28
+ def Invalid(message: str) -> typing.Any:
29
+ """Return an invalid shape computation from a type-shape DSL body."""
30
+
31
+ ...
32
+
33
+
34
+ class Int:
35
+ """Operations that produce values in the DSL integer domain."""
36
+
37
+ @staticmethod
38
+ def gradual() -> typing.Any: ...
39
+
40
+
41
+ class IntTuple(_IntTupleSchema):
42
+ """Operations that produce values in the DSL integer-tuple domain."""
43
+
44
+ def __new__(cls, values: typing.Iterable[typing.Any]) -> _IntTupleSchema:
45
+ return _IntTupleSchema(values)
46
+
47
+ @staticmethod
48
+ def gradual() -> typing.Any: ...
49
+
50
+
51
+ class IntTuples(_IntTuplesSchema):
52
+ """Operations that produce tuples of `IntTuple` values in the DSL."""
53
+
54
+ def __new__(cls, values: typing.Iterable[_IntTupleSchema]) -> _IntTuplesSchema:
55
+ return _IntTuplesSchema(values)
56
+
57
+ @staticmethod
58
+ def gradual() -> typing.Any: ...
59
+
60
+
61
+ # `TypeGuard`, not `TypeIs`: a false result does not narrow symbolic or gradual `Int` values.
62
+ def is_concrete_int(value: object) -> typing.TypeGuard[_IntSchema]: ...
63
+
64
+
65
+ def is_int_value(value: object) -> "typing.TypeIs[int]": ...
66
+
67
+
68
+ def concat(
69
+ left: typing.Iterable[typing.Any], right: typing.Iterable[typing.Any], /
70
+ ) -> _IntTupleSchema:
71
+ """Concatenate two shape values inside a type-shape DSL body."""
72
+
73
+ return _IntTupleSchema((*left, *right))
74
+
75
+
76
+ def prod(xs: _IntTupleSchema, /) -> _IntSchema: ...
77
+
78
+
79
+ def sum(xs: _IntTupleSchema, /) -> _IntSchema: ...
80
+
81
+
82
+ def einsum(spec: str, shapes: _IntTuplesSchema, /) -> _IntTupleSchema:
83
+ """Compute the output shape described by an explicit einsum equation."""
84
+ ...
85
+
86
+
87
+ def _gufunc_broadcast(spec: str, shapes: _IntTuplesSchema, /) -> _IntTupleSchema:
88
+ """Compute a gufunc result shape inside a type-level shape DSL function."""
89
+ return _IntTupleSchema()
@@ -0,0 +1,346 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ # pyre-ignore-all-errors
7
+
8
+ """Shape annotations with TorchScript-compatible runtime behavior enabled.
9
+
10
+ Importing this module erases shape-only runtime annotations to types
11
+ TorchScript understands. This must happen before any annotated class body is
12
+ evaluated, because TorchScript reads class attribute annotations back out of
13
+ `__annotations__`, so a call made after import would be too late.
14
+
15
+ `Int` here is the same class object as `shape_extensions.Int`, so the change is
16
+ process-global and one-way: it also affects code that imports `shape_extensions`
17
+ directly. There is intentionally no way to undo it.
18
+
19
+ Importing this module also patches TorchScript's source loading so that shape
20
+ annotations written in source, rather than read back from `__annotations__`,
21
+ are erased too. The two halves are not independently useful: a model needs both
22
+ to be scriptable.
23
+ """
24
+
25
+ from __future__ import annotations
26
+
27
+ import ast
28
+ import textwrap
29
+
30
+ from . import * # noqa: F401, F403
31
+ from . import __all__ as _shape_extensions_all, _return_int, Int as _Int
32
+
33
+ __all__ = [*_shape_extensions_all, "remove_shape_types_from_torch_sources"]
34
+
35
+ _Int.__class_getitem__ = classmethod(_return_int)
36
+
37
+
38
+ _SHAPE_TYPE_REPLACEMENTS = (
39
+ ("shape_extensions.torchscript.Int[", "int"),
40
+ ("shape_extensions.Int[", "int"),
41
+ ("torch.Tensor[", "torch.Tensor"),
42
+ ("Tensor[", "torch.Tensor"),
43
+ ("Int[", "int"),
44
+ )
45
+
46
+
47
+ def _shape_type_end(source: str, open_bracket: int) -> int | None:
48
+ depth = 0
49
+ for i in range(open_bracket, len(source)):
50
+ if source[i] == "[":
51
+ depth += 1
52
+ elif source[i] == "]":
53
+ depth -= 1
54
+ if depth == 0:
55
+ return i + 1
56
+ return None
57
+
58
+
59
+ def _is_identifier_part(char: str) -> bool:
60
+ return char == "_" or char.isalnum()
61
+
62
+
63
+ def _has_name_boundary(source: str, start: int) -> bool:
64
+ return start == 0 or (
65
+ source[start - 1] != "." and not _is_identifier_part(source[start - 1])
66
+ )
67
+
68
+
69
+ def _replace_shape_types(source: str) -> str:
70
+ rewritten: list[str] = []
71
+ i = 0
72
+ while i < len(source):
73
+ for prefix, replacement in _SHAPE_TYPE_REPLACEMENTS:
74
+ if source.startswith(prefix, i) and _has_name_boundary(source, i):
75
+ end = _shape_type_end(source, i + len(prefix) - 1)
76
+ if end is None:
77
+ continue
78
+ rewritten.append(replacement)
79
+ i = end
80
+ break
81
+ else:
82
+ rewritten.append(source[i])
83
+ i += 1
84
+ return "".join(rewritten)
85
+
86
+
87
+ def _annotation_nodes(tree: ast.AST):
88
+ for node in ast.walk(tree):
89
+ if isinstance(node, ast.arg) and node.annotation is not None:
90
+ yield node.annotation
91
+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
92
+ if node.returns is not None:
93
+ yield node.returns
94
+ elif isinstance(node, ast.AnnAssign):
95
+ yield node.annotation
96
+
97
+
98
+ def _source_line_offsets(source: str) -> list[int]:
99
+ offsets = [0]
100
+ for line in source.splitlines(keepends=True):
101
+ offsets.append(offsets[-1] + len(line))
102
+ return offsets
103
+
104
+
105
+ def _source_offset(
106
+ source: str, line_offsets: list[int], lineno: int, col_offset: int
107
+ ) -> int:
108
+ line_start = line_offsets[lineno - 1]
109
+ line_end = line_offsets[lineno] if lineno < len(line_offsets) else len(source)
110
+ line = source[line_start:line_end]
111
+ return line_start + len(line.encode("utf-8")[:col_offset].decode("utf-8"))
112
+
113
+
114
+ def _line_indent(source: str, start: int) -> str:
115
+ line_start = source.rfind("\n", 0, start) + 1
116
+ prefix = source[line_start:start]
117
+ return prefix[: len(prefix) - len(prefix.lstrip())]
118
+
119
+
120
+ def _preserve_line_count(source: str, replacement: str, indent: str) -> str:
121
+ missing_newlines = source.count("\n") - replacement.count("\n")
122
+ if missing_newlines <= 0:
123
+ return replacement
124
+
125
+ filler = [f"{indent}# shape annotation erased"] * (missing_newlines - 1)
126
+ return "\n".join([f"({replacement}", *filler, f"{indent})"])
127
+
128
+
129
+ def _is_type_checking_sentinel(test: ast.expr) -> bool:
130
+ """Recognize the typing sentinel, and only it.
131
+
132
+ `TYPE_CHECKING` and `typing.TYPE_CHECKING` are the spellings this erasure
133
+ supports. An attribute on anything else is not assumed to be the sentinel:
134
+ a project is free to have its own `TYPE_CHECKING` flag that is live at
135
+ runtime, and erasing that would delete real behavior. An aliased import
136
+ such as `import typing as t` is likewise not recognized, which errs toward
137
+ leaving code alone.
138
+ """
139
+
140
+ if isinstance(test, ast.Name):
141
+ return test.id == "TYPE_CHECKING"
142
+ return (
143
+ isinstance(test, ast.Attribute)
144
+ and test.attr == "TYPE_CHECKING"
145
+ and isinstance(test.value, ast.Name)
146
+ and test.value.id == "typing"
147
+ )
148
+
149
+
150
+ def _erase_type_checking_blocks(source: str) -> str:
151
+ """Replace `if TYPE_CHECKING:` blocks with `pass`, preserving line count.
152
+
153
+ TorchScript compiles both arms of an `if`, and cannot resolve
154
+ `TYPE_CHECKING` itself: it fails with "python value of type 'bool' cannot
155
+ be used as a value". Such a block is dead at runtime by construction, so
156
+ erasing it is safe, and it is what lets a scripted model carry static
157
+ `assert_type` checks on its shapes.
158
+
159
+ A block with an `else` is left alone: the `else` arm is the live one, and
160
+ rewriting it is beyond what this erasure is for.
161
+ """
162
+
163
+ tree = ast.parse(source)
164
+ line_offsets = _source_line_offsets(source)
165
+ spans: list[tuple[int, int]] = []
166
+ for node in ast.walk(tree):
167
+ if not isinstance(node, ast.If) or node.orelse:
168
+ continue
169
+ if not _is_type_checking_sentinel(node.test):
170
+ continue
171
+ if node.end_lineno is None or node.end_col_offset is None:
172
+ continue
173
+ spans.append(
174
+ (
175
+ _source_offset(source, line_offsets, node.lineno, node.col_offset),
176
+ _source_offset(
177
+ source, line_offsets, node.end_lineno, node.end_col_offset
178
+ ),
179
+ )
180
+ )
181
+
182
+ # Only the outermost of a set of nested blocks is erased. A nested block is
183
+ # already inside the text its enclosing block replaces, so keeping both
184
+ # would apply two edits to overlapping spans, and the second would be
185
+ # working from offsets the first had already invalidated.
186
+ outermost: list[tuple[int, int]] = []
187
+ for start, end in sorted(spans):
188
+ if outermost and start < outermost[-1][1]:
189
+ continue
190
+ outermost.append((start, end))
191
+
192
+ for start, end in reversed(outermost):
193
+ indent = _line_indent(source, start)
194
+ filler = [f"{indent}# type-checking block erased"] * source[start:end].count(
195
+ "\n"
196
+ )
197
+ replacement = "\n".join(["pass", *filler])
198
+ source = f"{source[:start]}{replacement}{source[end:]}"
199
+ return source
200
+
201
+
202
+ def _replace_shape_types_in_source(source: str) -> str:
203
+ try:
204
+ ast.parse(source)
205
+ except IndentationError:
206
+ source = textwrap.dedent(source)
207
+
208
+ # Erased first, in a separate pass, so that annotations inside these blocks
209
+ # are gone before annotation rewriting runs and cannot produce overlapping
210
+ # edits to the same span.
211
+ source = _erase_type_checking_blocks(source)
212
+ tree = ast.parse(source)
213
+
214
+ line_offsets = _source_line_offsets(source)
215
+ replacements: list[tuple[int, int, str]] = []
216
+ for node in _annotation_nodes(tree):
217
+ if (
218
+ node.end_lineno is None
219
+ or node.end_col_offset is None
220
+ or node.lineno is None
221
+ or node.col_offset is None
222
+ ):
223
+ continue
224
+ start = _source_offset(source, line_offsets, node.lineno, node.col_offset)
225
+ end = _source_offset(source, line_offsets, node.end_lineno, node.end_col_offset)
226
+ original = source[start:end]
227
+ if isinstance(node, ast.Constant) and isinstance(node.value, str):
228
+ # A wholly quoted annotation. Rewrite what the quotes contain and
229
+ # emit the result unquoted, rather than trying to consume the
230
+ # quotes while scanning: an annotation that continues past the
231
+ # shape type, such as `"Tensor[[B, D]] | None"`, would otherwise
232
+ # lose its opening quote and keep its closing one.
233
+ #
234
+ # Dropping the quotes is deliberate. TorchScript rejects a union
235
+ # written as a string but accepts the same union unquoted, so
236
+ # leaving the quotes on would turn a rewrite that parses into one
237
+ # that still fails to script. The cost is that a genuine forward
238
+ # reference inside a rewritten annotation is now evaluated eagerly.
239
+ # Compare against the contents rather than the quoted form, so an
240
+ # annotation with nothing to rewrite keeps its quotes.
241
+ rewritten = _replace_shape_types(node.value)
242
+ changed = rewritten != node.value
243
+ else:
244
+ rewritten = _replace_shape_types(original)
245
+ changed = rewritten != original
246
+ if changed:
247
+ replacements.append(
248
+ (
249
+ start,
250
+ end,
251
+ _preserve_line_count(
252
+ original, rewritten, _line_indent(source, start)
253
+ ),
254
+ )
255
+ )
256
+
257
+ for start, end, replacement in sorted(replacements, reverse=True):
258
+ source = f"{source[:start]}{replacement}{source[end:]}"
259
+ return source
260
+
261
+
262
+ class _ShapeTypingRemover:
263
+ def __init__(self, func) -> None:
264
+ self.func = func
265
+
266
+ def __call__(self, obj, error_msg=None):
267
+ source_lines, file_lineno, filename = self.func(obj, error_msg)
268
+ return (
269
+ _replace_shape_types_in_source("".join(source_lines)).splitlines(
270
+ keepends=True
271
+ ),
272
+ file_lineno,
273
+ filename,
274
+ )
275
+
276
+
277
+ class _ShapeAnnotationTypeRemover:
278
+ def __init__(self, func, torch) -> None:
279
+ self.func = func
280
+ self.torch = torch
281
+
282
+ def __call__(self, ann, *args, **kwargs):
283
+ if isinstance(ann, str):
284
+ rewritten = _replace_shape_types(ann)
285
+ if rewritten != ann:
286
+ rcb = kwargs.get("rcb", args[1] if len(args) > 1 else None)
287
+ annotations = self.torch.jit.annotations
288
+ # Resolved outside the try so that a rename or removal of these
289
+ # private torch APIs surfaces as an error here rather than being
290
+ # mistaken for an unresolvable annotation.
291
+ eval_no_call = annotations._eval_no_call
292
+ eval_env = annotations.EvalEnv(rcb)
293
+ try:
294
+ ann = eval_no_call(rewritten, {}, eval_env)
295
+ except (AttributeError, NameError, SyntaxError):
296
+ # Resolving the rewrite is best effort: it relies on private
297
+ # torch APIs, and on the rewritten text naming something
298
+ # resolvable in the caller's scope. Any failure means we
299
+ # cannot improve on the annotation we were given, so pass the
300
+ # original through and let TorchScript report it against the
301
+ # source the user wrote. Swallowing the error here would be
302
+ # wrong; TorchScript still rejects an annotation it cannot
303
+ # resolve, just with a better message than an eval traceback.
304
+ pass
305
+ return self.func(ann, *args, **kwargs)
306
+
307
+
308
+ def _is_shape_typing_remover(func) -> bool:
309
+ while func is not None:
310
+ if isinstance(func, _ShapeTypingRemover):
311
+ return True
312
+ func = getattr(func, "func", None)
313
+ return False
314
+
315
+
316
+ def _is_shape_annotation_type_remover(func) -> bool:
317
+ while func is not None:
318
+ if isinstance(func, _ShapeAnnotationTypeRemover):
319
+ return True
320
+ func = getattr(func, "func", None)
321
+ return False
322
+
323
+
324
+ def remove_shape_types_from_torch_sources() -> None:
325
+ """Patch TorchScript source loading to erase Pyrefly shape annotations."""
326
+
327
+ try:
328
+ import torch # @manual
329
+ except ImportError:
330
+ return
331
+
332
+ sources = torch._sources if hasattr(torch, "_sources") else torch.jit.frontend
333
+ if not _is_shape_typing_remover(sources.get_source_lines_and_file):
334
+ sources.get_source_lines_and_file = _ShapeTypingRemover(
335
+ sources.get_source_lines_and_file
336
+ )
337
+
338
+ annotations = torch.jit.annotations
339
+ if _is_shape_annotation_type_remover(annotations.ann_to_type):
340
+ return
341
+ annotations.ann_to_type = _ShapeAnnotationTypeRemover(
342
+ annotations.ann_to_type, torch
343
+ )
344
+
345
+
346
+ remove_shape_types_from_torch_sources()
@@ -1,74 +0,0 @@
1
- # Copyright (c) Meta Platforms, Inc. and affiliates.
2
- #
3
- # This source code is licensed under the MIT license found in the
4
- # LICENSE file in the root directory of this source tree.
5
-
6
- # pyre-ignore-all-errors
7
-
8
- """DSL internals for shape typing.
9
-
10
- Only used inside DSL definition files (e.g. torch/_shapes.pyi), not in
11
- normal stubs or user code.
12
- """
13
-
14
- import typing
15
-
16
-
17
- def shape_dsl_function(fn: typing.Callable) -> typing.Callable:
18
- """Marks a function as a shape DSL function.
19
-
20
- At runtime this is a no-op: the decorated function is returned unchanged.
21
- Pyrefly uses this decorator at type-checking time to convert the function
22
- body to DSL IR via convert_fndef.
23
- """
24
- return fn
25
-
26
-
27
- # DSL builtins: these exist so that DSL definition files can import them
28
- # and avoid unbound-name errors. The DSL compiler recognizes these names
29
- # as builtins regardless of the Python-level definitions here.
30
-
31
-
32
- class ShapedArray:
33
- """A shaped-array value in the DSL, constructed via ShapedArray(shape=[...])."""
34
-
35
- shape: list[int]
36
-
37
- def __init__(self, *, shape: list[int]) -> None:
38
- self.shape = shape
39
-
40
-
41
- class symint:
42
- """A symbolic integer dimension in the DSL."""
43
-
44
- ...
45
-
46
-
47
- class Error(Exception):
48
- """DSL error raised via `raise Error("message")`."""
49
-
50
- ...
51
-
52
-
53
- class _Unknown:
54
- """Sentinel returned from DSL functions to fall back to the declared return type."""
55
-
56
- ...
57
-
58
-
59
- Unknown: _Unknown = _Unknown()
60
-
61
-
62
- def prod(xs: list[int]) -> int:
63
- """Compute the product of a list of dimension sizes."""
64
- ...
65
-
66
-
67
- def sum(xs: list[int]) -> int:
68
- """Compute the sum of a list of dimension sizes."""
69
- ...
70
-
71
-
72
- def parse_einsum_equation(spec: str) -> list[list[list[int]]]:
73
- """Parse an einsum equation string into output map and check pairs."""
74
- ...