python/toml-without-tomllib
| Rule | toml-without-tomllib |
|---|---|
| Pack | Python |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
The standard library reads TOML since Python 3.11. Use tomllib in place of tomli or toml.load.
import tomlimport tomllib
def settings(path: Path) -> dict[str, object]: return toml.load(path) return tomllib.loads(path.read_text())What the rule reports
Section titled “What the rule reports”The rule reports an import of tomli, and toml.load and toml.loads. It leaves alone
toml.dump and toml.dumps, because tomllib cannot write, and an import of tomli inside
try or if, which is how code that still runs on Python 3.10 falls back.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
tomllib has been in the standard library
since Python 3.11, and every Python that still gets fixes has it. tomli is the package that
tomllib was copied from, with the same load and loads, so only the import changes.
toml.load takes a path or a file opened as text, and tomllib.load takes a file opened with
"rb", so pass the text of the file to tomllib.loads or open the file in binary mode.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
The standard library reads TOML. Write import tomllib, and call tomllib.load on a file opened with "rb" or tomllib.loads on text.Browse the other rules of the Python pack.