-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
ARG001
/ARG002
being thrown for pytest
fixtures
#4673
Comments
ARG001
being thrown for pytest
fixturesARG001
/ARG002
being thrown for pytest
fixtures
This issue could be avoided by using pytest’s usefixtures decorator, see: https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#use-fixtures-in-classes-and-modules-with-usefixtures |
Oh I forgot about that! Using it no longer emits import csv
import pathlib
import pytest
THIS_DIRECTORY = pathlib.PurePath(__file__).parent
@pytest.fixture(name="this_dir_fs")
def fixture_this_dir_fs(fs):
fs.add_real_directory(THIS_DIRECTORY, read_only=False)
return fs
@pytest.mark.usefixtures("this_dir_fs")
def test_with_numpy_genfromtxt() -> None: # ARG001 is no longer thrown
csv_file = THIS_DIRECTORY / "stub.csv"
with open(csv_file, mode="w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows([(1, 2), (3, 4)]) Feel free to close this out, with the resolution to have |
Oh, great :) Thanks @calumy. |
So as I understand it, the problem with 'usefixtures' is that it can't be used on fixtures (fixtures may depend upon other fixtures in pytest) only on tests. That being the case, how would the ruff project feel about the following solution? If one were able to define a regular expression that if a function matched, then the unused argument check would be disabled. That would allow pytest ruff uses to define for example ''' And that would cause unused checking to be disabled for functions or methods that began with 'fix_' or 'test_'. And that would default to the empty string i.e. we would always check. I realise that this is somewhat ugly, but I suppose that pytest is pretty widely used and we are not the only ones suffering from this "unused argument" problem. Also, it would be basically invisible to anyone who didn't care about this problem, so the only cost would be the maintenance load of that code If the ruff project thinks that that is an okay idea, I could look at sending in a PR for it. Assuming it's doable with a reasonable amount of effort. (Apologies for any potential formatting or grammar problems here I'm tapping this out on my phone) |
Motivation
Here is an overcomplicated minimal repro of using a fake filesystem from
pyfakefs==5.2.2
, a very useful tool for unit testing with filesystems. I am usingpytest==7.3.1
.Aside: if you don't know
pyfakefs
, here's a snippet from its docs:The fake filesystem
pytest
fixture is not supposed to be used, it's more of an inclusion in the signature. It works more invisibly behind the scene, patching lots file I/O stuff.Thus, we run into false positive
ARG001
withruff==0.0.270
:Similarly, if the test is moved to a test method, the error code thrown will be
ARG002
.Request
pyfakefs
is one of many cases where apytest
fixture gets included in a test signature, but goes unused within the test's body.Is there anything ruff can do for this? Can
ruff
detect if an argument is apytest
fixture?I am wondering if
ARG001
/ARG002
should not be thrown forpytest
fixtures in signatures.For what it's worth,
pylint
'sunused-argument
has the same problem.The text was updated successfully, but these errors were encountered: