Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/argparse warning #576

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions clearml/binding/args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" Argparse utilities"""
import sys
import warnings
from copy import copy

from six import PY2
Expand Down Expand Up @@ -36,27 +37,49 @@ def add_subparsers(self, **kwargs):
@staticmethod
def parse_args(self, args=None, namespace=None):
if PatchArgumentParser._recursion_guard:
return {} if not PatchArgumentParser._original_parse_args else \
PatchArgumentParser._original_parse_args(self, args=args, namespace=namespace)
return (
{}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_args(self, args=args, namespace=namespace)
)

PatchArgumentParser._recursion_guard = True
try:
result = PatchArgumentParser._patched_parse_args(
PatchArgumentParser._original_parse_args, self, args=args, namespace=namespace)
PatchArgumentParser._original_parse_args, self, args=args, namespace=namespace
)
except Exception as e:
result = (
{}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_args(self, args=args, namespace=namespace)
)
warnings.warn("Error when patching arguments: %s" % e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is specific to argparse, I think the error should mention argparse, something like "Failed patching argparse arguments: %s"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now having more relevant warnings

finally:
PatchArgumentParser._recursion_guard = False
return result

@staticmethod
def parse_known_args(self, args=None, namespace=None):
if PatchArgumentParser._recursion_guard:
return {} if not PatchArgumentParser._original_parse_args else \
PatchArgumentParser._original_parse_known_args(self, args=args, namespace=namespace)
return (
{}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_known_args(self, args=args, namespace=namespace)
)

PatchArgumentParser._recursion_guard = True
try:
result = PatchArgumentParser._patched_parse_args(
PatchArgumentParser._original_parse_known_args, self, args=args, namespace=namespace)
PatchArgumentParser._original_parse_known_args, self, args=args, namespace=namespace
)
except Exception as e:
result = (
{}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_known_args(self, args=args, namespace=namespace)
)
warnings.warn("Error when patching arguments: %s" % e)
finally:
PatchArgumentParser._recursion_guard = False
return result
Expand Down Expand Up @@ -224,7 +247,10 @@ def patch_argparse():


# Notice! we are patching argparser, so we know if someone parsed arguments before connecting to task
patch_argparse()
try:
patch_argparse()
except Exception as e:
warnings.warn("Error when patching argparse: %s" % e)


def call_original_argparser(self, args=None, namespace=None):
Expand Down