Skip to content

Commit bcc58ec

Browse files
authored
Merge pull request #1740 from RonnyPfannschmidt/float-argument
optparse compatibility - add float and complex
2 parents 2b99738 + 9af872a commit bcc58ec

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ time or change existing behaviors in order to make them less surprising/more use
220220
still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_
221221
for the PR (`#1626`_).
222222

223+
* ``optparse`` type usage now triggers DeprecationWarnings (`#1740`_).
224+
225+
* ``optparse`` backward compatibility supports float/complex types (`#457`_).
226+
223227
*
224228

225229
*
@@ -256,6 +260,8 @@ time or change existing behaviors in order to make them less surprising/more use
256260

257261
.. _#372: https://github.com/pytest-dev/pytest/issues/372
258262
.. _#460: https://github.com/pytest-dev/pytest/pull/460
263+
.. _#457: https://github.com/pytest-dev/pytest/issues/457
264+
.. _#1740: https://github.com/pytest-dev/pytest/issues/1740
259265
.. _#607: https://github.com/pytest-dev/pytest/issues/607
260266
.. _#925: https://github.com/pytest-dev/pytest/issues/925
261267
.. _#1235: https://github.com/pytest-dev/pytest/issues/1235

_pytest/config.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -552,31 +552,31 @@ def __str__(self):
552552

553553

554554
class Argument:
555-
"""class that mimics the necessary behaviour of optparse.Option """
555+
"""class that mimics the necessary behaviour of optparse.Option
556+
557+
its currently a least effort implementation
558+
and ignoring choices and integer prefixes
559+
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
560+
"""
556561
_typ_map = {
557562
'int': int,
558563
'string': str,
559-
}
560-
# enable after some grace period for plugin writers
561-
TYPE_WARN = False
564+
'float': float,
565+
'complex': complex,
566+
}
562567

563568
def __init__(self, *names, **attrs):
564569
"""store parms in private vars for use in add_argument"""
565570
self._attrs = attrs
566571
self._short_opts = []
567572
self._long_opts = []
568573
self.dest = attrs.get('dest')
569-
if self.TYPE_WARN:
570-
try:
571-
help = attrs['help']
572-
if '%default' in help:
573-
warnings.warn(
574-
'pytest now uses argparse. "%default" should be'
575-
' changed to "%(default)s" ',
576-
FutureWarning,
577-
stacklevel=3)
578-
except KeyError:
579-
pass
574+
if '%default' in (attrs.get('help') or ''):
575+
warnings.warn(
576+
'pytest now uses argparse. "%default" should be'
577+
' changed to "%(default)s" ',
578+
DeprecationWarning,
579+
stacklevel=3)
580580
try:
581581
typ = attrs['type']
582582
except KeyError:
@@ -585,25 +585,23 @@ def __init__(self, *names, **attrs):
585585
# this might raise a keyerror as well, don't want to catch that
586586
if isinstance(typ, py.builtin._basestring):
587587
if typ == 'choice':
588-
if self.TYPE_WARN:
589-
warnings.warn(
590-
'type argument to addoption() is a string %r.'
591-
' For parsearg this is optional and when supplied '
592-
' should be a type.'
593-
' (options: %s)' % (typ, names),
594-
FutureWarning,
595-
stacklevel=3)
588+
warnings.warn(
589+
'type argument to addoption() is a string %r.'
590+
' For parsearg this is optional and when supplied '
591+
' should be a type.'
592+
' (options: %s)' % (typ, names),
593+
DeprecationWarning,
594+
stacklevel=3)
596595
# argparse expects a type here take it from
597596
# the type of the first element
598597
attrs['type'] = type(attrs['choices'][0])
599598
else:
600-
if self.TYPE_WARN:
601-
warnings.warn(
602-
'type argument to addoption() is a string %r.'
603-
' For parsearg this should be a type.'
604-
' (options: %s)' % (typ, names),
605-
FutureWarning,
606-
stacklevel=3)
599+
warnings.warn(
600+
'type argument to addoption() is a string %r.'
601+
' For parsearg this should be a type.'
602+
' (options: %s)' % (typ, names),
603+
DeprecationWarning,
604+
stacklevel=3)
607605
attrs['type'] = Argument._typ_map[typ]
608606
# used in test_parseopt -> test_parse_defaultgetter
609607
self.type = attrs['type']

0 commit comments

Comments
 (0)