astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.12k stars 1.03k forks source link

Use pytest file discovery logic to apply `PT` rules #8794

Open dhruvmanila opened 10 months ago

dhruvmanila commented 10 months ago

For additional context, refer to https://github.com/astral-sh/ruff/issues/8145#issuecomment-1817657808 and the following comments.

For me that would not help as I do not have a tests/ directory. Instead, I have test files next to the files to be tested. e.g. mypkg/util/string.py has its test in mypkg/util/string_test.py and so on. I think for larger applications this is not uncommon.

I think ideally it would apply the pytest rules for test discovery, ideally with override settings or parsing of the respective pytest.ini options.

I think mirroring the pytest rules for discovery would be sensible. It would also solve #8703, and arguably solve #6474.

However, I definitely want to avoid parsing pytest.ini and interpreting pytest configuration -- it's just a huge pain to implement and maintain (and also requires that we know the pytest version and keep our settings in-sync with pytest's settings). So if we did do this, we'd need to stick to the defaults, or add our own limited configuration.

flying-sheep commented 1 month ago

And the other way around, some rules never make sense for pytest, and should always be disabled:

[tool.ruff.lint.per-file-ignores]
'tests/**/*.py' = [
    'INP001', # Test directories are not namespace packages
    'S101',   # Pytest tests should use `assert`
    'RUF018', # Assignment expressions in assert are fine here
]