Skip to content

[ty] Resolve type aliases in filter_union method#22989

Merged
carljm merged 1 commit intomainfrom
claude/explore-typealias-expansion-31ZUP
Jan 30, 2026
Merged

[ty] Resolve type aliases in filter_union method#22989
carljm merged 1 commit intomainfrom
claude/explore-typealias-expansion-31ZUP

Conversation

@carljm
Copy link
Contributor

@carljm carljm commented Jan 30, 2026

Summary

Update the filter_union method to resolve type aliases before checking if a type is a union. This ensures that union filtering works correctly when the type is a type alias that resolves to a union, not just direct union types.

Test Plan

Added tests exercising various uses of filter_union that fail without this fix.

@carljm carljm added ty Multi-file analysis & type inference ecosystem-analyzer labels Jan 30, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 30, 2026

Typing conformance results

No changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 30, 2026

mypy_primer results

Changes were detected when running on open source projects
prefect (https://github.com/PrefectHQ/prefect)
- src/integrations/prefect-dbt/prefect_dbt/core/settings.py:94:28: error[invalid-assignment] Object of type `dict[str, Any] | int | dict[Any, Any] | ... omitted 4 union elements` is not assignable to `dict[str, Any]`
- src/integrations/prefect-dbt/prefect_dbt/core/settings.py:99:28: error[invalid-assignment] Object of type `int | dict[Any, Any] | float | ... omitted 3 union elements` is not assignable to `dict[str, Any]`
- src/prefect/cli/deploy/_core.py:86:21: error[invalid-assignment] Object of type `dict[str, Any] | int | dict[Any, Any] | ... omitted 4 union elements` is not assignable to `dict[str, Any]`
- src/prefect/cli/deploy/_core.py:87:21: error[invalid-assignment] Object of type `int | dict[Any, Any] | float | ... omitted 3 union elements` is not assignable to `dict[str, Any]`
- src/prefect/deployments/steps/core.py:137:38: error[invalid-argument-type] Argument is incorrect: Argument type `dict[str, Any] | int | dict[Any, Any] | ... omitted 4 union elements` does not satisfy constraints (`str`, `int`, `int | float`, `bool`, `dict[Any, Any]`, `list[Any]`, `None`) of type variable `T`
- src/prefect/utilities/templating.py:320:13: error[invalid-assignment] Invalid subscript assignment with key of type `object` and value of type `dict[str, Any] | int | Unknown | ... omitted 4 union elements` on object of type `dict[str, Any]`
+ src/prefect/utilities/templating.py:320:13: error[invalid-assignment] Invalid subscript assignment with key of type `object` and value of type `Unknown | dict[str, Any]` on object of type `dict[str, Any]`
- src/prefect/utilities/templating.py:323:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_block_document_references | dict[str, Any]`, found `list[Unknown | dict[str, Any] | int | ... omitted 4 union elements]`
+ src/prefect/utilities/templating.py:323:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_block_document_references | dict[str, Any]`, found `list[Unknown | dict[str, Any]]`
- src/prefect/utilities/templating.py:437:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_variables`, found `dict[object, Unknown | int | float | ... omitted 4 union elements]`
+ src/prefect/utilities/templating.py:437:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_variables`, found `dict[object, Unknown]`
- src/prefect/utilities/templating.py:442:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_variables`, found `list[Unknown | int | float | ... omitted 4 union elements]`
+ src/prefect/utilities/templating.py:442:16: error[invalid-return-type] Return type does not match returned value: expected `T@resolve_variables`, found `list[Unknown]`
+ src/prefect/workers/base.py:232:13: error[invalid-argument-type] Argument is incorrect: Argument type `str | dict[str, Any]` does not satisfy constraints (`str`, `int`, `int | float`, `bool`, `dict[Any, Any]`, `list[Any]`, `None`) of type variable `T`
- src/prefect/workers/base.py:232:13: error[invalid-argument-type] Argument is incorrect: Argument type `dict[str, Any] | int | str | ... omitted 3 union elements` does not satisfy constraints (`str`, `int`, `int | float`, `bool`, `dict[Any, Any]`, `list[Any]`, `None`) of type variable `T`
- src/prefect/workers/base.py:234:22: error[invalid-argument-type] Argument expression after ** must be a mapping type: Found `int | Unknown | float | ... omitted 4 union elements`
- Found 5376 diagnostics
+ Found 5370 diagnostics

core (https://github.com/home-assistant/core)
- homeassistant/components/system_bridge/__init__.py:290:16: error[invalid-return-type] Return type does not match returned value: expected `ServiceResponse`, found `dict[Unknown | str, Unknown | int | list[dict[str, Any]]]`
+ homeassistant/components/system_bridge/__init__.py:290:16: error[invalid-return-type] Return type does not match returned value: expected `ServiceResponse`, found `dict[str, dict[str, dict[str, Any] | list[Any] | str | ... omitted 3 union elements] | list[dict[str, Any] | list[Any] | str | ... omitted 3 union elements] | str | ... omitted 4 union elements]`
- homeassistant/helpers/device_registry.py:1308:20: error[invalid-assignment] Object of type `dict[Unknown | str, Unknown | str]` is not assignable to `EventDeviceRegistryUpdatedData`
- homeassistant/helpers/device_registry.py:1310:20: error[invalid-assignment] Object of type `dict[Unknown | str, Unknown | str | dict[str, Any]]` is not assignable to `EventDeviceRegistryUpdatedData`
- Found 14515 diagnostics
+ Found 14513 diagnostics

No memory usage changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 30, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-assignment 0 6 2
invalid-parameter-default 0 0 7
invalid-argument-type 0 2 2
invalid-return-type 0 0 4
type-assertion-failure 0 2 0
Total 0 10 15

Full report with detailed diff (timing results)

This change modifies `filter_union` to resolve type aliases before
checking if the type is a union. This allows `filter_union` to properly
handle cases where a type alias resolves to a union type.

With this change, the explicit `resolve_type_alias` call in `bind.rs`
(added in #22988) is now redundant and has been removed.

Added additional test cases for union type aliases with:
- TypedDict dict literal inference
- TypedDict dict() call inference
- Set literal inference

https://claude.ai/code/session_01Fsp2dSw6mnTBV1M2Fnf6gt
@carljm carljm force-pushed the claude/explore-typealias-expansion-31ZUP branch from d6cc664 to 620916c Compare January 30, 2026 20:27
@carljm carljm requested a review from ibraheemdev January 30, 2026 20:32
@carljm carljm marked this pull request as ready for review January 30, 2026 20:32
@carljm
Copy link
Contributor Author

carljm commented Jan 30, 2026

@ibraheemdev You'd mentioned this might break the current constraint solver, but if so it doesn't seem to show up in tests or in ecosystem? Is there some particular breakage you were thinking of that I should test for?

@AlexWaygood AlexWaygood removed their request for review January 30, 2026 20:41
Copy link
Member

@ibraheemdev ibraheemdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that eagerly resolving type aliases would change our behavior in this example:

type X[T] = list[T] | None

def _(x: X[int]):
    reveal_type(x)  # revealed: `list[T] | None`

But it looks like we already reveal the value of the type alias instead of the type alias itself, so this seems fine.

@carljm carljm merged commit 216a48f into main Jan 30, 2026
49 checks passed
@carljm carljm deleted the claude/explore-typealias-expansion-31ZUP branch January 30, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants