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 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
28 changes: 17 additions & 11 deletions parlai/core/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = [
Expand Down Expand Up @@ -1099,7 +1103,9 @@ 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 = 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)):
if args_that_override[i] in option_strings_dict:
Expand Down