Skip to content

Commit

Permalink
Account for breaking change in argparse.ArgumentParser._parse_optiona…
Browse files Browse the repository at this point in the history
…l affecting python 3.11.9 and likely >3.13 (#484).
  • Loading branch information
mauvilsa committed Apr 9, 2024
1 parent c9732a5 commit a7cb5e4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The semantic versioning only considers the public API as described in
:ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import
paths are considered internals and can change in minor and patch releases.


v4.28.0 (2024-03-??)
--------------------

Expand All @@ -19,6 +20,13 @@ Added
- Support for "-" as value for Path class initialization so that user
can ask to use standard input/output instead of file.

Fixed
^^^^^
- Account for breaking change in ``argparse.ArgumentParser._parse_optional``
affecting python ``3.11.9`` and likely ``>3.13`` (`#484
<https://github.com/omni-us/jsonargparse/issues/484>`__).


v4.27.7 (2024-03-21)
--------------------

Expand Down
9 changes: 5 additions & 4 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,13 @@ def parse_known_args(self, args=None, namespace=None):
return namespace, args

def _parse_optional(self, arg_string):
subclass_arg = ActionTypeHint.parse_argv_item(arg_string)
if subclass_arg:
return subclass_arg
if arg_string == self._print_config:
arg_string += "="
return super()._parse_optional(arg_string)
arg_parsed = super()._parse_optional(arg_string)
subclass_arg = ActionTypeHint.parse_argv_item(arg_string, arg_parsed)
if subclass_arg:
return subclass_arg
return arg_parsed

def _parse_common(
self,
Expand Down
7 changes: 5 additions & 2 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,21 @@ def is_init_arg_mapping_typehint(self, key, cfg):
return result

@staticmethod
def parse_argv_item(arg_string):
def parse_argv_item(arg_string, arg_parsed):
parser = subclass_arg_parser.get()
action = None
sep = None
if arg_string.startswith("--"):
arg_base, explicit_arg = (arg_string, None)
if "=" in arg_string:
arg_base, explicit_arg = arg_string.split("=", 1)
arg_base, sep, explicit_arg = arg_string.partition("=")
if "." in arg_base and arg_base not in parser._option_string_actions:
action = _find_parent_action(parser, arg_base[2:])

typehint = typehint_from_action(action)
if typehint:
if len(arg_parsed) == 4:
return action, arg_base, sep, explicit_arg

Check warning on line 349 in jsonargparse/_typehints.py

View check run for this annotation

Codecov / codecov/patch

jsonargparse/_typehints.py#L349

Added line #L349 was not covered by tests
return action, arg_base, explicit_arg
return None

Expand Down
2 changes: 2 additions & 0 deletions jsonargparse_tests/test_argcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
def skip_if_argcomplete_unavailable():
if not find_spec("argcomplete"):
pytest.skip("argcomplete package is required")
if sys.version_info[:2] == (3, 11):
pytest.skip("argcomplete is not compatible with Python 3.11, https://github.com/kislyuk/argcomplete/issues/481")


@contextmanager
Expand Down

0 comments on commit a7cb5e4

Please sign in to comment.