Skip to content

Comments

[ty] Don't introduce invalid syntax when autofixing override-of-final-method#21699

Merged
AlexWaygood merged 2 commits intomainfrom
alex/delete-statement
Nov 30, 2025
Merged

[ty] Don't introduce invalid syntax when autofixing override-of-final-method#21699
AlexWaygood merged 2 commits intomainfrom
alex/delete-statement

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Nov 29, 2025

Summary

This didn't actually turn out to be too hard afterall.

  • If the overriding definition isn't a function, mark the autofix as display-only (we don't know what we're dealing with)
  • If any function overloads don't have the StmtClassDef node as their immediate parent in the AST (e.g. one or more overloads is inside a StmtIf node), mark the fix as display-only
  • If the overriding definition is the only statement in the class body, replace the overriding definition with pass rather than just deleting the range of the overriding definition

Fixes astral-sh/ty#1678

Test Plan

Updated snapshtos

@AlexWaygood AlexWaygood requested a review from carljm as a code owner November 29, 2025 22:50
@AlexWaygood AlexWaygood added the fixes Related to suggested fixes for violations label Nov 29, 2025
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Nov 29, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 29, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 29, 2025

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
- beartype/claw/_package/clawpkgtrie.py:66:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieBlacklist]'> | <class 'dict[str, Divergent]'>`
- beartype/claw/_package/clawpkgtrie.py:247:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieWhitelist]'> | <class 'dict[str, Divergent]'>`
- Found 498 diagnostics
+ Found 496 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`
+ pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, Divergent], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`

No memory usage changes detected ✅

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thank you

.contains(overload.node(db, context.file(), context.module()))
});

let applicability = if should_fix {
Copy link
Member

Choose a reason for hiding this comment

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

I'm leaning towards not showing the fix in that case because, I think, the is_only detection uses the wrong parent in that case and the more correct fix is to delete the entire outer statement, or the statement itself.

DisplayOnlyFixes are also never shown anywhere. They're just dead code

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, I assumed making it display-only was what you were asking for in #21646 (comment) (we don't have an Applicability::Manual :-)

But sure, I'll just get rid of the fix in these cases.

DisplayOnlyFixes are also never shown anywhere. They're just dead code

What's the purpose of Applicability::DisplayOnly if they aren't shown anywhere? I figured that if/when we show fixes in the CLI rendering of diagnostics, the diff would be helpful for users in telling them what we were asking for (you mentioned in #21646 that you found it confusing that the diagnostic was only omitted on one overload, but that to fix the diagnostic you actually had to remove all overloads).

Copy link
Member

Choose a reason for hiding this comment

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

The idea was to only show them in the CLI but we never made it that far. There were just too many design decisions.

@AlexWaygood AlexWaygood force-pushed the alex/delete-statement branch from 9f73442 to d99cfc5 Compare November 30, 2025 13:35
@AlexWaygood AlexWaygood enabled auto-merge (squash) November 30, 2025 13:37
@AlexWaygood AlexWaygood merged commit b02e821 into main Nov 30, 2025
40 checks passed
@AlexWaygood AlexWaygood deleted the alex/delete-statement branch November 30, 2025 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autofix for override-of-final-method can introduce invalid syntax

2 participants