Skip to content

Commit

Permalink
Extend addopts rather than overwrite (#627)
Browse files Browse the repository at this point in the history
* Allow addopts to be extended rather than overwritten

* Add simple test for addopts overrides

* Add note to docs on addopts behaviour

* Fix test for Windows

* Concatenate sequences rather than extend in-place
  • Loading branch information
tcbegley authored Jul 21, 2021
1 parent 75bbb8a commit 67352b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ or you can put the following in your :code:`pyproject.toml` file
"--extend-ignore=E203"
]
.. note::
If you specify extra flags via both the :code:`pyproject.toml` file and the command-line, both will be passed on to the underlying command-line tool,
with the options specified in :code:`pyproject.toml` passed first. In this case the exact behaviour will depend on the tool and the option in question.
It's common that subsequent flags override earlier ones, but check the documentation for the tool and option in question to be sure.

Allow mutations
~~~~~~~~~~~~~~~

Expand Down
8 changes: 6 additions & 2 deletions nbqa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,12 @@ def _get_configs(cli_args: CLIArgs, project_root: Path) -> Configs:
# If a section was passed via CLI, use that.
for section in config:
if getattr(cli_args, section) is not None:
# TypedDict key must be a string literal
config[section] = getattr(cli_args, section) # type: ignore
if section == "addopts":
# addopts are added to / overridden rather than replaced outright
config["addopts"] = (*config["addopts"], *getattr(cli_args, section))
else:
# TypedDict key must be a string literal
config[section] = getattr(cli_args, section) # type: ignore

# add default options
if cli_args.command == "isort":
Expand Down
18 changes: 14 additions & 4 deletions tests/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_pyproject_toml_works(capsys: "CaptureFixture") -> None:
assert out == expected_out


def test_cli_overwrites_pyprojecttoml(capsys: "CaptureFixture") -> None:
def test_cli_extends_pyprojecttoml(capsys: "CaptureFixture") -> None:
"""
Check CLI args overwrite pyproject.toml
Expand All @@ -64,11 +64,21 @@ def test_cli_overwrites_pyprojecttoml(capsys: "CaptureFixture") -> None:
[
"flake8",
os.path.join("tests", "data", "notebook_for_testing.ipynb"),
"--select=E303",
"--ignore=E402,W291",
]
)
out, _ = capsys.readouterr()
Path("pyproject.toml").unlink()

out, _ = capsys.readouterr()
expected_out = ""
# if arguments are specified on command line, they will take precedence
# over those specified in the pyproject.toml
notebook_path = os.path.join("tests", "data", "notebook_for_testing.ipynb")
expected_out = dedent(
f"""\
{notebook_path}:cell_1:1:1: F401 'os' imported but unused
{notebook_path}:cell_1:3:1: F401 'glob' imported but unused
{notebook_path}:cell_1:5:1: F401 'nbqa' imported but unused
{notebook_path}:cell_4:1:1: F401 'random.randint' imported but unused
"""
)
assert out == expected_out

0 comments on commit 67352b9

Please sign in to comment.