Skip to content

Add warning for PEP 604 when not in annotation #3937

@wookie184

Description

@wookie184

Ruff doesn't warn on this:

import typing

y = typing.Union[int, str]
z = typing.Optional[str]

This seems to be as a result of #3215, although I don't really understand the reasoning there, the same change is made if a type is used in an annotation, and as it can still be accessed at runtime it could still cause errors.

For example,

from typing import Union

new_types = (int, float)
def thing(arg: Union[new_types]):
    ...

becomes

new_types = (int, float)
def thing(arg: new_types):
    ...

(really the issue there seems to be invalid use of Union in the first place, afaik the only variables you can use there are type aliases, which that tuple is not)

This is also the case in the other issue linked, #3215, where as far as I can tell the issue happens whether the union is defined in a variable or directly in the annotation.

Anyway, I don't really mind too much about autofixes, but I think it would be nice to be able to have a warning for it regardless.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions