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

F821 false positive when using ExceptionGroup #13255

Closed
iurii-skorniakov opened this issue Sep 5, 2024 · 11 comments
Closed

F821 false positive when using ExceptionGroup #13255

iurii-skorniakov opened this issue Sep 5, 2024 · 11 comments

Comments

@iurii-skorniakov
Copy link

version: ruff 0.6.4, Python 3.11.6
command: ruff check --isolated example.py

when using built-in ExceptionGroup

class MyGroup(ExceptionGroup):
    def derive(self, excs):
        return MyGroup(self.message, excs)

ruff raises F821

example.py:1:15: F821 Undefined name `ExceptionGroup`
  |
1 | class MyGroup(ExceptionGroup):
  |               ^^^^^^^^^^^^^^ F821
2 |     def derive(self, excs):
3 |         return MyGroup(self.message, excs)
  |
@g3rv4
Copy link

g3rv4 commented Sep 5, 2024

seems like a regression from #3167

@charliermarsh
Copy link
Member

Do you have a minimum Python version set, in your pyproject.toml or via --target-version? Otherwise, Ruff defaults to Python 3.8, and ExceptionGroup doesn't exist in that version.

@dhruvmanila dhruvmanila added the needs-info More information is needed from the issue author label Sep 6, 2024
@bonastreyair
Copy link

bonastreyair commented Sep 6, 2024

maybe it is another issue but for me it fails finding anext when using python3.11
it was working fine with previous version but now it fails because of that

I think the problem comes from here: https://github.com/astral-sh/ruff/pull/13172/files/de081b67c3072829c21cf8176a96baef64793ba7#diff-a5297ef4a78229f14881c57e472a6a79dc17e6a5a9244a676dfa6276d10f0dab

anext was before and now it is not present in that same list: https://github.com/astral-sh/ruff/pull/13172/files/de081b67c3072829c21cf8176a96baef64793ba7#r1746582057

related PR: #13172

@MichaReiser
Copy link
Member

MichaReiser commented Sep 6, 2024

@bonastreyair could you share your configuration? anext is still in the builtins list but only when targeting Py310 or newer.

  • Py38 -> builtin doesn't exist
  • Py311 -> no error

You may need to make a whitespace change for the errors to show up

@bartfeenstra
Copy link

I started having similar problems, also with anext() and aiter().

Project.toml: https://github.com/bartfeenstra/betty/blob/0.4.x/pyproject.toml
Ruff failures: https://github.com/bartfeenstra/betty/actions/runs/10734022531/job/29768422099

Let me know if you need more details :)

@iurii-skorniakov
Copy link
Author

Found my problem.
I had incorrect configuration in subdirectory, which didn't extend root config.
With correct configuration it works well
Thank you for your help!

@dhruvmanila dhruvmanila removed the needs-info More information is needed from the issue author label Sep 6, 2024
@bartfeenstra
Copy link

Can we reopen this? I still have this problem, with a single root configuration file:

[lint]
extend-select = [
    'A',
    'B',
    'C4',
    'D',
    'F',
    'INT',
    'PT',
    'PTH',
    'SIM',
    'SLOT',
    'T20',
    'TCH',
]
ignore = [
    'D105',
    'D107',
    'D200',
    'D203',
    'D212',
    'D401',
    'F501',
    'PTH123',
    'SIM103',
]

[lint.per-file-ignores]
"betty/tests/*" = [
    'D100',
    'D101',
    'D102',
    'D103',
    'D104',
    'D106',
]

@MichaReiser
Copy link
Member

The fix is to specify the requires-python setting to a version that supports ExceptionGroup.

@bartfeenstra
Copy link

requires-python = '~= 3.11' in https://github.com/bartfeenstra/betty/blob/0.4.x/pyproject.toml, yet Ruff fails on ExceptionGroup, aiter, anext... I tried a simpler constraint of requires-python = '>= 3.11' but that did not seem to change Ruff's output.

@MichaReiser
Copy link
Member

@bartfeenstra I don't think ruff picks up the requires-python constraint if you also have a .ruff.toml because the .ruff.toml takes precedence. You have to explicitly set the target-version in the ruff.toml. The alternative is to move the ruff configuration into the pyroject.toml.

@bartfeenstra
Copy link

That makes sense! I'll give that a try shortly. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants