Merge pull request #1740 from RonnyPfannschmidt/float-argument

optparse compatibility - add float and complex
This commit is contained in:
Bruno Oliveira 2016-07-19 19:17:35 -03:00 committed by GitHub
commit bcc58ec916
2 changed files with 34 additions and 30 deletions

View File

@ -220,6 +220,10 @@ time or change existing behaviors in order to make them less surprising/more use
still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_ still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_
for the PR (`#1626`_). for the PR (`#1626`_).
* ``optparse`` type usage now triggers DeprecationWarnings (`#1740`_).
* ``optparse`` backward compatibility supports float/complex types (`#457`_).
* *
* *
@ -256,6 +260,8 @@ time or change existing behaviors in order to make them less surprising/more use
.. _#372: https://github.com/pytest-dev/pytest/issues/372 .. _#372: https://github.com/pytest-dev/pytest/issues/372
.. _#460: https://github.com/pytest-dev/pytest/pull/460 .. _#460: https://github.com/pytest-dev/pytest/pull/460
.. _#457: https://github.com/pytest-dev/pytest/issues/457
.. _#1740: https://github.com/pytest-dev/pytest/issues/1740
.. _#607: https://github.com/pytest-dev/pytest/issues/607 .. _#607: https://github.com/pytest-dev/pytest/issues/607
.. _#925: https://github.com/pytest-dev/pytest/issues/925 .. _#925: https://github.com/pytest-dev/pytest/issues/925
.. _#1235: https://github.com/pytest-dev/pytest/issues/1235 .. _#1235: https://github.com/pytest-dev/pytest/issues/1235

View File

@ -552,13 +552,18 @@ class ArgumentError(Exception):
class Argument: class Argument:
"""class that mimics the necessary behaviour of optparse.Option """ """class that mimics the necessary behaviour of optparse.Option
its currently a least effort implementation
and ignoring choices and integer prefixes
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
"""
_typ_map = { _typ_map = {
'int': int, 'int': int,
'string': str, 'string': str,
} 'float': float,
# enable after some grace period for plugin writers 'complex': complex,
TYPE_WARN = False }
def __init__(self, *names, **attrs): def __init__(self, *names, **attrs):
"""store parms in private vars for use in add_argument""" """store parms in private vars for use in add_argument"""
@ -566,17 +571,12 @@ class Argument:
self._short_opts = [] self._short_opts = []
self._long_opts = [] self._long_opts = []
self.dest = attrs.get('dest') self.dest = attrs.get('dest')
if self.TYPE_WARN: if '%default' in (attrs.get('help') or ''):
try: warnings.warn(
help = attrs['help'] 'pytest now uses argparse. "%default" should be'
if '%default' in help: ' changed to "%(default)s" ',
warnings.warn( DeprecationWarning,
'pytest now uses argparse. "%default" should be' stacklevel=3)
' changed to "%(default)s" ',
FutureWarning,
stacklevel=3)
except KeyError:
pass
try: try:
typ = attrs['type'] typ = attrs['type']
except KeyError: except KeyError:
@ -585,25 +585,23 @@ class Argument:
# this might raise a keyerror as well, don't want to catch that # this might raise a keyerror as well, don't want to catch that
if isinstance(typ, py.builtin._basestring): if isinstance(typ, py.builtin._basestring):
if typ == 'choice': if typ == 'choice':
if self.TYPE_WARN: warnings.warn(
warnings.warn( 'type argument to addoption() is a string %r.'
'type argument to addoption() is a string %r.' ' For parsearg this is optional and when supplied '
' For parsearg this is optional and when supplied ' ' should be a type.'
' should be a type.' ' (options: %s)' % (typ, names),
' (options: %s)' % (typ, names), DeprecationWarning,
FutureWarning, stacklevel=3)
stacklevel=3)
# 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:
if self.TYPE_WARN: warnings.warn(
warnings.warn( 'type argument to addoption() is a string %r.'
'type argument to addoption() is a string %r.' ' For parsearg this should be a type.'
' For parsearg this should be a type.' ' (options: %s)' % (typ, names),
' (options: %s)' % (typ, names), DeprecationWarning,
FutureWarning, stacklevel=3)
stacklevel=3)
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
self.type = attrs['type'] self.type = attrs['type']