Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Commit 588ed9a

Browse files
oliiscottj97
authored andcommitted
Fix parsing of lists with relativedelta objects.
The relativedelta objects were rejected because we cannot compare them to strings. Both `==` and `!=` results to `False`. Instead we have to first test the type and then do the comparison.
1 parent 195b0e9 commit 588ed9a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pyhocon/config_parser.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,16 @@ def postParse(self, instring, loc, token_list):
751751
:return:
752752
"""
753753
cleaned_token_list = []
754+
# Note that a token can be a duration value object:
755+
# >>> relativedelta(hours = 1) == ''
756+
# False
757+
# >>> relativedelta(hours = 1) != ''
758+
# False
759+
# relativedelta.__eq__() raises NotImplemented if it is compared with
760+
# a different object type so Python falls back to identity comparison.
761+
# We cannot compare this object to a string object.
754762
for token in token_list:
755-
if token == '':
763+
if isinstance(token, str) and token == '':
756764
# This is the case when there was a trailing comma in the list.
757765
# The last token is just an empty string so we can safely ignore
758766
# it.

tests/test_config_parser.py

+10
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ def test_parse_string_with_duration_with_long_unit_name(self):
148148
)
149149
assert config['b'] == period(weeks=10)
150150

151+
def test_parse_with_list_mixed_types_with_durations_and_trailing_comma(self):
152+
config = ConfigFactory.parse_string(
153+
"""
154+
a: foo
155+
b: [a, 1, 10 weeks, 5 minutes,]
156+
c: bar
157+
"""
158+
)
159+
assert config['b'] == ['a', 1, period(weeks=10), period(minutes=5)]
160+
151161
def test_parse_with_enclosing_square_bracket(self):
152162
config = ConfigFactory.parse_string("[1, 2, 3]")
153163
assert config == [1, 2, 3]

0 commit comments

Comments
 (0)