[ty] Nominal Tagged Union Narrowing#24916
Conversation
|
@ibraheemdev No rush, but can I get a review for this? |
|
Thanks for working on this. It will probably be a few days before a review. Some of us are out this week, and all of us are traveling next week. But we try to get a look as soon as possible. |
ibraheemdev
left a comment
There was a problem hiding this comment.
This looks solid! Just a few comments.
8e2f3ef to
552db98
Compare
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 91.94%. The percentage of expected errors that received a diagnostic held steady at 87.09%. The number of fully passing files held steady at 92/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unresolved-attribute |
0 | 9 | 16 |
redundant-cast |
4 | 0 | 0 |
invalid-argument-type |
0 | 2 | 0 |
invalid-assignment |
0 | 0 | 1 |
| Total | 4 | 11 | 17 |
Raw diff (32 changes)
core (https://github.com/home-assistant/core)
+ homeassistant/components/ssdp/common.py:18:41 warning[redundant-cast] Value is already of type `IPv6Address`
- homeassistant/components/anthropic/entity.py:1066:44 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/anthropic/entity.py:1080:53 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/conversation/chat_log.py:91:16 error[unresolved-attribute] Attribute `content` is not defined on `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/conversation/chat_log.py:365:17 error[unresolved-attribute] Attribute `content` is not defined on `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/conversation/chat_log.py:366:17 error[unresolved-attribute] Attribute `content` is not defined on `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/google_generative_ai_conversation/entity.py:549:37 error[invalid-argument-type] Argument to bound method `list.append` is incorrect: Expected `ToolResultContent`, found `Content`
- homeassistant/components/google_generative_ai_conversation/entity.py:567:46 error[invalid-argument-type] Argument to function `_convert_content` is incorrect: Expected `UserContent | AssistantContent | SystemContent`, found `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/open_router/entity.py:271:44 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/open_router/entity.py:279:49 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/openai_conversation/entity.py:636:44 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
- homeassistant/components/openai_conversation/entity.py:639:49 error[unresolved-attribute] Attribute `attachments` is not defined on `SystemContent`, `AssistantContent`, `ToolResultContent` in union `SystemContent | UserContent | AssistantContent | ToolResultContent`
+ homeassistant/components/shelly/utils.py:243:13 warning[redundant-cast] Value is already of type `IPv4Address`
+ homeassistant/components/ssdp/scanner.py:261:29 warning[redundant-cast] Value is already of type `IPv6Address`
+ homeassistant/components/ssdp/server.py:177:33 warning[redundant-cast] Value is already of type `IPv6Address`
meson (https://github.com/mesonbuild/meson)
- run_project_tests.py:1314:13 error[invalid-assignment] Object of type `str` is not assignable to attribute `msg` on type `TestResult | None`
+ run_project_tests.py:1314:13 error[invalid-assignment] Object of type `str` is not assignable to attribute `msg` on type `Unknown | TestResult | None`
- run_project_tests.py:1317:41 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1317:41 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1318:64 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1318:64 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1321:14 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1321:14 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1323:41 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1323:41 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1324:41 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1324:41 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1338:46 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1338:46 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1338:76 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1338:76 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1339:16 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1339:16 error[unresolved-attribute] Attribute `step` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1339:55 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1339:55 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1343:37 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1343:37 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1348:37 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1348:37 error[unresolved-attribute] Attribute `mlog` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1349:37 error[unresolved-attribute] Attribute `stdo` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1349:37 error[unresolved-attribute] Attribute `stdo` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1351:37 error[unresolved-attribute] Attribute `stdo` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1351:37 error[unresolved-attribute] Attribute `stdo` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1352:28 error[unresolved-attribute] Attribute `cicmds` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1352:28 error[unresolved-attribute] Attribute `cicmds` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1354:33 error[unresolved-attribute] Attribute `stde` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1354:33 error[unresolved-attribute] Attribute `stde` is not defined on `None` in union `Unknown | TestResult | None`
- run_project_tests.py:1360:64 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `TestResult | None`
+ run_project_tests.py:1360:64 error[unresolved-attribute] Attribute `msg` is not defined on `None` in union `Unknown | TestResult | None`|
might be interesting to compare this with #23116, which I believe was attempting the same thing |
552db98 to
a61f79c
Compare
|
@AlexWaygood Yeah, unfortunately I only noticed that PR after opening this. That one additionally covers is/is not with singletons and uses a stricter equality pre-check (won't narrow at all if any union member has a non-literal tag type). This PR preserves non-literal arms during positive narrowing and gates attribute place tracking by operator. |
a61f79c to
01d2027
Compare
sharkdp
left a comment
There was a problem hiding this comment.
This looks good, thank you.
I don't think the is/is not narrowing that the other PR has is as important, and this PR has other advantages (e.g. supports yolo conditions). If someone feels the is narrowing is important, then we can do that later.
Merging this PR will improve performance by 4.5%
Performance Changes
Tip Curious why this is faster? Use the CodSpeed MCP and ask your agent. Comparing Footnotes
|
* main: [`pydocstyle`] Improve discoverability of rules enabled for each convention (#24973) [ty] Deduplicate retained use-def place states (#25450) [ty] reduce features of low-level crates depended on by `ty_python_semantic` (#25524) [ty] Fix narrowing enum literal unions by member identity (#25520) [ty] Test tagged union narrowing for named tuples (#25519) [ty] Disallow file-system access in `ty_python_core` (#25518) [ty] Nominal Tagged Union Narrowing (#24916) Commit `scripts/uv.lock` (#25517) Fix potential index out of range in `LineIndex` computation (#25492) [ty] Sync vendored typeshed stubs (#25514) [ty] Add disjointness for protocol method members (#25315) [ty] Use compact sets for more immutable fields (#25476) [ty] Derive `Default` for `FunctionDecoratorInference` (#25482) [ty] Ignore rejected assignments for synthesized bindings (#25340) [ty] Handle cycles in function decorator inference (#25475) docs: fix typo `bin/active` → `bin/activate` in tutorial (#25473) [ty] Narrow bound method overloads by receiver (#24707)
Summary
Adds narrowing for tagged unions of nominal class instances based on literal attribute comparisons. Addresses part of astral-sh/ty#1479. Related to astral-sh/ty#2897.
This allows ty to narrow code such as
x.tag == "a","a" == x.tag,x.tag != "a", andmatch x.tagwhenxis a union of classes whose tag attributes are annotated with supported literal types.To keep the performance impact small, the new place tracking is gated by comparison operator. We only add base places for attribute/subscript expressions when the operator can produce a relevant narrowing constraint, which avoids extra Salsa narrowing queries for unrelated comparisons.
Test Plan