Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UP007: autofix does not work for some examples #4843

Open
saippuakauppias opened this issue Jun 3, 2023 · 2 comments
Open

UP007: autofix does not work for some examples #4843

saippuakauppias opened this issue Jun 3, 2023 · 2 comments
Labels
fixes Related to suggested fixes for violations needs-decision Awaiting a decision from a maintainer

Comments

@saippuakauppias
Copy link

Example:

from typing import Any, Dict, Optional, Type, Union


# 1
DataType = Optional[Dict[str, Any]]


# 2
class StatusedModel:
    ACTIVE: str = 'active'
    BLOCKED: str = 'blocked'
    DELETED: str = 'deleted'


StatusType = Union[StatusedModel.ACTIVE, StatusedModel.BLOCKED, StatusedModel.DELETED]


# 3
BALANCE_ZERO = 'zero'
BALANCE_LOW = 'low'
ModeType = Union[BALANCE_LOW, BALANCE_ZERO]


# 4
Numeric = Union[int, float, str]


# 5
IdType = Union[int, Type[int]]

Run output:

$ ruff --fix --select UP007 ruff_errors/UP007.py
ruff_errors/UP007.py:5:12: UP007 Use `X | Y` for type annotations
ruff_errors/UP007.py:15:14: UP007 Use `X | Y` for type annotations
ruff_errors/UP007.py:21:12: UP007 Use `X | Y` for type annotations
ruff_errors/UP007.py:25:11: UP007 Use `X | Y` for type annotations
ruff_errors/UP007.py:29:10: UP007 Use `X | Y` for type annotations
Found 5 errors.
@charliermarsh
Copy link
Member

charliermarsh commented Jun 3, 2023

These are intentional, right now we don't autofix UP007 errors that are used outside of annotations (e.g., on the right-hand side of an assignment). (pyupgrade doesn't either.)

Its caused some subtle bugs in the past: #3215, #2981. It was also discussed here: #4108.

It's possible that it's actually fine to fix these, with the exception that we can't rewrite Union[new_types] to new_types. I'm not certain.

@charliermarsh charliermarsh added question Asking for support or clarification fixes Related to suggested fixes for violations labels Jun 3, 2023
@charliermarsh charliermarsh added needs-decision Awaiting a decision from a maintainer and removed question Asking for support or clarification labels Jul 10, 2023
@Hnasar
Copy link
Contributor

Hnasar commented Apr 12, 2024

It would be very helpful to autofix the PEP604 | unions, but have it gated behind an unsafe fix.
I'm using --add-noqa as a workaround for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

No branches or pull requests

4 participants