Skip to content

[ty] Highlight declarations in invalid-assignment diagnostics - #27872

Merged
AlexWaygood merged 1 commit into
mainfrom
alex/annotate-invalid-assignment-declarations
Aug 19, 2026
Merged

AlexWaygood merged 1 commit into
mainfrom
alex/annotate-invalid-assignment-declarations

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Summary

When an invalid-assignment diagnostic is caused by a previously declared type, point directly to the annotation that established that type instead of only annotating the assignment target.

Example

value: int
value = "three"
error[invalid-assignment]: Object of type `Literal["three"]` is not assignable to `int`
 --> example.py:2:9
  |
1 | value: int
  |        --- Declared type
2 | value = "three"
  |         ^^^^^^^ Incompatible value of type `Literal["three"]`

@astral-sh-bot astral-sh-bot Bot added the ty The ty type checker label Aug 19, 2026
@AlexWaygood AlexWaygood added the diagnostics Diagnostic messages, annotations, suggested fixes, and rendering infrastructure label Aug 19, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.68%. The percentage of expected errors that received a diagnostic held steady at 93.44%. The number of fully passing files held steady at 109/136.

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@AlexWaygood
AlexWaygood marked this pull request as ready for review August 19, 2026 11:52
@AlexWaygood
AlexWaygood requested a review from a team as a code owner August 19, 2026 11:52
@astral-sh-bot
astral-sh-bot Bot requested a review from charliermarsh August 19, 2026 11:52
x: int = 1

def assign() -> None:
nonlocal x

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Worth testing global too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

already tested a few lines above!

@AlexWaygood
AlexWaygood removed the request for review from charliermarsh August 19, 2026 16:58
@AlexWaygood
AlexWaygood merged commit 708a952 into main Aug 19, 2026
104 of 106 checks passed
@AlexWaygood
AlexWaygood deleted the alex/annotate-invalid-assignment-declarations branch August 19, 2026 18:02
AlexWaygood added a commit that referenced this pull request Aug 19, 2026
(Stacked on top of #27872)

## Summary

Add a disabled-by-default `unsound-assignment` rule for variable
assignments whose inferred value is assignable to a fully static
declared type but is not a subtype of it. Attribute and subscript
assignments are deliberately excluded from this initial implementation.

Share declaration lookup and diagnostic rendering with
`invalid-assignment` so the rule points to the annotation responsible
for the expected type, including parameters, variadics, global/nonlocal
bindings, and augmented assignments.

Fixes astral-sh/ty#4289.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Diagnostic messages, annotations, suggested fixes, and rendering infrastructure ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants