Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

python 3.8 parser fix on args_that_override #4507

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
handle args during parsing
Jing Xu committed Apr 20, 2022
commit 39086e1fbbd128b36753e34a9a8ab5d8a05f720e
28 changes: 16 additions & 12 deletions parlai/core/params.py
Original file line number Diff line number Diff line change
@@ -974,6 +974,19 @@ def add_extra_args(self, args=None):
'got an attribute error when parsing.'
)

def _handle_single_dash_parsearg(self, args, actions):
if _sys.version_info >= (3, 8, 0):
newargs = []
for arg in args:
darg = f'-{arg}'
if arg.startswith('-') and not arg.startswith('--') and darg in actions:
newargs.append(darg)
else:
newargs.append(arg)
return newargs
else:
return args

def parse_known_args(self, args=None, namespace=None, nohelp=False):
"""
Parse known args to ignore help flag.
@@ -987,16 +1000,7 @@ def parse_known_args(self, args=None, namespace=None, nohelp=False):
actions = set()
for action in self._actions:
actions.update(action.option_strings)
if _sys.version_info >= (3, 8, 0):
newargs = []
for arg in args:
darg = f'-{arg}'
if arg.startswith('-') and not arg.startswith('--') and darg in actions:
newargs.append(darg)
else:
newargs.append(arg)
args = newargs

args = self._handle_single_dash_parsearg(args, actions)
if nohelp:
# ignore help
args = [
@@ -1099,8 +1103,8 @@ def _process_args_to_opts(self, args_that_override: Optional[List[str]] = None):
if args_that_override is None:
args_that_override = _sys.argv[1:]

args_that_override = self._handle_single_dash_addarg(
fix_underscores(args_that_override)
args_that_override = self._handle_single_dash_parsearg(
fix_underscores(args_that_override), option_strings_dict.keys()
)

for i in range(len(args_that_override)):