{"id":8166,"library":"flake8-pytest-style","title":"flake8-pytest-style","description":"flake8-pytest-style is a flake8 plugin designed to enforce common style guidelines and identify inconsistencies within pytest-based test suites. It helps maintain clean and idiomatic pytest code by reporting a variety of style violations. The current version is 2.2.0, released in October 2025, and it maintains a somewhat regular release cadence, with major/minor updates typically every 3-9 months.","status":"active","version":"2.2.0","language":"en","source_language":"en","source_url":"https://github.com/m-burst/flake8-pytest-style","tags":["flake8","pytest","linter","code quality","testing","plugin"],"install":[{"cmd":"pip install flake8-pytest-style","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core linter that this plugin extends.","package":"flake8","optional":false},{"reason":"The testing framework whose style this plugin checks.","package":"pytest","optional":false}],"imports":[{"note":"The PT013 rule discourages 'from pytest import ...' in favor of a simple 'import pytest' to avoid implicit imports and make dependencies clearer.","wrong":"from pytest import fixture, mark","symbol":"pytest","correct":"import pytest"}],"quickstart":{"code":"import os\n\n# Create a dummy test file\nwith open('test_example.py', 'w') as f:\n    f.write(\"\"\"\nimport pytest\n\n@pytest.fixture\ndef my_fixture(scope='function'): # PT001, PT003\n    return 1\n\ndef test_example(my_fixture): # PT004\n    assert my_fixture == 1\n\n@pytest.mark.parametrize(('val'), [1,2]) # PT023, PT006\ndef test_parametrized_bad_style(val=10): # PT028\n    assert val > 0\n\ndef test_raises():\n    with pytest.raises(Exception):\n        pass # PT010, PT011, PT012\n\"\"\")\n\n# Run flake8 with the plugin enabled\n# flake8 automatically discovers installed plugins\nprint(\"\\n--- Running flake8 ---\")\nos.system(\"flake8 test_example.py\")\n\n# Clean up\nos.remove('test_example.py')","lang":"python","description":"Install the plugin, then run `flake8` as you normally would. The plugin automatically integrates with flake8 to check pytest-related style issues in your project."},"warnings":[{"fix":"If you experience new PT001 or PT023 violations, you may need to explicitly configure these options in your `flake8` configuration (e.g., `setup.cfg`, `pyproject.toml`) to match your preferred style, or update your code to include/remove parentheses as per the new default.","message":"Version 2.0.0 (released 2024-04-01) inverted the default values for `pytest-fixture-no-parentheses` (PT001) and `pytest-mark-no-parentheses` (PT023). This change aligns with the official pytest style, but may introduce many new violations upon upgrade.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your project's Python environment meets the minimum version requirement for the `flake8-pytest-style` version you are using. Update your Python environment if necessary.","message":"The minimum Python version requirement has increased across minor and major versions. Version 2.0.0 requires Python >=3.8.1, version 2.1.0 requires Python >=3.9, and version 2.2.0 requires Python >=3.10.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"While `flake8-pytest-style` still reports these, consider if these specific rules fit your project's conventions. You can selectively ignore them via `flake8` configuration if they conflict with your established patterns or other linter's recommendations.","message":"Rules PT004 (fixture without return value should have leading underscore) and PT005 (fixture returning value should not have leading underscore) have been noted by the Ruff linter team (a popular alternative to flake8) as potentially not aligning with all pytest 'best practices'.","severity":"gotcha","affected_versions":"All"},{"fix":"Remove default values from test function parameters that are intended to receive fixtures. Fixtures should be injected without an explicit default.","message":"Rule PT028 warns against defining default values for parameters in test functions (e.g., `def test_foo(bar=42):`). Pytest's behavior is to use the default value instead of injecting a fixture if one is defined for that parameter, leading to unexpected test behavior.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change `@pytest.fixture()` to `@pytest.fixture` in your code, or set `pytest-fixture-no-parentheses = false` in your `flake8` configuration to revert to the old behavior.","cause":"After upgrading to `flake8-pytest-style` v2.0.0+, the default for `pytest-fixture-no-parentheses` was inverted. It now prefers `@pytest.fixture` without parentheses when no arguments are passed.","error":"PT001 use @pytest.fixture over @pytest.fixture()"},{"fix":"Replace specific imports from `pytest` with `import pytest` and then use `pytest.fixture`, `pytest.mark`, etc.","cause":"You are using `from pytest import ...` (e.g., `from pytest import fixture`) which `flake8-pytest-style` discourages.","error":"PT013 found incorrect import of pytest, use simple 'import pytest' instead"},{"fix":"Remove the default value from the test function parameter. For example, change `def test_foo(bar=42):` to `def test_foo(bar):`.","cause":"A test function parameter has a default value, which conflicts with pytest's fixture injection mechanism. Pytest will use the default value instead of the fixture.","error":"PT028 default values in test functions"},{"fix":"The recommended approach is to define reusable fixtures in a `conftest.py` file or use `pytest_plugins = [...]` in `conftest.py` to load them. If you must import, add `# noqa: F401` to the import line to suppress the warning.","cause":"This is a standard `flake8` (PyFlakes) error, often seen when importing pytest fixtures from a separate file (not `conftest.py`) into your test module. Fixtures are used by injection, not direct calls, making the import appear 'unused' to `flake8`.","error":"F401 'my_fixture' imported but unused"}]}