Skip to content

Commit

Permalink
Install and use toml conditionally on Python2 (#88)
Browse files Browse the repository at this point in the history
tomli is not available for Python2, so to keep the Python2
compatibility, we need to install and use toml instead.

Use conditional import on ImportError to avoid introducing tox as new
dependency.

As there is difference in the naming of the toml and tomli error
classes, we use generic Exception. More over, we actually want to
fallback to empty config nevertheless what went wrong.
  • Loading branch information
iivanov-qb committed Mar 26, 2024
1 parent 4833cf8 commit 43b3302
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def get_abs_path(pathname):
install_requires=[
"flake8 >= 2.0.0",
"setuptools >= 10.0.0",
"tomli>=1.2.1; python_version < '3.11'",
"toml >= 0.7.1; python_version < '3.0'",
"tomli >= 1.2.1; python_version >= '3.0' and python_version < '3.11'",
],
setup_requires=["pytest-runner"],
tests_require=["mock", "pytest"],
Expand Down
15 changes: 9 additions & 6 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
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
try:
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
except ImportError:
import toml 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.8"
__version__ = "1.7.9"
__license__ = "MIT"

LOG = getLogger('flake8.plugin.requirements')
Expand Down Expand Up @@ -557,7 +560,7 @@ def get_pyproject_toml(cls):
try:
with open(pyproject_config_path, mode="rb") as f:
return tomllib.load(f)
except (IOError, tomllib.TOMLDecodeError) as e:
except Exception as e:
LOG.debug("Couldn't load project setup: %s", e)
return {}

Expand Down

0 comments on commit 43b3302

Please sign in to comment.