Skip to content

Commit

Permalink
Invert defaults for pytest-fixture-no-parentheses and `pytest-mark-…
Browse files Browse the repository at this point in the history
…no-parentheses` (#280)
  • Loading branch information
m-burst committed Apr 1, 2024
1 parent 705c2ad commit 87ac31f
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 76 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ MIT

**Unreleased**

* **BREAKING:** invert default values for `pytest-fixture-no-parentheses` and `pytest-mark-no-parentheses`
to conform with `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

Expand Down
14 changes: 7 additions & 7 deletions docs/rules/PT001.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# PT001

`use @pytest.fixture() over @pytest.fixture`
`use @pytest.fixture over @pytest.fixture()`

## Configuration

* `pytest-fixture-no-parentheses`
Boolean flag specifying whether `@pytest.fixture()` without parameters
should have parentheses.
If the option is set to false (the default), `@pytest.fixture()` is valid
and `@pytest.fixture` is an error.
If set to true, `@pytest.fixture` is valid and `@pytest.fixture()` is
If the option is set to true (the default), `@pytest.fixture` is valid
and `@pytest.fixture()` is an error.
If set to false, `@pytest.fixture()` is valid and `@pytest.fixture` is
an error.

## Examples

Bad code (assuming `pytest-fixture-no-parentheses` set to false):
Bad code (assuming `pytest-fixture-no-parentheses` set to true):

```python
import pytest

@pytest.fixture
@pytest.fixture()
def my_fixture():
...
```
Expand All @@ -29,7 +29,7 @@ Good code:
```python
import pytest

@pytest.fixture()
@pytest.fixture
def my_fixture():
...
```
Expand Down
14 changes: 7 additions & 7 deletions docs/rules/PT023.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# PT023

`use @pytest.mark.foo() over @pytest.mark.foo`
`use @pytest.mark.foo over @pytest.mark.foo()`

## Configuration

* `pytest-mark-no-parentheses`
Boolean flag specifying whether `@pytest.mark.foo()` without parameters
should have parentheses.
If the option is set to false (the default), `@pytest.mark.foo()` is valid
and `@pytest.mark.foo` is an error.
If set to true, `@pytest.mark.foo` is valid and `@pytest.mark.foo()` is
If the option is set to true (the default), `@pytest.mark.foo` is valid
and `@pytest.mark.foo()` is an error.
If set to false, `@pytest.mark.foo()` is valid and `@pytest.mark.foo` is
an error.

## Examples

Bad code (assuming `pytest-mark-no-parentheses` set to false):
Bad code (assuming `pytest-mark-no-parentheses` set to true):

```python
import pytest

@pytest.mark.foo
@pytest.mark.foo()
def test_something():
...
```
Expand All @@ -29,7 +29,7 @@ Good code:
```python
import pytest

@pytest.mark.foo()
@pytest.mark.foo
def test_something():
...
```
Expand Down
4 changes: 2 additions & 2 deletions flake8_pytest_style/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Config(NamedTuple):


DEFAULT_CONFIG = Config(
fixture_parentheses=True,
fixture_parentheses=False,
raises_require_match_for=[
'BaseException',
'Exception',
Expand All @@ -45,5 +45,5 @@ class Config(NamedTuple):
parametrize_names_type=ParametrizeNamesType.TUPLE,
parametrize_values_type=ParametrizeValuesType.LIST,
parametrize_values_row_type=ParametrizeValuesRowType.TUPLE,
mark_parentheses=True,
mark_parentheses=False,
)
16 changes: 9 additions & 7 deletions tests/test_PT001_incorrect_fixture_parentheses_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from flake8_pytest_style.errors import IncorrectFixtureParenthesesStyle
from flake8_pytest_style.visitors import FixturesVisitor

# make the configs independent of the actual default
_CONFIG_WITHOUT_PARENS = DEFAULT_CONFIG._replace(fixture_parentheses=False)
_CONFIG_WITH_PARENS = DEFAULT_CONFIG._replace(fixture_parentheses=True)


def test_ok_no_parameters():
code = """
Expand All @@ -13,7 +17,7 @@ def test_ok_no_parameters():
def my_fixture():
return 0
"""
assert_not_error(FixturesVisitor, code, config=DEFAULT_CONFIG)
assert_not_error(FixturesVisitor, code, config=_CONFIG_WITH_PARENS)


def test_ok_with_parameters():
Expand All @@ -24,7 +28,7 @@ def test_ok_with_parameters():
def my_fixture():
return 0
"""
assert_not_error(FixturesVisitor, code, config=DEFAULT_CONFIG)
assert_not_error(FixturesVisitor, code, config=_CONFIG_WITH_PARENS)


def test_ok_without_parens():
Expand All @@ -35,8 +39,7 @@ def test_ok_without_parens():
def my_fixture():
return 0
"""
config = DEFAULT_CONFIG._replace(fixture_parentheses=False)
assert_not_error(FixturesVisitor, code, config=config)
assert_not_error(FixturesVisitor, code, config=_CONFIG_WITHOUT_PARENS)


def test_error_without_parens():
Expand All @@ -51,7 +54,7 @@ def my_fixture():
FixturesVisitor,
code,
IncorrectFixtureParenthesesStyle,
config=DEFAULT_CONFIG,
config=_CONFIG_WITH_PARENS,
expected_parens='()',
actual_parens='',
)
Expand All @@ -65,12 +68,11 @@ def test_error_with_parens():
def my_fixture():
return 0
"""
config = DEFAULT_CONFIG._replace(fixture_parentheses=False)
assert_error(
FixturesVisitor,
code,
IncorrectFixtureParenthesesStyle,
config=config,
config=_CONFIG_WITHOUT_PARENS,
expected_parens='',
actual_parens='()',
)
2 changes: 1 addition & 1 deletion tests/test_PT002_fixture_positional_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_ok_no_args():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def my_fixture():
return 0
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_PT003_extraneous_scope_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_ok_no_scope():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def my_fixture():
return 0
"""
Expand Down
16 changes: 8 additions & 8 deletions tests/test_PT004_missing_fixture_name_underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_ok_simple():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _patch_something(mocker):
mocker.patch('some.thing')
"""
Expand All @@ -20,7 +20,7 @@ def test_ok_with_return():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _patch_something(mocker):
if something:
return
Expand All @@ -33,7 +33,7 @@ def test_ok_with_yield():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _activate_context():
with context:
yield
Expand All @@ -48,7 +48,7 @@ def test_ok_abstract_with_import_abc():
import pytest
class BaseTest:
@pytest.fixture()
@pytest.fixture
@abc.abstractmethod
def my_fixture():
raise NotImplementedError
Expand All @@ -63,7 +63,7 @@ def test_ok_abstract_with_from_import():
import pytest
class BaseTest:
@pytest.fixture()
@pytest.fixture
@abstractmethod
def my_fixture():
raise NotImplementedError
Expand All @@ -75,7 +75,7 @@ def test_ok_ignoring_yield_from():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def my_fixture():
yield from some_generator()
"""
Expand All @@ -86,7 +86,7 @@ def test_error_simple():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def patch_something(mocker):
mocker.patch('some.thing')
"""
Expand All @@ -103,7 +103,7 @@ def test_error_with_yield():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def activate_context():
with context:
yield
Expand Down
16 changes: 8 additions & 8 deletions tests/test_PT005_incorrect_fixture_name_underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_ok_with_return():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def my_fixture(mocker):
return 0
"""
Expand All @@ -20,7 +20,7 @@ def test_ok_with_yield():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def activate_context():
with get_context() as context:
yield context
Expand All @@ -30,7 +30,7 @@ def activate_context():

def test_ok_nested_function():
code = """
@pytest.fixture()
@pytest.fixture
def _any_fixture(mocker):
def nested_function():
return 1
Expand All @@ -47,7 +47,7 @@ def test_ok_abstract_with_import_abc():
import pytest
class BaseTest:
@pytest.fixture()
@pytest.fixture
@abc.abstractmethod
def _my_fixture():
return NotImplemented
Expand All @@ -62,7 +62,7 @@ def test_ok_abstract_with_from_import():
import pytest
class BaseTest:
@pytest.fixture()
@pytest.fixture
@abstractmethod
def _my_fixture():
return NotImplemented
Expand All @@ -74,7 +74,7 @@ def test_error_with_return():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _my_fixture(mocker):
return 0
"""
Expand All @@ -91,7 +91,7 @@ def test_error_with_yield():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _activate_context():
with get_context() as context:
yield context
Expand All @@ -109,7 +109,7 @@ def test_error_with_conditional_yield_from():
code = """
import pytest
@pytest.fixture()
@pytest.fixture
def _activate_context():
if some_condition:
with get_context() as context:
Expand Down
22 changes: 14 additions & 8 deletions tests/test_PT020_deprecated_yield_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from flake8_pytest_style.errors import DeprecatedYieldFixture
from flake8_pytest_style.visitors import FixturesVisitor

# make the configs independent of the actual default
_CONFIG_WITHOUT_PARENS = DEFAULT_CONFIG._replace(fixture_parentheses=False)
_CONFIG_WITH_PARENS = DEFAULT_CONFIG._replace(fixture_parentheses=True)


def test_ok_no_parameters():
code = """
Expand All @@ -13,7 +17,7 @@ def test_ok_no_parameters():
def my_fixture():
return 0
"""
assert_not_error(FixturesVisitor, code, config=DEFAULT_CONFIG)
assert_not_error(FixturesVisitor, code, config=_CONFIG_WITH_PARENS)


def test_ok_without_parens():
Expand All @@ -24,28 +28,30 @@ def test_ok_without_parens():
def my_fixture():
return 0
"""
config = DEFAULT_CONFIG._replace(fixture_parentheses=False)
assert_not_error(FixturesVisitor, code, config=config)
assert_not_error(FixturesVisitor, code, config=_CONFIG_WITHOUT_PARENS)


def test_error_without_parens():
code = """
import pytest
@pytest.yield_fixture()
@pytest.yield_fixture
def my_fixture():
return 0
"""
assert_error(FixturesVisitor, code, DeprecatedYieldFixture, config=DEFAULT_CONFIG)
assert_error(
FixturesVisitor, code, DeprecatedYieldFixture, config=_CONFIG_WITHOUT_PARENS
)


def test_error_with_parens():
code = """
import pytest
@pytest.yield_fixture
@pytest.yield_fixture()
def my_fixture():
return 0
"""
config = DEFAULT_CONFIG._replace(fixture_parentheses=False)
assert_error(FixturesVisitor, code, DeprecatedYieldFixture, config=config)
assert_error(
FixturesVisitor, code, DeprecatedYieldFixture, config=_CONFIG_WITH_PARENS
)
Loading

0 comments on commit 87ac31f

Please sign in to comment.