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

A004 Preview Mode ExceptionGroup import in pre-3.11 #13037

Closed
CoolCat467 opened this issue Aug 21, 2024 · 3 comments · Fixed by #13172
Closed

A004 Preview Mode ExceptionGroup import in pre-3.11 #13037

CoolCat467 opened this issue Aug 21, 2024 · 3 comments · Fixed by #13172
Assignees
Labels
accepted Ready for implementation preview Related to preview mode features rule Implementing or modifying a lint rule

Comments

@CoolCat467
Copy link

CoolCat467 commented Aug 21, 2024

Getting A004 Import BaseExceptionGroup is shadowing a Python builtin and ExceptionGroup errors raised in this case:

if sys.version_info < (3, 11):
    from exceptiongroup import BaseExceptionGroup, ExceptionGroup

I would argue that this should not be the case. BaseExceptionGroup and ExceptionGroup do not exist in python versions prior to 3.11, and using the exceptiongroup package is commonly used for backwards compatibility.

Using ruff preview mode v0.6.1

CoolCat467 added a commit to CoolCat467/trio that referenced this issue Aug 22, 2024
I feel like this is a bug, so astral-sh/ruff#13037
is tracking this.
@MichaReiser
Copy link
Member

MichaReiser commented Aug 22, 2024

This makes sense to me, but we should only allow the use of BaseExceptionGroup and ExceptionGroup when inside a sys.version_info < (3.11) branch. I'm not sure if we have the necessary infrastructure today. wdyt @AlexWaygood ?

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Aug 22, 2024
@AlexWaygood
Copy link
Member

I'm quite hesistant to add the infrastructure to Ruff to support detecting which sys.version_info branch we're in. Generalised sys.version_info parsing would be tricky (though we'll have to implement it for red-knot to comply with PEP 484), and I'm not sure it's actually needed in this case. For this case, I think we could fix the issue by making the is_python_builtin() function smarter:

  • The rule calls shadows_builtin() here.
  • shadows_builtin() calls is_python_builtin() here
  • is_python_builtin() is defined here. Unlike other similar functions in the ruff_python_stdlib crate, it assumes that all Python builtins have always been Python builtins; it doesn't take account of the fact that some builtins were added in newer Python versions. If we added a minor_version parameter, similar to ruff_python_stdlib::sys::is_builtin_module or ruff_python_stdlib::sys::is_known_standard_library, then we could take account of the --target-version the user has provided when deciding whether something is a Python builtin or not. Since @CoolCat467 clearly supports running Python <3.11 on their code, they'll probably be running Ruff with --target-version py310 or lower.

@CoolCat467
Copy link
Author

I think that using --target-version is a good solution

@MichaReiser MichaReiser added the accepted Ready for implementation label Aug 22, 2024
@dhruvmanila dhruvmanila added the preview Related to preview mode features label Aug 23, 2024
@charliermarsh charliermarsh self-assigned this Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants