ruff icon indicating copy to clipboard operation
ruff copied to clipboard

[ruff] Implement union-with-annotated-type (RUF066)

Open wilt00 opened this issue 2 months ago • 4 comments

Summary

This PR implements a new rule, nested-annotated-type, as discussed in #21037 . This rule fails for type definitions including Annotated nested within some other type.

Test Plan

New snapshosts in RUF066.py with cargo insta test.

wilt00 avatar Oct 26 '25 03:10 wilt00

I think this rule makes sense to me (catching Annotated[...] | ...) and I'd be in favor adding it because it catches a very likely mistake. But I'd be curious to hear @amyreese's opinion, too, just to ensure I'm not overlooking any valid use cases for this.

I do think we should come up with a less cryptic name :). We certainly can't use the nested terminology because the annotated documentation uses that to refer to Annotated[Annotated] nesting

Nested Annotated types are flattened. The order of the metadata elements starts with the innermost annotation: https://docs.python.org/3/library/typing.html#typing.Annotated

Maybe something like type-operation-with-annotated or mixed-annotated-regular-type?

MichaReiser avatar Oct 30 '25 13:10 MichaReiser

I agree that it's a useful rule — I'm not aware of any case where you would want to use an Annotated type with anything, let alone nest Annotated inside of anything other than another Annotated type.

For names I prefer mixed-annotated-regular-type over the other suggestion, but I might instead name it union-with-annotated-type

amyreese avatar Oct 30 '25 22:10 amyreese

I'll leave the main review to @amyreese, but this looks reasonable overall to me. Would you mind re-numbering the rule to RUF068? We have existing PRs for RUF066 and RUF067.

ntBre avatar Nov 05 '25 22:11 ntBre

Hey, I see this PR has not had any movement in the last month. If you don't mind, I think I am going to pick it up. I will include the original commits (rebased with main) and update based on the included comments. I hope I am not stepping on your toes with this.

CoderJoshDK avatar Dec 16 '25 16:12 CoderJoshDK