Skip to content

Commit

Permalink
Switch from 'toml' package to tomli/tomllib
Browse files Browse the repository at this point in the history
Python 3.11 comes with built-in support for TOML. It's an almost direct
import of the tomli package. As the 'toml' library is unmaintained, and
tomli has the same interface as what's in the standard library, this
switches the TOML support to a supported library with a minimum of fuss.

Tomllib requires files to be opened in 'rb' mode, so the relevant tests
had to be updated accordingly.
  • Loading branch information
mwgamble authored Jan 12, 2023
1 parent a246dd4 commit 978e205
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_abs_path(pathname):
install_requires=[
"flake8 >= 2.0.0",
"setuptools >= 10.0.0",
"toml >= 0.7.0",
"tomli>=1.2.1; python_version < '3.11'",
],
setup_requires=["pytest-runner"],
tests_require=["mock", "pytest"],
Expand Down
15 changes: 10 additions & 5 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
from logging import getLogger

import flake8
import toml
from pkg_resources import parse_requirements
from pkg_resources import yield_lines

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

from .modules import KNOWN_3RD_PARTIES
from .modules import STDLIB_PY2
from .modules import STDLIB_PY3

# NOTE: Changing this number will alter package version as well.
__version__ = "1.7.5"
__version__ = "1.7.6"
__license__ = "MIT"

LOG = getLogger('flake8.plugin.requirements')
Expand Down Expand Up @@ -545,10 +549,11 @@ def resolve_requirement(cls, requirement, max_depth=0, path=None):
@memoize
def get_pyproject_toml(cls):
"""Try to load PEP 518 configuration file."""
pyproject_config_path = os.path.join(cls.root_dir, "pyproject.toml")
try:
with open(os.path.join(cls.root_dir, "pyproject.toml")) as f:
return toml.loads(f.read())
except (IOError, toml.TomlDecodeError) as e:
with open(pyproject_config_path, mode="rb") as f:
return tomllib.load(f)
except (IOError, tomllib.TOMLDecodeError) as e:
LOG.debug("Couldn't load project setup: %s", e)
return {}

Expand Down
4 changes: 2 additions & 2 deletions test/test_pep621.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Flake8Options:

class Pep621TestCase(unittest.TestCase):

content = """
content = b"""
[project]
name="test"
dependencies=["tools==1.0"]
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_get_pyproject_toml_pep621(self):
self.assertDictEqual(pep621, expected)

def test_get_pyproject_toml_invalid(self):
content = self.content + "invalid"
content = self.content + b"invalid"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
self.assertDictEqual(Flake8Checker.get_pyproject_toml_pep621(), {})

Expand Down
12 changes: 6 additions & 6 deletions test/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def setUp(self):
memoize.mem = {}

def test_get_pyproject_toml_poetry(self):
content = "[tool.poetry]\nname='x'\n[tool.poetry.tag]\nx=0\n"
content = b"[tool.poetry]\nname='x'\n[tool.poetry.tag]\nx=0\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
poetry = Flake8Checker.get_pyproject_toml_poetry()
self.assertDictEqual(poetry, {'name': "x", 'tag': {'x': 0}})

def test_1st_party(self):
content = "[tool.poetry]\nname='book'\n"
content = b"[tool.poetry]\nname='book'\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
Expand All @@ -38,8 +38,8 @@ def test_1st_party(self):
self.assertEqual(mods, ModuleSet({"book": {}}))

def test_3rd_party(self):
content = "[tool.poetry.dependencies]\ntools='1.0'\n"
content += "[tool.poetry.dev-dependencies]\ndev-tools='1.0'\n"
content = b"[tool.poetry.dependencies]\ntools='1.0'\n"
content += b"[tool.poetry.dev-dependencies]\ndev-tools='1.0'\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
Expand All @@ -53,8 +53,8 @@ def test_3rd_party(self):
self.assertEqual(mods, ModuleSet({"tools": {}, "dev_tools": {}}))

def test_3rd_party_groups(self):
content = "[tool.poetry.dependencies]\ntools='1.0'\n"
content += "[tool.poetry.group.dev.dependencies]\ndev-tools='1.0'\n"
content = b"[tool.poetry.dependencies]\ntools='1.0'\n"
content += b"[tool.poetry.group.dev.dependencies]\ndev-tools='1.0'\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
Expand Down

0 comments on commit 978e205

Please sign in to comment.