Fix no warning when failing to patch argparse (#576)

This commit is contained in:
eugen-ajechiloae-clearml 2022-02-13 16:25:26 +02:00 committed by GitHub
parent 5be1c8b2b0
commit ba2e349d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
""" Argparse utilities""" """ Argparse utilities"""
import sys import sys
import warnings
from copy import copy from copy import copy
from six import PY2 from six import PY2
@ -36,13 +37,24 @@ class PatchArgumentParser:
@staticmethod @staticmethod
def parse_args(self, args=None, namespace=None): def parse_args(self, args=None, namespace=None):
if PatchArgumentParser._recursion_guard: if PatchArgumentParser._recursion_guard:
return {} if not PatchArgumentParser._original_parse_args else \ return (
PatchArgumentParser._original_parse_args(self, args=args, namespace=namespace) {}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_args(self, args=args, namespace=namespace)
)
PatchArgumentParser._recursion_guard = True PatchArgumentParser._recursion_guard = True
try: try:
result = PatchArgumentParser._patched_parse_args( 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("Failed patching argparse arguments: %s" % e)
finally: finally:
PatchArgumentParser._recursion_guard = False PatchArgumentParser._recursion_guard = False
return result return result
@ -50,13 +62,24 @@ class PatchArgumentParser:
@staticmethod @staticmethod
def parse_known_args(self, args=None, namespace=None): def parse_known_args(self, args=None, namespace=None):
if PatchArgumentParser._recursion_guard: if PatchArgumentParser._recursion_guard:
return {} if not PatchArgumentParser._original_parse_args else \ return (
PatchArgumentParser._original_parse_known_args(self, args=args, namespace=namespace) {}
if not PatchArgumentParser._original_parse_args
else PatchArgumentParser._original_parse_known_args(self, args=args, namespace=namespace)
)
PatchArgumentParser._recursion_guard = True PatchArgumentParser._recursion_guard = True
try: try:
result = PatchArgumentParser._patched_parse_args( 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("Failed patching argparse arguments: %s" % e)
finally: finally:
PatchArgumentParser._recursion_guard = False PatchArgumentParser._recursion_guard = False
return result return result
@ -224,7 +247,10 @@ def patch_argparse():
# Notice! we are patching argparser, so we know if someone parsed arguments before connecting to task # Notice! we are patching argparser, so we know if someone parsed arguments before connecting to task
try:
patch_argparse() patch_argparse()
except Exception as e:
warnings.warn("Failed patching argparse: %s" % e)
def call_original_argparser(self, args=None, namespace=None): def call_original_argparser(self, args=None, namespace=None):