Skip to content

Commit

Permalink
Use the modern tomli/tomllib to parse TOML files
Browse files Browse the repository at this point in the history
Replace the unmaintained `toml` package with the modern combination
of built-in `tomllib` in Python 3.11+, and its drop-in replacement
`tomli` for older Python versions.  This provides full and correct
support for TOML 1.0 that `toml` does not provide.  Unfortunately, they
do not support overriding dict class but that does not seem to be
a major problem, given that TOML tables are not ordered according
to the specification.
  • Loading branch information
mgorny committed Oct 28, 2022
1 parent 91bf9f6 commit 31a1d5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
15 changes: 10 additions & 5 deletions dparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from collections import OrderedDict
import re
import sys

from io import StringIO

Expand All @@ -17,11 +18,15 @@
from packaging.requirements import Requirement as PackagingRequirement,\
InvalidRequirement
from . import filetypes
import toml
from packaging.specifiers import SpecifierSet
from packaging.version import Version, InvalidVersion
import json

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


# this is a backport from setuptools 26.1
def setuptools_parse_requirements_backport(strs): # pragma: no cover
Expand Down Expand Up @@ -356,7 +361,7 @@ def parse(self):
:return:
"""
try:
data = toml.loads(self.obj.content, _dict=OrderedDict)
data = tomllib.loads(self.obj.content)
if data:
for package_type in ['packages', 'dev-packages']:
if package_type in data:
Expand All @@ -374,7 +379,7 @@ def parse(self):
section=package_type
)
)
except (toml.TomlDecodeError, IndexError):
except (tomllib.TOMLDecodeError, IndexError):
pass


Expand Down Expand Up @@ -443,7 +448,7 @@ def parse(self):
Parse a poetry.lock
"""
try:
data = toml.loads(self.obj.content, _dict=OrderedDict)
data = tomllib.loads(self.obj.content)
pkg_key = 'package'
if data:
try:
Expand Down Expand Up @@ -471,7 +476,7 @@ def parse(self):
section=section
)
)
except (toml.TomlDecodeError, IndexError) as e:
except (tomllib.TOMLDecodeError, IndexError) as e:
raise MalformedDependencyFileError(info=str(e))


Expand Down
9 changes: 7 additions & 2 deletions dparse/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import re
import json
import tempfile
import toml
import os
import sys

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


class RequirementsTXTUpdater(object):
Expand Down Expand Up @@ -79,7 +84,7 @@ class SetupCFGUpdater(CondaYMLUpdater):
class PipfileUpdater(object):
@classmethod
def update(cls, content, dependency, version, spec="==", hashes=()):
data = toml.loads(content)
data = tomllib.loads(content)
if data:
for package_type in ['packages', 'dev-packages']:
if package_type in data:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

requirements = [
"packaging",
"toml",
"tomli; python_version < '3.11'",
]

setup(
Expand Down
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pyyaml
pytest
pytest-cov
codecov
toml
tomli; python_version < "3.11"
packaging

0 comments on commit 31a1d5b

Please sign in to comment.