Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions configargparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,13 @@ def __call__(self):
def parse(self, stream):
"""Parses the keys and values from a TOML config file."""
# parse with configparser to allow multi-line values
import toml
try:
import tomllib
except ImportError:
import tomli as tomllib

try:
config = toml.load(stream)
config = tomllib.load(stream)
except Exception as e:
raise ConfigFileParserException("Couldn't parse TOML file: %s" % e)

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def launch_http_server(directory):
tests_require = [
"black",
"mock",
"toml",
"PyYAML",
"pytest",
"pytest-cov",
"pytest-subtests",
"tomli; python_version < '3.11'",
]


Expand Down Expand Up @@ -125,6 +125,7 @@ def launch_http_server(directory):
install_requires=install_requires,
tests_require=tests_require,
extras_require={
"toml": ["tomli; python_version < '3.11'"],
"yaml": ["PyYAML"],
"test": tests_require,
},
Expand Down
21 changes: 12 additions & 9 deletions tests/test_configargparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

IS_PY36 = sys.version_info[:2] == (3, 6)


def replace_error_method(arg_parser):
"""Swap out arg_parser's error(..) method so that instead of calling
Expand Down Expand Up @@ -1774,7 +1776,7 @@ def testYAMLConfigFileParser_w_ArgumentParser_parsed_values(self):


class TestTomlConfigParser(unittest.TestCase):
def write_toml_file(self, content, obj=StringIO):
def write_toml_file(self, content, obj=BytesIO):
f = obj()
dedent = lambda x: x
if isinstance(content, str):
Expand All @@ -1785,7 +1787,7 @@ def write_toml_file(self, content, obj=StringIO):

def test_section(self):
f = self.write_toml_file(
"""
b"""
[section]
key1 = 'toml1'
key2 = 'toml2'
Expand All @@ -1796,7 +1798,7 @@ def test_section(self):

def test_no_sections(self):
f = self.write_toml_file(
"""
b"""
[section]
key1 = 'toml1'
key2 = 'toml2'
Expand All @@ -1806,19 +1808,19 @@ def test_no_sections(self):
self.assertEqual(parser.parse(f), {})

def test_empty_section(self):
f = self.write_toml_file("[section]")
f = self.write_toml_file(b"[section]")
parser = configargparse.TomlConfigParser(["section"])
self.assertEqual(parser.parse(f), {})

def test_empty_file(self):
f = self.write_toml_file("")
f = self.write_toml_file(b"")
parser = configargparse.TomlConfigParser(["section"])
self.assertEqual(parser.parse(f), {})

@unittest.expectedFailure # Ints should be strings
def test_advanced(self):
f = self.write_toml_file(
"""
b"""
[tool.section]
key1 = "toml1"
key2 = [1, 2, 3]
Expand All @@ -1827,11 +1829,12 @@ def test_advanced(self):
parser = configargparse.TomlConfigParser(["tool.section"])
self.assertEqual(parser.parse(f), {"key1": "toml1", "key2": ["1", "2", "3"]})

def test_fails_binary_read(self):
@unittest.skipIf(IS_PY36, "3.6 does not fail on bad parse")
def test_fails_str_read(self):
f = self.write_toml_file(
b"""[tool.section]\nkey1 = "toml1"
"""[tool.section]\nkey1 = "toml1"
""",
obj=BytesIO,
obj=StringIO,
)
parser = configargparse.TomlConfigParser(["tool.section"])
with self.assertRaises(configargparse.ConfigFileParserException):
Expand Down
Loading