Skip to content

Rule request: detect redundant type unions #18546

@robsdedude

Description

@robsdedude

Summary

Inspired by #18508

I'd like to see ruff detect (and possibly fix) redundant types in unions. Examples:

import typing

foo1: A | B | A  # becomes A | B
foo2: typing.Union[int, B, float, int, C]  # becomes Union[int, B, float, C]
foo3: typing.Optional[None]  # becomes None

For simplicity, I'd be fine leaving out nested annotations in the first iteration. They could still be added at a later time. E.g.,

import typing

foo: typing.Union[int, None, typing.Optional[int]]  # could become typing.Union[int, None]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions