Skip to content

Commit

Permalink
Updated attribute error and modified fix_py_36
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michel committed Apr 13, 2020
1 parent 5f718c4 commit 3e2fc09
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
9 changes: 6 additions & 3 deletions tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def as_dict(self) -> Dict[str, Any]:

return {var: getattr(self, var) for var in self._get_argument_names()}

def from_dict(self, args_dict: Dict[str, Any]) -> None:
def from_dict(self, args_dict: Dict[str, Any], skip_unsettable: bool = False) -> None:
"""Loads arguments from a dictionary, ensuring all required arguments are set.
:param args_dict: A dictionary from argument names to the values of the arguments.
Expand All @@ -426,8 +426,11 @@ def from_dict(self, args_dict: Dict[str, Any]) -> None:
for key, value in args_dict.items():
try:
setattr(self, key, value)
except AttributeError as e:
warnings.warn(e)
except AttributeError:
if not skip_unsettable:
raise AttributeError(f'Cannot set attribute "{key}" to "{value}." '
f'To skip arguments that cannot be set \n'
f'"skip_unsettable = True" \n')

self._parsed = True

Expand Down
4 changes: 1 addition & 3 deletions tap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ def fix_py36_copy(func: Callable) -> Callable:
if sys.version_info[:2] != (3, 6):
return func

re_type = type(re.compile(''))

@wraps(func)
def wrapper(*args, **kwargs):
re_type = type(re.compile(''))
has_prev_val = re_type in copy._deepcopy_dispatch
prev_val = copy._deepcopy_dispatch.get(re_type, None)
copy._deepcopy_dispatch[type(re.compile(''))] = lambda r, _: r
Expand All @@ -355,8 +355,6 @@ def wrapper(*args, **kwargs):

if has_prev_val:
copy._deepcopy_dispatch[re_type] = prev_val
else:
del copy._deepcopy_dispatch[re_type]

return result

Expand Down
1 change: 1 addition & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ class SimpleFromDictTap(Tap):


class TestStoringTap(TestCase):

def test_save_load_simple(self):
class SimpleSaveLoadTap(Tap):
a: str
Expand Down

0 comments on commit 3e2fc09

Please sign in to comment.