flake8-pytest-style
Description
A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
Currently the following errors are reported:
| Code | Description |
| ------- | ----------- |
| [PT001] | use @pytest.fixture over @pytest.fixture()
(configurable by pytest-fixture-no-parentheses) |
| [PT002] | configuration for fixture '{name}' specified via positional args, use kwargs |
| [PT003] | scope='function' is implied in @pytest.fixture() |
| [PT004] | fixture '{name}' does not return anything, add leading underscore |
| [PT005] | fixture '{name}' returns a value, remove leading underscore |
| [PT006] | wrong name(s) type in @pytest.mark.parametrize, expected {expected_type}
(configurable by pytest-parametrize-names-type) |
| [PT007] | wrong values type in @pytest.mark.parametrize, expected {expected_type}
(configurable by pytest-parametrize-values-type and pytest-parametrize-values-row-type) |
| [PT008] | use return_value= instead of patching with lambda |
| [PT009] | use a regular assert instead of unittest-style '{assertion}' |
| [PT010] | set the expected exception in pytest.raises() |
| [PT011] | pytest.raises({exception}) is too broad, set the match parameter or use a more specific exception
(configurable by pytest-raises-require-match-for) |
| [PT012] | pytest.raises() block should contain a single simple statement |
| [PT013] | found incorrect import of pytest, use simple 'import pytest' instead |
| [PT014] | found duplicate test cases {indexes} in @pytest.mark.parametrize |
| [PT015] | assertion always fails, replace with pytest.fail() |
| [PT016] | no message passed to pytest.fail() |
| [PT017] | found assertion on exception {name} in except block, use pytest.raises() instead |
| [PT018] | assertion should be broken down into multiple parts |
| [PT019] | fixture {name} without value is injected as parameter, use @pytest.mark.usefixtures instead |
| [PT020] | @pytest.yield_fixture is deprecated, use @pytest.fixture |
| [PT021] | use yield instead of request.addfinalizer |
| [PT022] | no teardown in fixture {name}, use return instead of yield |
| [PT023] | use @pytest.mark.foo over @pytest.mark.foo()
(configurable by pytest-mark-no-parentheses) |
| [PT024] | pytest.mark.asyncio is unnecessary for fixtures |
| [PT025] | pytest.mark.usefixtures has no effect on fixtures |
| [PT026] | useless pytest.mark.usefixtures without parameters |
| [PT027] | use pytest.raises() instead of unittest-style '{assertion}' |
| [PT028] | test function {name} has default value for argument {arg}, remove it |
| [PT029] | set the expected warning in pytest.warns() |
| [PT030] | pytest.warns({warning}) is too broad, set the match parameter or use a more specific warning
(configurable by pytest-warns-require-match-for) |
| [PT031] | pytest.warns() block should contain a single simple statement |
Installation and usage
As a dependency
Install the plugin using the package manager of your choice, e.g.
uv add --group dev flake8-pytest-style
or
poetry add --group dev flake8-pytest-style
or
pip install flake8-pytest-style
and then run flake8 as you normally would.
As a pre-commit hook
The plugin is a flake8 extension, not a standalone tool. To use it with
pre-commit, add the plugin to the
additional_dependencies section of the official flake8 hook:
repos:
- repo: https://github.com/PyCQA/flake8
rev: 7.3.0
hooks:
- id: flake8
additional_dependencies:
- flake8-pytest-style==2.2.0
Configuration
The plugin is configured via flake8 configuration
file and/or CLI options. Specific errors can be selected or ignored;
there are also the following plugin-specific configuration options:
pytest-fixture-no-parentheses— see [PT001]pytest-parametrize-names-type— see [PT006]pytest-parametrize-values-type— see [PT007]pytest-parametrize-values-row-type— see [PT007]pytest-raises-require-match-for— see [PT011]pytest-mark-no-parentheses— see [PT023]pytest-warns-require-match-for— see [PT030]
For developers
Install deps and setup pre-commit hook
make init
Run linters, autoformat, tests etc.
make format lint test
Bump new version
make bump_major make bump_minor make bump_patch
License
MIT
Change Log
Unreleased
...
2.2.0 - 2025-10-20
- require at least Python 3.10
- support Python 3.14
- support
reason=kwarg inpytest.failfor [PT016] - add [PT028] (checks for default values in test functions)
- add [PT029] (checks for
pytest.warnswithout expected warning) - add [PT030] (checks for too broad
pytest.warnsclauses) - add [PT031] (checks for multiple statements in
pytest.warnsblocks) - require at least Python 3.9
- support Python 3.13
- BREAKING: invert default values for
pytest-fixture-no-parenthesesandpytest-mark-no-parentheses
pytest official style
* If you get a lot of [PT001] or [PT023] violations after upgrading, consider setting explicit values
for these configuration options
- require at least Python 3.8.1
- support Python 3.12
- fix false positive for [PT009] on
pytest.fail
- update list of unittest-style assert methods for [PT009]/[PT027]
- require at least Python 3.7.2
- support Python 3.11
- add [PT027] (checks for unittest-style
assertRaises)
- require at least Python 3.6.2
- expose
py.typedfile
- better wording for [PT011]
- support Python 3.10
- add [PT025] (checks for erroneous
pytest.mark.usefixtureson fixtures) - add [PT026] (checks for
pytest.mark.usefixtureswithout parameters)
- fix [PT023] not checking marks in classes
- fix [PT004] incorrectly firing on fixtures with
yield from
- update
flake8-plugin-utilsversion to improve stability
- fix argparse-related warnings
- add [PT023] (checks for parentheses consistency in
pytest.markusage) - add [PT024] (checks for unnecessary
pytest.mark.asyncioon fixtures) - fix [PT004], [PT005] firing on abstract fixtures
- fix [PT012] firing on
withstatements containing a singlepass
- add [PT022] (checks for
yieldfixtures without teardown)
- update
flake8-plugin-utilsdependency to fix encoding problems on Windows
- fix [PT004]/[PT005] inspecting returns of nested functions
- fix [PT021] for factory fixtures (#46)
- support scoped
mockerfixtures frompytest-mockfor [PT008] - check for positional-only lambda arguments in [PT008]
- add [PT020] (checks for
pytest.yield_fixture) - add [PT021] (checks for
request.addfinalizer) - add documentation pages for all rules
- fix [PT011] not reporting
match=''as a violation
- add [PT015] (checks for
assert False) - add [PT016] (checks for
pytest.fail()without message) - add [PT017] (checks for assertions on exceptions in
exceptblocks) - add [PT018] (checks for composite assertions)
- add [PT019] (checks for fixtures without value injected as parameters)
- add [PT014] (checks for duplicate test cases in
@pytest.mark.parametrize)
- add configuration option
pytest-parametrize-names-typefor [PT006] - add configuration options
pytest-parametrize-values-typeand
pytest-parametrize-values-row-type for [PT007]
0.5.0 - 2020-03-09
- add configuration option
pytest-fixture-no-parenthesesfor [PT001] - add [PT013] (checks for
from-imports frompytest)
- add [PT012] (checks for multiple statements in
with pytest.raises())
- fix default value of
pytest-raises-require-match-forconfig option
- add [PT010] and [PT011] (checks for
pytest.raisesparameters)
- add [PT009] (ported from flake8-pytest)
- add
yieldfixtures support - fix changelog entry for 0.1.2
- fix parametrize checkers not working in decorators
- update PyPI description
- initial