Skip to content

Commit

Permalink
Carolguo/fix override (#308)
Browse files Browse the repository at this point in the history
* fix override optional casacade substitution
  • Loading branch information
carolguo-dd authored May 27, 2024
1 parent dbed4b1 commit 351a33d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
13 changes: 8 additions & 5 deletions pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def fixed_get_attr(self, item):
use_urllib2 = False
try:
# For Python 3.0 and later
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
except ImportError: # pragma: no cover
# Fall back to Python 2's urllib2
from urllib2 import urlopen, HTTPError, URLError
from urllib2 import HTTPError, URLError, urlopen

use_urllib2 = True
try:
Expand Down Expand Up @@ -643,9 +643,11 @@ def resolve_substitutions(cls, config, accept_unresolved=False):
continue

is_optional_resolved, resolved_value = cls._resolve_variable(config, substitution)
if isinstance(resolved_value, ConfigValues) and overridden_value and not isinstance(
overridden_value, ConfigValues):
unresolved, _, _ = cls._do_substitute(substitution, overridden_value, is_optional_resolved)
if isinstance(resolved_value, ConfigValues) :
value_to_be_substitute = resolved_value
if overridden_value and not isinstance(overridden_value, ConfigValues):
value_to_be_substitute = overridden_value
unresolved, _, _ = cls._do_substitute(substitution, value_to_be_substitute, is_optional_resolved)
any_unresolved = unresolved or any_unresolved
if not unresolved and substitution in substitutions:
substitutions.remove(substitution)
Expand All @@ -656,6 +658,7 @@ def resolve_substitutions(cls, config, accept_unresolved=False):
continue

cache_values = []

if isinstance(overridden_value, ConfigValues):
cache_values = cache.get(substitution)
if cache_values is None:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def run_tests(self):

setup(
name='pyhocon',
version='0.3.59',
version='0.3.61',
description='HOCON parser for Python',
long_description='pyhocon is a HOCON parser for Python. Additionally we provide a tool (pyhocon) to convert any HOCON '
'content into json, yaml and properties format.',
Expand Down
22 changes: 19 additions & 3 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
# Python 2
from urllib import pathname2url

from pyparsing import ParseBaseException, ParseException, ParseSyntaxException
import mock
import pytest
from pyhocon import (ConfigFactory, ConfigParser, ConfigSubstitutionException, ConfigTree, HOCONConverter)
from pyparsing import ParseBaseException, ParseException, ParseSyntaxException

from pyhocon import (ConfigFactory, ConfigParser, ConfigSubstitutionException,
ConfigTree, HOCONConverter)
from pyhocon.exceptions import (ConfigException, ConfigMissingException,
ConfigWrongTypeException)

Expand Down Expand Up @@ -1486,7 +1488,6 @@ def test_include_dict(self):
""".format(tmp_file=incl_name)
)
assert config3['a'] == expected_res

finally:
os.remove(incl_name)

Expand Down Expand Up @@ -1747,6 +1748,21 @@ def test_cascade_optional_substitution(self):
'retries_msg': 'You have 3 retries'
}

def test_override_optional_substitution(self):
config = ConfigFactory.parse_string(
"""
a = 3
test = ${a}
test = ${?b}
result = ${test}
""")
assert config == {
'a' : 3,
'test': 3,
'result': 3
}


def test_substitution_cycle(self):
with pytest.raises(ConfigSubstitutionException):
ConfigFactory.parse_string(
Expand Down

0 comments on commit 351a33d

Please sign in to comment.