Merge pull request #3801 from nicoddemus/improve-warning-addoption

Improve warning messages when addoption is called with string as `type`
This commit is contained in:
Ronny Pfannschmidt 2018-08-14 09:30:17 +02:00 committed by GitHub
commit 22ee2093b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 7 deletions

View File

@ -174,23 +174,23 @@ class Argument(object):
if isinstance(typ, six.string_types): if isinstance(typ, six.string_types):
if typ == "choice": if typ == "choice":
warnings.warn( warnings.warn(
"type argument to addoption() is a string %r." "`type` argument to addoption() is the string %r."
" For parsearg this is optional and when supplied" " For choices this is optional and can be omitted, "
" should be a type." " but when supplied should be a type (for example `str` or `int`)."
" (options: %s)" % (typ, names), " (options: %s)" % (typ, names),
DeprecationWarning, DeprecationWarning,
stacklevel=3, stacklevel=4,
) )
# argparse expects a type here take it from # argparse expects a type here take it from
# the type of the first element # the type of the first element
attrs["type"] = type(attrs["choices"][0]) attrs["type"] = type(attrs["choices"][0])
else: else:
warnings.warn( warnings.warn(
"type argument to addoption() is a string %r." "`type` argument to addoption() is the string %r, "
" For parsearg this should be a type." " but when supplied should be a type (for example `str` or `int`)."
" (options: %s)" % (typ, names), " (options: %s)" % (typ, names),
DeprecationWarning, DeprecationWarning,
stacklevel=3, stacklevel=4,
) )
attrs["type"] = Argument._typ_map[typ] attrs["type"] = Argument._typ_map[typ]
# used in test_parseopt -> test_parse_defaultgetter # used in test_parseopt -> test_parse_defaultgetter