Skip to content

UP007 rule is not catching type aliases defined without the TypeAlias annotation #4108

@ericbn

Description

@ericbn

Hi! 👋

The UP007 rule is not catching the scenarios where a type alias is defined without the TypeAlias annotation, which is likely to happen when upgrading from py39 to py310.

test.py

Minimal code snipped that reproduces the bug:

from typing import Literal, Optional, TypeAlias, Union

YesNo = Literal["yes", "no"]
OptionalYesNo = Optional[YesNo]
OptionalBlankYesNo = Union[OptionalYesNo, Literal[""]]
OptionalYesNoAlias: TypeAlias = Optional[YesNo]
OptionalBlankYesNoAlias: TypeAlias = Union[OptionalYesNo, Literal[""]]

Ruff output

The command I've invoked: ruff --isolated --select UP --target-version py310 test.py

test.py:6:33: UP007 [*] Use `X | Y` for type annotations
test.py:7:38: UP007 [*] Use `X | Y` for type annotations
Found 2 errors.
[*] 2 potentially fixable with the --fix option.

I was expecting UP007 to have reported lines 4 and 5 too. When using --fix I was expecting the fixed code to be:

from typing import Literal, TypeAlias

YesNo = Literal["yes", "no"]
OptionalYesNo = YesNo | None
OptionalBlankYesNo = OptionalYesNo | Literal[""]
OptionalYesNoAlias: TypeAlias = YesNo | None
OptionalBlankYesNoAlias: TypeAlias = OptionalYesNo | Literal[""]

ruff --version

ruff 0.0.262

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions