Skip to content
Open
Show file tree
Hide file tree
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
35 changes: 27 additions & 8 deletions python-stdlib/argparse/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,41 @@ class _ArgError(BaseException):
pass


class ArgumentTypeError(BaseException):
pass


class _Arg:
def __init__(self, names, dest, action, nargs, const, default, help):
def __init__(self, names, dest, action, nargs, const, default, help, type):
self.names = names
self.dest = dest
self.action = action
self.nargs = nargs
self.const = const
self.default = default
self.help = help
self.type = type

def _apply(self, optname, arg):
if self.type:
try:
return self.type(arg)
except Exception as e:
if isinstance(e, (ArgumentTypeError, TypeError, ValueError)):
raise _ArgError("invalid value for %s: %s (%s)" % (optname, arg, str(e)))
raise
return arg

def parse(self, optname, args):
# parse args for this arg
if self.action == "store":
if self.nargs is None:
if args:
return args.pop(0)
return self._apply(optname, args.pop(0))
else:
raise _ArgError("expecting value for %s" % optname)
elif self.nargs == "?":
if args:
return args.pop(0)
else:
return self.default
return self._apply(optname, args.pop(0) if args else self.default)
else:
if self.nargs == "*":
n = -1
Expand All @@ -52,7 +64,7 @@ def parse(self, optname, args):
else:
break
else:
ret.append(args.pop(0))
ret.append(self._apply(optname, args.pop(0)))
n -= 1
if n > 0:
raise _ArgError("expecting value for %s" % optname)
Expand Down Expand Up @@ -103,6 +115,10 @@ def add_argument(self, *args, **kwargs):
dest = args[0]
if not args:
args = [dest]
arg_type = kwargs.get("type", None)
if arg_type is not None:
if not callable(arg_type):
Copy link

Choose a reason for hiding this comment

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

Not sure but for code size perhaps this could be left out? It would become clear eventually from the moment the argument is used.

Copy link
Author

@agatti agatti Nov 19, 2025

Choose a reason for hiding this comment

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

I've made it follow CPython here, type kwarg checks are performed before arguments are actually parsed.

There's another issue, though. Letting the code fail when performing the argument check has the side effect that non-callable types raise a ValueError TypeError which, for CPython compatibility, is caught and reported as an _ArgError that in turn will terminate execution.

If you don't have typing stubs set up for one reason or another, I'd say this is the kind of error you may want to catch as early as possible.

Copy link

Choose a reason for hiding this comment

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

Sure, makes sense.

raise ValueError("type is not callable")
list.append(
_Arg(
args,
Expand All @@ -112,6 +128,7 @@ def add_argument(self, *args, **kwargs):
const,
default,
kwargs.get("help", ""),
arg_type,
)
)

Expand Down Expand Up @@ -176,7 +193,9 @@ def _parse_args(self, args, return_unknown):
arg_vals = []
for opt in self.opt:
arg_dest.append(opt.dest)
arg_vals.append(opt.default)
arg_vals.append(
opt._apply(opt.dest, opt.default) if isinstance(opt.default, str) else opt.default
)

# deal with unknown arguments, if needed
unknown = []
Expand Down
2 changes: 1 addition & 1 deletion python-stdlib/argparse/manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
metadata(version="0.4.0")
metadata(version="0.4.1")

# Originally written by Damien George.

Expand Down
44 changes: 44 additions & 0 deletions python-stdlib/argparse/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,47 @@
args, rest = parser.parse_known_args(["a", "b", "c", "-b", "2", "--x", "5", "1"])
assert args.a == ["a", "b"] and args.b == "2"
assert rest == ["c", "--x", "5", "1"]


class CustomArgType:
def __init__(self, add):
self.add = add

def __call__(self, value):
return int(value) + self.add


parser = argparse.ArgumentParser()
parser.add_argument("-a", type=int)
args = parser.parse_args(["-a", "123"])
assert args.a == 123
parser.add_argument("-b", type=str)
args = parser.parse_args(["-b", "string"])
assert args.b == "string"
parser.add_argument("-c", type=CustomArgType(1))
args = parser.parse_args(["-c", "123"])
assert args.c == 124
try:
parser.add_argument("-d", type=())
assert False
except ValueError as e:
assert "not callable" in str(e)
parser.add_argument("-d", type=int, nargs="+")
args = parser.parse_args(["-d", "123", "124", "125"])
assert args.d == [123, 124, 125]
parser.add_argument("-e", type=CustomArgType(1), nargs="+")
args = parser.parse_args(["-e", "123", "124", "125"])
assert args.e == [124, 125, 126]
parser.add_argument("-f", type=CustomArgType(1), nargs="?")
args = parser.parse_args(["-f", "123"])
assert args.f == 124
parser.add_argument("-g", type=CustomArgType(1), default=1)
parser.add_argument("-i", type=CustomArgType(1), default="1")
args = parser.parse_args([])
assert args.g == 1
assert args.i == 2
parser.add_argument("-j", type=CustomArgType(1), default=1)
args = parser.parse_args(["-j", "3"])
assert args.g == 1
assert args.i == 2
assert args.j == 4
Loading