Guard TOML config read when tomli/tomllib is unavailable (NameError on Python < 3.11) - #371
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
configuration.pyimportstomllib(ortomliastomllib) undercontextlib.suppress(ImportError), then callstomllib.loadunconditionally. On Python < 3.11 without thetomlibackport, the import fails silently, leavingtomllibunbound, so any run in a project with apyproject.tomlcrashes (#368):Python < 3.11 is a supported version (
pyproject.tomldeclarespython = "^3.10"). 1.7.7 guarded this with aTOMLI_INSTALLEDcheck; the 1.7.8 refactor dropped it.Fix
Use a
try/exceptthat bindstomllib = Nonewhen neither the stdlibtomllibnor thetomlibackport 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.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
contextlibimport. Whentomllibis available (Python ≥ 3.11, or< 3.11withtomli), behaviour is unchanged.Verification
Reproduced in
docker python:3.10(no stdlibtomllib, notomliinstalled) with apyproject.tomlcontaining[tool.docformatter]:black --checkpasses on the changed file.Closes #368