Ruff "Undefined name ExceptionGroup" error with Python 3.12 and Poetry
ruff check fails with:
F821 Undefined name `ExceptionGroup`. Consider specifying `requires-python = ">= 3.11"` or `tool.ruff.target-version = "py311"` in your `pyproject.toml` file....on a project whose pyproject.toml already says the Python version is 3.12:
[tool.poetry.dependencies]
python = ">=3.12,<3.13"The code runs fine and the tests pass; only ruff (and therefore pre-commit / CI lint) objects. Same failure applies to any 3.11+ builtin, e.g. BaseExceptionGroup.
ruff infers the target version from PEP 621 project.requires-python. Poetry projects using the legacy [tool.poetry.dependencies] python = "..." layout have no [project] table at all, so ruff sees nothing and falls back to its default (py39), where ExceptionGroup genuinely does not exist.
Fix the config rather than the code — add an explicit target version:
[tool.ruff]
target-version = "py312"Put it in the pyproject.toml ruff will discover for those files (the per-package one in a monorepo, not necessarily the repo root).
Notes:
- Safe by default:
target-versionmainly drives pyupgrade (UP) and typing rules. If you are on ruff's default rule set (E4,E7,E9,F), raising it fixes the false positive without surfacing new errors. If you haveUPenabled, re-runruff checkafter the change and expect legitimate new suggestions. - Alternative if you are migrating anyway: add a PEP 621
[project]table withrequires-python = ">=3.12". Poetry 2.x supports this and ruff will then infer correctly with no ruff-specific config. - Do NOT work around it in the source (importing
ExceptionGroupfrom somewhere,# noqa: F821, or avoiding the builtin). The lint config is what is wrong.