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

Missing pyupgrade errors (UP006, UP035) #8698

Closed
rademacher-p opened this issue Nov 15, 2023 · 8 comments
Closed

Missing pyupgrade errors (UP006, UP035) #8698

rademacher-p opened this issue Nov 15, 2023 · 8 comments
Assignees
Labels
fixes Related to suggested fixes for violations

Comments

@rademacher-p
Copy link

Hi, I seem to be missing pyupgrade errors for Python 3.11 and Ruff 0.1.5. Reproduce w/ ruff --isolated --select UP file.py on a fresh venv. This is all from the CLI, no pyproject.toml.

# file.py
from typing import Type

foo = Type[list]

I ❤️ ruff, thanks in advance for any help!

@charliermarsh
Copy link
Member

Thank you for the kind words!

I think this is a result of #3215 -- we stopped rewriting annotations that are used at runtime (roughly, on the right-hand side of an expression) because it can change behavior. #4843 has some more information. I think we actually can rewrite these in some but not all scenarios. In other words, our current approach is probably too conservative.

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Nov 15, 2023
@charliermarsh
Copy link
Member

I think it's always safe to perform these rewrites for UP006 (like List to list, assuming you're on a supported Python version).

It's probably not always safe to perform these rewrites for UP007 (which converts Union[int, str] to int | str).

@NeilGirdhar
Copy link

NeilGirdhar commented Nov 15, 2023

I think you should write this as a type alias:

foo: TypeAlias = Type[list]  # Python < 3.12
type foo = type[list]  # Python >= 3.12

And then would it be possible for Ruff to make the transformation? Are right-hand-sides of type aliases okay?

@rademacher-p
Copy link
Author

Aside from the issues with autofix, is there a reason not to provide the deprecation inspection?

@charliermarsh
Copy link
Member

The only reason is to ensure we're not introducing false positives. So it'd be nice to make sure we have a sense for where this is and isn't safe prior to enabling the check for runtime annotations.

@charliermarsh charliermarsh self-assigned this Nov 16, 2023
@charliermarsh
Copy link
Member

Oh hmm, I'm actually noticing that if your Python version is set correctly, we do flag and fix this: https://play.ruff.rs/526e6c64-b8da-4cd5-8dcd-68ee71a6f886

Do you have your --target-version configured to Python 3.11?

@rademacher-p
Copy link
Author

@charliermarsh No, I did not! Should have had that in my config (actually, should just up my project.requires-python) 😮 Adding this option gives me the pyupgrade errors - feel free to close, if you're ready 👍

@charliermarsh
Copy link
Member

Awesome, thanks @rademacher-p! Sorry for the confusion but glad to resolve.

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

No branches or pull requests

3 participants