Skip to content

Commit

Permalink
Replace toml with tomli (#829)
Browse files Browse the repository at this point in the history
* Replace `toml` with `tomli`

* Only require `tomli` on Python < 3.11

* Update test-requirements.txt

Co-authored-by: Eric Brown <ericwb@users.noreply.github.com>
  • Loading branch information
mkniewallner and ericwb committed Mar 25, 2022
1 parent af9f8dc commit 5a8f105
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions bandit/core/config.py
Expand Up @@ -3,13 +3,17 @@
#
# SPDX-License-Identifier: Apache-2.0
import logging
import sys

import yaml

try:
import toml
except ImportError:
toml = None
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib
except ImportError:
tomllib = None

from bandit.core import constants
from bandit.core import extension_loader
Expand All @@ -34,23 +38,23 @@ def __init__(self, config_file=None):

if config_file:
try:
f = open(config_file)
f = open(config_file, "rb")
except OSError:
raise utils.ConfigError(
"Could not read config file.", config_file
)

if config_file.endswith(".toml"):
if toml is None:
if tomllib is None:
raise utils.ConfigError(
"toml parser not available, reinstall with toml extra",
config_file,
)

try:
with f:
self._config = toml.load(f)["tool"]["bandit"]
except toml.TomlDecodeError as err:
self._config = tomllib.load(f)["tool"]["bandit"]
except tomllib.TOMLDecodeError as err:
LOG.error(err)
raise utils.ConfigError("Error parsing file.", config_file)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -31,7 +31,7 @@ project_urls =
yaml =
PyYAML
toml =
toml
tomli>=1.1.0; python_version < "3.11"

[entry_points]
console_scripts =
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Expand Up @@ -7,6 +7,6 @@ flake8>=4.0.0 # Apache-2.0
stestr>=2.5.0 # Apache-2.0
testscenarios>=0.5.0 # Apache-2.0/BSD
testtools>=2.3.0 # MIT
toml # MIT
tomli>=1.1.0;python_version<"3.11" # MIT
beautifulsoup4>=4.8.0 # MIT
pylint==1.9.4 # GPLv2

0 comments on commit 5a8f105

Please sign in to comment.