Skip to content

Commit f2bb3df

Browse files
Merge pull request #1645 from userzimmermann/sprint/addoption-check
added check for already existing option names to OptionGroup.addoption()
2 parents 7d60fcc + 69bab4a commit f2bb3df

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ Tom Viner
106106
Trevor Bekolay
107107
Wouter van Ackooy
108108
Bernard Pratz
109+
Stefan Zimmermann

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127

128128
*
129129

130+
* ``OptionGroup.addoption()`` now checks if option names were already
131+
added before, to make it easier to track down issues like `#1618`_.
132+
Before, you only got exceptions later from ``argparse`` library,
133+
giving no clue about the actual reason for double-added options.
134+
130135
.. _@milliams: https://github.com/milliams
131136
.. _@csaftoiu: https://github.com/csaftoiu
132137
.. _@flub: https://github.com/flub
@@ -161,6 +166,7 @@
161166
.. _#1628: https://github.com/pytest-dev/pytest/pull/1628
162167
.. _#1629: https://github.com/pytest-dev/pytest/issues/1629
163168
.. _#1633: https://github.com/pytest-dev/pytest/pull/1633
169+
.. _#1618: https://github.com/pytest-dev/pytest/issues/1618
164170

165171

166172
**Bug Fixes**

_pytest/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,10 @@ def addoption(self, *optnames, **attrs):
686686
results in help showing '--two-words' only, but --twowords gets
687687
accepted **and** the automatic destination is in args.twowords
688688
"""
689+
conflict = set(optnames).intersection(
690+
name for opt in self.options for name in opt.names())
691+
if conflict:
692+
raise ValueError("option names %s already added" % conflict)
689693
option = Argument(*optnames, **attrs)
690694
self._addoption_instance(option, shortupper=False)
691695

testing/test_parseopt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def test_group_addoption(self):
7777
assert len(group.options) == 1
7878
assert isinstance(group.options[0], parseopt.Argument)
7979

80+
def test_group_addoption_conflict(self):
81+
group = parseopt.OptionGroup("hello again")
82+
group.addoption("--option1", "--option-1", action="store_true")
83+
with pytest.raises(ValueError) as err:
84+
group.addoption("--option1", "--option-one", action="store_true")
85+
assert str(set(["--option1"])) in str(err.value)
86+
8087
def test_group_shortopt_lowercase(self, parser):
8188
group = parser.getgroup("hello")
8289
pytest.raises(ValueError, """

0 commit comments

Comments
 (0)