Skip to content

Commit

Permalink
support partial default types
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Feb 14, 2021
1 parent a3e868c commit 0ccfc86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
21 changes: 8 additions & 13 deletions argopt/_docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,19 @@ def __init__(self, name, value=None, desc=None, typ=None):
self.value = value
self.desc = desc
self.type = typ
self._fix_value_type()

def _fix_value_type(self):
value, typ = self.value, self.type
if (value is not None) and (typ is None):
if type(value) is bool:
self.type = bool
elif value == "None":
self.value = None
elif hasattr(value, 'rsplit'):
i = value.rsplit(':', 1)
if len(i) == 2:
# potentially used in `eval`, e.g. `partial(open, mode="w")`
from functools import partial # NOQA: F401

self.type = eval(i[1])
self.value = typecast(i[0], i[1])

Expand Down Expand Up @@ -210,17 +215,7 @@ def __init__(self, short=None, long=None, argcount=0, value=False,
self.type = typ
self.desc = desc
self.meta = meta

if (value is not None) and (typ is None):
if type(value) is bool:
self.type = bool
elif value == "None":
self.value = None
elif hasattr(value, 'rsplit'):
i = value.rsplit(':', 1)
if len(i) == 2:
self.type = eval(i[1])
self.value = typecast(i[0], i[1])
self._fix_value_type()

@classmethod
def parse(class_, option_description):
Expand Down
3 changes: 3 additions & 0 deletions argopt/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import subprocess

# potentially used in `eval`, e.g. `partial(open, mode="w")`
from functools import partial # NOQA: F401

__all__ = ["_range", "typecast", "set_nargs", "_sh", "DictAttrWrap"]

try: # py2
Expand Down

0 comments on commit 0ccfc86

Please sign in to comment.