[ty] Narrow tuple expression match subjects#25874
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 94.36%. The percentage of expected errors that received a diagnostic held steady at 88.82%. The number of fully passing files held steady at 93/134. |
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unresolved-attribute |
0 | 12 | 0 |
invalid-argument-type |
0 | 6 | 0 |
unsupported-operator |
0 | 2 | 0 |
invalid-assignment |
0 | 0 | 1 |
no-matching-overload |
0 | 1 | 0 |
| Total | 0 | 21 | 1 |
Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.
Raw diff (22 changes)
jax (https://github.com/google/jax)
- jax/experimental/mosaic/gpu/constraints.py:544:13 error[invalid-argument-type] Argument to function `splat_is_compatible_with_tiled` is incorrect: Expected `WGSplatFragLayout`, found `WGSplatFragLayout | WGStridedFragLayout | TiledLayout`
- jax/experimental/mosaic/gpu/constraints.py:544:28 error[invalid-argument-type] Argument to function `splat_is_compatible_with_tiled` is incorrect: Expected `TiledLayout`, found `WGSplatFragLayout | WGStridedFragLayout | TiledLayout`
- jax/experimental/mosaic/gpu/constraints.py:548:13 error[invalid-argument-type] Argument to function `_is_supported_tiled_relayout` is incorrect: Expected `TiledLayout`, found `WGSplatFragLayout | WGStridedFragLayout | TiledLayout`
- jax/experimental/mosaic/gpu/constraints.py:548:28 error[invalid-argument-type] Argument to function `_is_supported_tiled_relayout` is incorrect: Expected `TiledLayout`, found `WGSplatFragLayout | WGStridedFragLayout | TiledLayout`
kopf (https://github.com/nolar/kopf)
- kopf/_cogs/structs/references.py:305:45 error[no-matching-overload] No overload of bound method `Pattern.fullmatch` matches arguments
- kopf/_cogs/structs/references.py:306:24 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:307:55 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:308:53 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:309:54 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:310:45 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal["."]` and `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:311:51 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:312:54 error[unresolved-attribute] Attribute `split` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:315:42 error[unsupported-operator] Operator `in` is not supported between objects of type `Literal["/"]` and `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:316:51 error[unresolved-attribute] Attribute `rsplit` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
- kopf/_cogs/structs/references.py:317:53 error[unresolved-attribute] Attribute `rsplit` is not defined on `Marker`, `(Resource, /) -> bool`, `None` in union `str | Marker | ((Resource, /) -> bool) | None`
pylint (https://github.com/pycqa/pylint)
- pylint/checkers/utils.py:2123:20 error[unresolved-attribute] Attribute `name` is not defined on `AssignAttr` in union `AssignName | AssignAttr`
- pylint/checkers/utils.py:2123:35 error[unresolved-attribute] Object of type `NodeNG | None` has no attribute `name`
- pylint/checkers/utils.py:2125:42 error[unresolved-attribute] Attribute `as_string` is not defined on `None` in union `NodeNG | None`
- pylint/checkers/utils.py:2127:45 error[invalid-argument-type] Argument to function `subscript_chain_is_equal` is incorrect: Expected `Subscript`, found `AssignName | AssignAttr`
- pylint/checkers/utils.py:2127:53 error[invalid-argument-type] Argument to function `subscript_chain_is_equal` is incorrect: Expected `Subscript`, found `NodeNG | None`
pytest (https://github.com/pytest-dev/pytest)
- src/_pytest/assertion/rewrite.py:1127:21 error[unresolved-attribute] Object of type `expr` has no attribute `target`
- src/_pytest/assertion/rewrite.py:1128:21 error[invalid-assignment] Invalid subscript assignment with key of type `@Todo` and value of type `expr` on object of type `dict[str, str]`
+ src/_pytest/assertion/rewrite.py:1128:21 error[invalid-assignment] Invalid subscript assignment with key of type `@Todo` and value of type `NamedExpr` on object of type `dict[str, str]`ccd1df9 to
3a6f6dd
Compare
cb378a7 to
62e45a8
Compare
3ab5b1d to
0629acd
Compare
0629acd to
e6f936e
Compare
| PatternNarrowingResult::Impossible => return PatternNarrowingResult::Impossible, | ||
| PatternNarrowingResult::Possible(element_constraints) => { | ||
| constraints = | ||
| Self::merge_optional_constraints_and(constraints, element_constraints); |
There was a problem hiding this comment.
Codex highlighted that this can cause exponential expansion but the example that it provided where this would happen doesn't look as convincing:
class Base: ...
class A1(Base): ...
class A2(Base): ...
class B1(Base): ...
class B2(Base): ...
class C1(Base): ...
class C2(Base): ...
class D1(Base): ...
class D2(Base): ...
def f(x: Base) -> None:
match ([x], [x], [x], [x]):
case (
([A1()] | [A2()]),
([B1()] | [B2()]),
([C1()] | [C2()]),
([D1()] | [D2()]),
):
# Revealed type: `(A1 & B1 & C1 & D1) | (A1 & B1 & C1 & D2) | (A1 & B1 & C2 & D1) | (A1 & B1 & C2 & D2) | (A1 & B2 & C1 & D1) | (A1 & B2 & C1 & D2) | (A1 & B2 & C2 & D1) | (A1 & B2 & C2 & D2) | (A2 & B1 & C1 & D1) | (A2 & B1 & C1 & D2) | (A2 & B1 & C2 & D1) | (A2 & B1 & C2 & D2) | (A2 & B2 & C1 & D1) | (A2 & B2 & C1 & D2) | (A2 & B2 & C2 & D1) | (A2 & B2 & C2 & D2)`
reveal_type(x)The above example doesn't cause a performance regression but increasing the number of list in the subject expression and the number of unions in the case expression will do so.
It's not convincing because I don't think a real world code base would have such code which is why it might not be worth spending time here.
There was a problem hiding this comment.
I see a few reference to "projected" / "projection" which I'm not exactly sure what the meaning is in this context. Is it meant to be synonymous to narrowing?
9eb3211 to
8beeb84
Compare
Summary
Prior to this change, we narrowed a sequence value used as a match subject, but not the places used to construct an inline tuple or list display:
A match subject is evaluated once, so we retain the bindings read by its narrowable elements at subject evaluation. Successful sequence patterns recursively project their fixed prefix and suffix constraints through nested tuple and list displays onto names, attributes, and literal subscripts whose subject-time bindings are still live.
This supports later cases, starred patterns, repeated places, multiple reaching definitions, nested displays, and constrained
orpatterns without scanning cases for possible reassignments. Failed matches still do not project element constraints because they do not identify which element failed. Dictionary displays and starred subject displays remain conservative and are documented with TODO tests.When combining
orpatterns, we distinguish impossible alternatives from possible alternatives that do not constrain the subject. Impossible alternatives are omitted, while unconstrained alternatives correctly prevent the overall pattern from narrowing.Closes astral-sh/ty#3743.