Skip to content
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

drop 3.7 support, migrate to ruff format #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: set up python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.12'

- run: pip install -r requirements/linting.txt

Expand All @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
- name: set up python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.12'

- name: install
run: pip install -U twine build
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ repos:
types: [python]
language: system
exclude: cases_update
- id: black
name: Black
entry: black
- id: ruff format
name: Ruff Format
entry: ruff
types: [python]
args: [format]
language: system
exclude: cases_update
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ update-lockfiles:

.PHONY: format
format:
black $(sources)
ruff format $(sources)
ruff $(sources) --fix --exit-zero

.PHONY: lint
lint:
black $(sources) --check --diff
ruff format $(sources) --check
ruff $(sources)

.PHONY: test
Expand Down
14 changes: 2 additions & 12 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,8 @@ print(b'hello world')
```py
import string

print(
[
string.ascii_letters[: i + 10]
for i in range(4)
]
)
print([string.ascii_letters[: i + 10] for i in range(4)])
"""
[
'abcdefghij',
'abcdefghijk',
'abcdefghijkl',
'abcdefghijklm',
]
['abcdefghij', 'abcdefghijk', 'abcdefghijkl', 'abcdefghijklm']
"""
```
17 changes: 6 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ classifiers = [
'Framework :: Pytest',
'Topic :: Software Development :: Libraries :: Python Modules',
]
requires-python = '>=3.7'
requires-python = '>=3.8'
dynamic = ['version']
dependencies = [
'pytest>=7',
'black>=23',
'ruff>=0.0.258',
'ruff>=0.1,<0.2',
]

[project.entry-points.pytest11]
Expand All @@ -68,7 +67,10 @@ extend-select = ['Q', 'RUF100', 'C90', 'UP', 'I']
flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}
mccabe = { max-complexity = 14 }
isort = { known-first-party = ['pytest_examples'] }
target-version = 'py37'
target-version = 'py38'

[tool.ruff.format]
quote-style = 'single'

[tool.coverage.run]
source = ['pytest_examples']
Expand All @@ -82,10 +84,3 @@ exclude_lines = [
'if TYPE_CHECKING:',
'@overload',
]

[tool.black]
exclude = '.*/cases_update/.*'
color = true
line-length = 120
target-version = ['py310']
skip-string-normalization = true
27 changes: 17 additions & 10 deletions pytest_examples/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,31 @@ def hash(self) -> str:

def ruff_config(self) -> tuple[str, ...]:
config_lines = []
format_config_lines = []
select = []
ignore = []
args = []

# line length is enforced by black
if self.ruff_line_length is None:
# if not ruff line length, ignore E501 which is line length errors
# by default, ruff sets the line length to 88
ignore.append('E501')
else:
args.append(f'--line-length={self.ruff_line_length}')
config_lines.append(f'line-length = {self.ruff_line_length}')

if self.ruff_select:
select.extend(self.ruff_select)

if self.quotes == 'single':
# enforce single quotes using ruff, black will enforce double quotes
if self.quotes != 'either':
select.append('Q')
config_lines.append("flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}")
config_lines.append(f"flake8-quotes = {{inline-quotes = '{self.quotes}', multiline-quotes = 'double'}}")
format_config_lines.append(f"quote-style = '{self.quotes}'")
else:
format_config_lines.append('preview = true')
format_config_lines.append("quote-style = 'preserve'")

if self.target_version:
args.append(f'--target-version={self.target_version}')
config_lines.append(f"target-version = '{self.target_version}'")

if self.upgrade:
select.append('UP')
Expand All @@ -76,9 +79,13 @@ def ruff_config(self) -> tuple[str, ...]:

if select:
# use extend to not disable default select
args.append(f'--extend-select={",".join(select)}')
config_lines.append(f'extend-select = [{",".join(f"'{s}'" for s in select)}]')
if ignore:
args.append(f'--ignore={",".join(ignore)}')
config_lines.append(f'ignore = [{",".join(f"'{i}'" for i in ignore)}]')

if format_config_lines:
config_lines.append('[format]')
config_lines.extend(format_config_lines)

if config_lines:
config_toml = '\n'.join(config_lines)
Expand All @@ -87,6 +94,6 @@ def ruff_config(self) -> tuple[str, ...]:
config_file.parent.mkdir(parents=True, exist_ok=True)
config_file.write_text(config_toml)

args.append(f'--config={config_file}')
return (f'--config={config_file}',)

return tuple(args)
return ()
58 changes: 5 additions & 53 deletions pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from _pytest.outcomes import Failed as PytestFailed

from .config import DEFAULT_LINE_LENGTH, ExamplesConfig
from .lint import FormatError, black_check, black_format, ruff_check, ruff_format
from .lint import FormatError, ruff_check, ruff_fix, ruff_format
from .run_code import InsertPrintStatements, run_code

if TYPE_CHECKING:
Expand Down Expand Up @@ -166,31 +166,7 @@ def _run(

def lint(self, example: CodeExample) -> None:
"""
Lint the example with black and ruff.

:param example: The example to lint.
"""
self.lint_black(example)
self.lint_ruff(example)

def lint_black(self, example: CodeExample) -> None:
"""
Lint the example using black.

:param example: The example to lint.
"""
example.test_id = self._test_id
try:
black_check(example, self.config)
except FormatError as exc:
raise PytestFailed(str(exc), pytrace=False) from None

def lint_ruff(
self,
example: CodeExample,
) -> None:
"""
Lint the example using ruff.
Lint the example with ruff.

:param example: The example to lint.
"""
Expand All @@ -202,39 +178,15 @@ def lint_ruff(

def format(self, example: CodeExample) -> None:
"""
Format the example with black and ruff, requires `--update-examples`.
Format the example with ruff, requires `--update-examples`.

:param example: The example to format.
"""
self.format_ruff(example)
self.format_black(example)

def format_black(self, example: CodeExample) -> None:
"""
Format the example using black, requires `--update-examples`.

:param example: The example to lint.
"""
self._check_update(example)

new_content = black_format(example.source, self.config, remove_double_blank=example.in_py_file())
if new_content != example.source:
example.source = new_content
self._mark_for_update(example)

def format_ruff(
self,
example: CodeExample,
) -> None:
"""
Format the example using ruff, requires `--update-examples`.

:param example: The example to lint.
"""
self._check_update(example)

try:
new_content = ruff_format(example, self.config)
new_content = ruff_fix(example, self.config)
new_content = ruff_format(new_content, self.config, remove_double_blank=example.in_py_file())
except FormatError as exc:
raise PytestFailed(str(exc), pytrace=False) from None
else:
Expand Down
Loading
Loading