Skip to content

added check for already existing option names to OptionGroup.addoption() #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ Tom Viner
Trevor Bekolay
Wouter van Ackooy
Bernard Pratz
Stefan Zimmermann
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@

*

* ``OptionGroup.addoption()`` now checks if option names were already
added before, to make it easier to track down issues like `#1618`_.
Before, you only got exceptions later from ``argparse`` library,
giving no clue about the actual reason for double-added options.

.. _@milliams: https://github.com/milliams
.. _@csaftoiu: https://github.com/csaftoiu
.. _@flub: https://github.com/flub
Expand Down Expand Up @@ -161,6 +166,7 @@
.. _#1628: https://github.com/pytest-dev/pytest/pull/1628
.. _#1629: https://github.com/pytest-dev/pytest/issues/1629
.. _#1633: https://github.com/pytest-dev/pytest/pull/1633
.. _#1618: https://github.com/pytest-dev/pytest/issues/1618


**Bug Fixes**
Expand Down
4 changes: 4 additions & 0 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@ def addoption(self, *optnames, **attrs):
results in help showing '--two-words' only, but --twowords gets
accepted **and** the automatic destination is in args.twowords
"""
conflict = set(optnames).intersection(
name for opt in self.options for name in opt.names())
if conflict:
raise ValueError("option names %s already added" % conflict)
option = Argument(*optnames, **attrs)
self._addoption_instance(option, shortupper=False)

Expand Down
7 changes: 7 additions & 0 deletions testing/test_parseopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def test_group_addoption(self):
assert len(group.options) == 1
assert isinstance(group.options[0], parseopt.Argument)

def test_group_addoption_conflict(self):
group = parseopt.OptionGroup("hello again")
group.addoption("--option1", "--option-1", action="store_true")
with pytest.raises(ValueError) as err:
group.addoption("--option1", "--option-one", action="store_true")
assert str(set(["--option1"])) in str(err.value)

def test_group_shortopt_lowercase(self, parser):
group = parser.getgroup("hello")
pytest.raises(ValueError, """
Expand Down