From 317b3f257da727419118b8c8bd5e3280dd47e8c0 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 19 Jul 2016 10:20:41 +0200 Subject: [PATCH 1/3] optparse compatibility - add float and complex also documents the implementation quality fixes #457 --- _pytest/config.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 68a3dafbe0d..17d0837cc58 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -552,11 +552,18 @@ def __str__(self): 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 = { 'int': int, 'string': str, - } + 'float': float, + 'complex': complex, + } # enable after some grace period for plugin writers TYPE_WARN = False From 61cc5c4d4e661bf7a8d3e679f3999c7f2302d861 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 19 Jul 2016 10:33:25 +0200 Subject: [PATCH 2/3] argument parsing: always warn for string types fix #1741 --- _pytest/config.py | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 17d0837cc58..a667df75d9c 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -564,8 +564,6 @@ class Argument: 'float': float, 'complex': complex, } - # enable after some grace period for plugin writers - TYPE_WARN = False def __init__(self, *names, **attrs): """store parms in private vars for use in add_argument""" @@ -573,17 +571,12 @@ def __init__(self, *names, **attrs): self._short_opts = [] self._long_opts = [] self.dest = attrs.get('dest') - if self.TYPE_WARN: - try: - help = attrs['help'] - if '%default' in help: - warnings.warn( - 'pytest now uses argparse. "%default" should be' - ' changed to "%(default)s" ', - FutureWarning, - stacklevel=3) - except KeyError: - pass + if '%default' in (attrs.get('help') or ''): + warnings.warn( + 'pytest now uses argparse. "%default" should be' + ' changed to "%(default)s" ', + DeprecationWarning, + stacklevel=3) try: typ = attrs['type'] except KeyError: @@ -592,25 +585,23 @@ def __init__(self, *names, **attrs): # this might raise a keyerror as well, don't want to catch that if isinstance(typ, py.builtin._basestring): if typ == 'choice': - if self.TYPE_WARN: - warnings.warn( - 'type argument to addoption() is a string %r.' - ' For parsearg this is optional and when supplied ' - ' should be a type.' - ' (options: %s)' % (typ, names), - FutureWarning, - stacklevel=3) + warnings.warn( + 'type argument to addoption() is a string %r.' + ' For parsearg this is optional and when supplied ' + ' should be a type.' + ' (options: %s)' % (typ, names), + DeprecationWarning, + stacklevel=3) # argparse expects a type here take it from # the type of the first element attrs['type'] = type(attrs['choices'][0]) else: - if self.TYPE_WARN: - warnings.warn( - 'type argument to addoption() is a string %r.' - ' For parsearg this should be a type.' - ' (options: %s)' % (typ, names), - FutureWarning, - stacklevel=3) + warnings.warn( + 'type argument to addoption() is a string %r.' + ' For parsearg this should be a type.' + ' (options: %s)' % (typ, names), + DeprecationWarning, + stacklevel=3) attrs['type'] = Argument._typ_map[typ] # used in test_parseopt -> test_parse_defaultgetter self.type = attrs['type'] From 9af872a2305c7306175991fd64721741b87223f1 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 19 Jul 2016 12:42:08 +0200 Subject: [PATCH 3/3] update changelog --- CHANGELOG.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3ff14be7fbb..f86cdce71da 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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`_ 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 .. _#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 .. _#925: https://github.com/pytest-dev/pytest/issues/925 .. _#1235: https://github.com/pytest-dev/pytest/issues/1235