Skip to content

docs: fix safety of UP007 should be clarified #17062

@pjonsson

Description

@pjonsson

Summary

This pyproject.yaml:

[tool.ruff]
target-version = "py310"

[tool.ruff.lint]
select = ["UP"]

and running ruff check --fix file.py on this example:

from typing import Optional

def a() -> Optional[str]:
    return None

results in:

from typing import Optional

def a() -> str | None:
    return None

so Ruff has updated the type signature, but left the import.

Trying to figure out if my expectations are wrong, the documentation at https://docs.astral.sh/ruff/rules/non-pep604-annotation-union/#fix-safety says:

This rule's fix is marked as unsafe, as it may lead to runtime errors when alongside libraries that rely on runtime type annotations, like Pydantic, on Python versions prior to Python 3.10.

which I believe is contradictory to being able to use --fix, unsafe rules require enabling some other switch.

Edit: the phrasing "it may lead to runtime errors when alongside libraries" is also confusing to me; what is alongside the libraries, the fix?

Version

ruff 0.11.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions