Skip to content

[ty] Use field type context for TypedDict constructor values#24422

Open
charliermarsh wants to merge 1 commit intomainfrom
charlie/td-spec
Open

[ty] Use field type context for TypedDict constructor values#24422
charliermarsh wants to merge 1 commit intomainfrom
charlie/td-spec

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

This PR adds logic to re-infer TypedDict constructor values with the destination field's declared type context, which prevents us from rejecting the constructor in:

from __future__ import annotations

from typing import Any, NotRequired, TypeAlias, TypedDict


class Comparison(TypedDict):
    field: str
    op: NotRequired[str]
    value: Any

class Logical(TypedDict):
    op: NotRequired[str]
    conditions: list[Filter]

Filter: TypeAlias = Comparison | Logical

logical = Logical(conditions=[Comparison(field='a', value='b')])

#23936 solves this with a broader rewrite of TypedDict construction. The change here is intentionally more narrow.

Closes astral-sh/ty#3027.

@astral-sh-bot astral-sh-bot bot added the ty Multi-file analysis & type inference label Apr 5, 2026
@charliermarsh charliermarsh added the bug Something isn't working label Apr 5, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.72%. The percentage of expected errors that received a diagnostic held steady at 82.85%. The number of fully passing files held steady at 74/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-await 0 40 0
invalid-argument-type 0 6 14
invalid-return-type 0 1 0
Total 0 47 14

Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.

Raw diff (20 changes)
core (https://github.com/home-assistant/core)
- homeassistant/components/adguard/entity.py:63:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, Unknown, Unknown]]`
+ homeassistant/components/adguard/entity.py:63:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, Unknown, Unknown]]`
- homeassistant/components/azure_devops/entity.py:23:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str, Unknown]]`
+ homeassistant/components/azure_devops/entity.py:23:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, str, Unknown]]`
- homeassistant/components/bond/entity.py:83:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown | str | None, Unknown | str]]`
+ homeassistant/components/bond/entity.py:83:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown | str | None, Unknown | str]]`
- homeassistant/components/hvv_departures/binary_sensor.py:144:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, Unknown, Unknown]]`
+ homeassistant/components/hvv_departures/binary_sensor.py:144:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, Unknown, Unknown]]`
- homeassistant/components/hvv_departures/sensor.py:79:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, Unknown, Unknown]]`
+ homeassistant/components/hvv_departures/sensor.py:79:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, Unknown, Unknown]]`
- homeassistant/components/izone/climate.py:479:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str, Unknown]]`
+ homeassistant/components/izone/climate.py:479:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, str, Unknown]]`
- homeassistant/components/met_eireann/weather.py:99:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str]]`
+ homeassistant/components/met_eireann/weather.py:99:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str]]`
- homeassistant/components/toon/entity.py:43:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:43:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/toon/entity.py:63:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:63:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/toon/entity.py:83:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:83:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/toon/entity.py:103:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:103:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/toon/entity.py:124:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:124:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/toon/entity.py:144:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, str]]`
+ homeassistant/components/toon/entity.py:144:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, str]]`
- homeassistant/components/vilfo/sensor.py:75:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, Unknown, Unknown]]`
+ homeassistant/components/vilfo/sensor.py:75:25 error[invalid-argument-type] Invalid argument to key "identifiers" with declared type `set[tuple[str, str]]` on TypedDict `DeviceInfo`: value of type `set[tuple[str, str] | tuple[str, Unknown, Unknown]]`

zulip (https://github.com/zulip/zulip)
- zerver/webhooks/fixtureless_integrations.py:98:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`
- zerver/webhooks/fixtureless_integrations.py:128:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`
- zerver/webhooks/fixtureless_integrations.py:155:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`
- zerver/webhooks/fixtureless_integrations.py:166:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`
- zerver/webhooks/fixtureless_integrations.py:189:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`
- zerver/webhooks/fixtureless_integrations.py:263:13 error[invalid-argument-type] Invalid argument to key "content" with declared type `str | list[MessageThread]` on TypedDict `ScreenshotContent`: value of type `list[dict[str, str]]`

Full report with detailed diff (timing results)

@charliermarsh charliermarsh marked this pull request as ready for review April 5, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use type context from synthesized TypedDict constructor when inferring arguments passed to that constructor

2 participants