Skip to content

Commit a372ce2

Browse files
committed
packagedcode: replace unmaintained toml with tomllib/tomli
toml has been unmaintained for years and was superseded by tomli on PyPI which was eventually added to the stdlib as tomllib in Python 3.11. Signed-off-by: Maxwell G <[email protected]>
1 parent c9015f8 commit a372ce2

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Next release
1010
https://github.com/aboutcode-org/scancode-toolkit/pull/4474
1111
https://github.com/aboutcode-org/scancode-toolkit/issues/4101
1212

13+
- Replace unmaintained ``toml`` library with ``tomllib`` / ``tomli``.
14+
https://github.com/aboutcode-org/scancode-toolkit/issues/4532
1315

1416
v32.4.1 - 2025-07-23
1517
--------------------

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ install_requires =
110110
saneyaml >= 0.6.0
111111
spdx_tools == 0.8.2
112112
text_unidecode >= 1.0
113-
toml >= 0.10.0
113+
tomli >= 2; python_version < "3.11"
114114
urlpy
115115
xmltodict >= 0.11.0
116116
zipp >= 3.0.0; python_version < "3.9"

src/packagedcode/cargo.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
import re
1313
import sys
1414

15-
import toml
1615
from packageurl import PackageURL
1716

1817
from packagedcode import models
1918

19+
# tomli was added to the stdlib as tomllib in Python 3.11.
20+
# It's the same code.
21+
# Still, prefer tomli if it's installed, as on newer Python versions, it is
22+
# compiled with mypyc and is more performant.
23+
try:
24+
import tomli as tomllib
25+
except ImportError:
26+
import tomllib
27+
2028
"""
2129
Handle Rust cargo crates
2230
"""
@@ -170,7 +178,8 @@ class CargoTomlHandler(CargoBaseHandler):
170178

171179
@classmethod
172180
def parse(cls, location, package_only=False):
173-
package_data_toml = toml.load(location, _dict=dict)
181+
with open(location, "rb") as fp:
182+
package_data_toml = tomllib.load(fp)
174183
workspace = package_data_toml.get('workspace', {})
175184
core_package_data = package_data_toml.get('package', {})
176185
extra_data = {}
@@ -283,7 +292,8 @@ class CargoLockHandler(CargoBaseHandler):
283292

284293
@classmethod
285294
def parse(cls, location, package_only=False):
286-
cargo_lock = toml.load(location, _dict=dict)
295+
with open(location, "rb") as fp:
296+
cargo_lock = tomllib.load(fp)
287297
dependencies = []
288298
package = cargo_lock.get('package', [])
289299
for dep in package:

src/packagedcode/pypi.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import packvers as packaging
3030
import pip_requirements_parser
3131
import pkginfo2
32-
import toml
3332
from commoncode import fileutils
3433
from commoncode.fileutils import as_posixpath
3534
from commoncode.resource import Resource
@@ -46,6 +45,15 @@
4645
from packagedcode.utils import yield_dependencies_from_package_resource
4746
from packagedcode.utils import get_base_purl
4847

48+
# tomli was added to the stdlib as tomllib in Python 3.11.
49+
# It's the same code.
50+
# Still, prefer tomli if it's installed, as on newer Python versions, it is
51+
# compiled with mypyc and is more performant.
52+
try:
53+
import tomli as tomllib
54+
except ImportError:
55+
import tomllib
56+
4957
try:
5058
from zipfile import Path as ZipPath
5159
except ImportError:
@@ -463,7 +471,8 @@ def is_datafile(cls, location, filetypes=tuple()):
463471

464472
@classmethod
465473
def parse(cls, location, package_only=False):
466-
package_data = toml.load(location, _dict=dict)
474+
with open(location, "rb") as fp:
475+
package_data = tomllib.load(fp)
467476
project_data = package_data.get("project")
468477
if not project_data:
469478
return
@@ -647,7 +656,8 @@ def parse_non_group_dependencies(
647656

648657
@classmethod
649658
def parse(cls, location, package_only=False):
650-
toml_data = toml.load(location, _dict=dict)
659+
with open(location, "rb") as fp:
660+
toml_data = tomllib.load(fp)
651661

652662
tool_data = toml_data.get('tool')
653663
if not tool_data:
@@ -725,7 +735,8 @@ class PoetryLockHandler(BasePoetryPythonLayout):
725735

726736
@classmethod
727737
def parse(cls, location, package_only=False):
728-
toml_data = toml.load(location, _dict=dict)
738+
with open(location, "rb") as fp:
739+
toml_data = tomllib.load(fp)
729740

730741
packages = toml_data.get('package')
731742
if not packages:

0 commit comments

Comments
 (0)