Skip to content

Guard TOML config read when tomli/tomllib is unavailable (NameError on Python < 3.11) - #371

Merged
weibullguy merged 1 commit into
PyCQA:masterfrom
fudianchn:fix-tomllib-nameerror-py310
Aug 10, 2026
Merged

Guard TOML config read when tomli/tomllib is unavailable (NameError on Python < 3.11)#371
weibullguy merged 1 commit into
PyCQA:masterfrom
fudianchn:fix-tomllib-nameerror-py310

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

Problem

configuration.py imports tomllib (or tomli as tomllib) under contextlib.suppress(ImportError), then calls tomllib.load unconditionally. On Python < 3.11 without the tomli backport, the import fails silently, leaving tomllib unbound, so any run in a project with a pyproject.toml crashes (#368):

config = tomllib.load(f)
NameError: name 'tomllib' is not defined

Python < 3.11 is a supported version (pyproject.toml declares python = "^3.10"). 1.7.7 guarded this with a TOMLI_INSTALLED check; the 1.7.8 refactor dropped it.

Fix

Use a try/except that binds tomllib = None when neither the stdlib tomllib nor the tomli backport is importable, and skip reading the TOML configuration in that case — matching 1.7.7, which ran cleanly on the same interpreter — instead of crashing.

-with contextlib.suppress(ImportError):
+try:
     if sys.version_info >= (3, 11):
         import tomllib
     else:
         import tomli as tomllib
+except ImportError:
+    tomllib = None
     def _do_read_toml_configuration(self) -> None:
         """Load configuration information from a *.toml file."""
+        if tomllib is None:
+            return
         with open(self.config_file, "rb") as f:
             config = tomllib.load(f)

Also drops the now-unused contextlib import. When tomllib is available (Python ≥ 3.11, or < 3.11 with tomli), behaviour is unchanged.

Verification

Reproduced in docker python:3.10 (no stdlib tomllib, no tomli installed) with a pyproject.toml containing [tool.docformatter]:

WITHOUT the fix:  NameError: name 'tomllib' is not defined   (exit 1)
WITH    the fix:  docformatter runs cleanly                   (exit 0)

black --check passes on the changed file.

Closes #368

configuration.py imported tomllib (or tomli as tomllib) under
contextlib.suppress(ImportError), then used tomllib.load unconditionally.
On Python < 3.11 without the tomli backport, the import fails silently,
leaving tomllib unbound, so any run with a pyproject.toml crashes with
NameError: name 'tomllib' is not defined (PyCQA#368). 1.7.7 guarded this with
a TOMLI_INSTALLED check; the 1.7.8 refactor dropped it.

Use a try/except that binds tomllib to None when neither is available, and
skip reading the TOML configuration in that case (matching 1.7.7, which ran
cleanly on the same interpreter) instead of crashing. Drops the now-unused
contextlib import.

Closes PyCQA#368
@weibullguy
weibullguy merged commit 4347165 into PyCQA:master Aug 10, 2026
6 of 7 checks passed
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

Successfully merging this pull request may close these issues.

1.7.8 raises NameError: name 'tomllib' is not defined on Python 3.10

2 participants