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 should leave type aliases alone #2981

Closed
aberres opened this issue Feb 17, 2023 · 4 comments · Fixed by #3217
Closed

UP007 should leave type aliases alone #2981

aberres opened this issue Feb 17, 2023 · 4 comments · Fixed by #3217
Labels
fixes Related to suggested fixes for violations

Comments

@aberres
Copy link

aberres commented Feb 17, 2023

Given the following snippet, ruff should not throw an error. At least this is the behavior of pyupgrade.

There are cases where one still needs an explicit Optional. See fastapi/typer#348 / fastapi/typer#348 (comment) for an example.

Code

from __future__ import annotations

from typing import Optional

StrOrNone = Optional[str]

Output

up007.py:6:13: UP007 [*] Use `X | Y` for type annotations
Found 1 error.
@charliermarsh
Copy link
Member

Ah that's interesting. I think we used to not change this (following pyupgrade), but I didn't see a good reason not to, and changed it. We can revert.

Note that pyupgrade will fix these cases:

from __future__ import annotations

from typing import List

StrOrNone = List[str]

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Feb 17, 2023
@twoertwein
Copy link

might be wrong: I think this was just a limitation of mypy<1.0

@aberres
Copy link
Author

aberres commented Feb 17, 2023

To have some more context, the use case for me is like this:

image

Without the indirection pyupgrade modernizes the code as expected, and typer fails.

@charliermarsh
Copy link
Member

Yeah that makes sense.

Is there any sense for when fastapi/typer#522 would be merged?

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants