auto change %default -> %(default)s in help parameter string (on retrieval)
added code for warnings on optparse arguments (type, help), which can be easily switched on with TYPE_WARN = True in config.py installed and tested ( py.test --help ) pytest-quickcheck-0.7 pytest-gae-0.2.2 pytest-growl-0.1 pytest-bdd-0.4.7 pytest-bdd-splinter-0.4.4 pytest-cache-1.0 pytest-capturelog-0.7 pytest-codecheckers-0.2 pytest-contextfixture-0.1.1 pytest-cov-1.6 pytest-flakes-0.1 pytest-incremental-0.3.0 pytest-xdist-1.8 pytest-localserver-0.1.5 pytest-monkeyplus-1.1.0 pytest-oerp-0.2.0 pytest-pep8-1.0.4 pytest-pydev-0.1 pytest-rage-0.1 pytest-runfailed-0.3 pytest-timeout-0.3 pytest-xprocess-0.7 pytest-browsermob-proxy-0.1 pytest-mozwebqa-1.1.1 pytest-random-0.02 pytest-rerunfailures-0.03 pytest-zap-0.1 pytest-blockage-0.1 pytest-django-2.3.0 pytest-figleaf-1.0 pytest-greendots-0.1 pytest-instafail-0.1.0 pytest-konira-0.2 pytest-marker-bugzilla-0.06 pytest-marks-0.4 pytest-poo-0.2 pytest-twisted-1.4 pytest-yamlwsgi-0.6 --HG-- branch : argparse
This commit is contained in:
parent
15ec5a898c
commit
ad72e7f29d
|
@ -5,6 +5,12 @@ import sys, os
|
||||||
from _pytest.core import PluginManager
|
from _pytest.core import PluginManager
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
# enable after some grace period for plugin writers
|
||||||
|
TYPE_WARN = False
|
||||||
|
if TYPE_WARN:
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
def pytest_cmdline_parse(pluginmanager, args):
|
def pytest_cmdline_parse(pluginmanager, args):
|
||||||
config = Config(pluginmanager)
|
config = Config(pluginmanager)
|
||||||
config.parse(args)
|
config.parse(args)
|
||||||
|
@ -147,6 +153,17 @@ 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 TYPE_WARN:
|
||||||
|
try:
|
||||||
|
help = attrs['help']
|
||||||
|
if '%default' in help:
|
||||||
|
warnings.warn(
|
||||||
|
'py.test now uses argparse. "%default" should be'
|
||||||
|
' changed to "%(default)s" ',
|
||||||
|
FutureWarning,
|
||||||
|
stacklevel=3)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
try:
|
try:
|
||||||
typ = attrs['type']
|
typ = attrs['type']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -155,10 +172,25 @@ 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, str):
|
if isinstance(typ, str):
|
||||||
if typ == 'choice':
|
if typ == 'choice':
|
||||||
|
if 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)
|
||||||
# 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 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)
|
||||||
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']
|
||||||
|
@ -185,7 +217,7 @@ class Argument:
|
||||||
|
|
||||||
def attrs(self):
|
def attrs(self):
|
||||||
# update any attributes set by processopt
|
# update any attributes set by processopt
|
||||||
attrs = 'default dest'.split()
|
attrs = 'default dest help'.split()
|
||||||
if self.dest:
|
if self.dest:
|
||||||
attrs.append(self.dest)
|
attrs.append(self.dest)
|
||||||
for attr in attrs:
|
for attr in attrs:
|
||||||
|
@ -193,6 +225,11 @@ class Argument:
|
||||||
self._attrs[attr] = getattr(self, attr)
|
self._attrs[attr] = getattr(self, attr)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
if self._attrs.get('help'):
|
||||||
|
a = self._attrs['help']
|
||||||
|
a = a.replace('%default', '%(default)s')
|
||||||
|
#a = a.replace('%prog', '%(prog)s')
|
||||||
|
self._attrs['help'] = a
|
||||||
return self._attrs
|
return self._attrs
|
||||||
|
|
||||||
def _set_opt_strings(self, opts):
|
def _set_opt_strings(self, opts):
|
||||||
|
|
Loading…
Reference in New Issue