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 1 commit
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
16 changes: 6 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ classifiers = [
'Framework :: Pytest',
'Topic :: Software Development :: Libraries :: Python Modules',
]
requires-python = '>=3.7'
requires-python = '>=3.8'
dynamic = ['version']
dependencies = [
'pytest>=7',
'black>=23',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess you can remove black as a dependency now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

black_format still exists as a function for now, I want to push that as a follow up PR to keep a smaller diff. "ruff format" in this context is for our CI, not for the functionality 😂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'll just add here as a follow-up commit.

'ruff>=0.0.258',
'ruff>=0.1,<0.2',
]

[project.entry-points.pytest11]
Expand All @@ -68,7 +68,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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use consistent quotes, double quotes would probably better, but we're using single quotes else where


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

[tool.black]
exclude = '.*/cases_update/.*'
color = true
line-length = 120
target-version = ['py310']
skip-string-normalization = true
39 changes: 11 additions & 28 deletions pytest_examples/run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def __call__(self, *args: Any, sep: str = ' ', **kwargs: Any) -> None:

class InsertPrintStatements:
def __init__(
self, python_path: Path, config: ExamplesConfig, enable: bool, print_callback: Callable[[str], str] | None
self,
python_path: Path,
config: ExamplesConfig,
enable: bool,
print_callback: Callable[[str], str] | None,
):
self.file = python_path
self.config = config
Expand Down Expand Up @@ -177,7 +181,12 @@ def _insert_print_statements(self, example: CodeExample) -> str:
return '\n'.join(lines) + '\n'

def _insert_print_args(
self, lines: list[str], statement: PrintStatement, in_python: bool, line_index: int, col: int
self,
lines: list[str],
statement: PrintStatement,
in_python: bool,
line_index: int,
col: int,
) -> None:
single_line = statement.sep.join(map(str, statement.args))
if self.print_callback:
Expand Down Expand Up @@ -205,21 +214,6 @@ def _insert_print_args(
triple_quotes_prefix_re = re.compile('^ *(?:"{3}|\'{3})', re.MULTILINE)


def find_print_line(lines: list[str], line_no: int) -> int:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to remove this.

"""
For 3.7 we have to reverse through lines to find the print statement lint
"""
if sys.version_info >= (3, 8):
return line_no

for back in range(100):
new_line_no = line_no - back
m = re.search(r'^ *print\(', lines[new_line_no - 1])
if m:
return new_line_no
return line_no


def remove_old_print(lines: list[str], line_index: int) -> None:
"""
Remove the old print statement.
Expand Down Expand Up @@ -253,17 +247,6 @@ def find_print_location(example: CodeExample, line_no: int) -> tuple[int, int]:
:param line_no: The line number on which the print statement starts (or approx on 3.7)
:return: tuple if `(line, column)` of the print statement
"""
# For 3.7 we have to reverse through lines to find the print statement lint
if sys.version_info < (3, 8):
# find the last print statement before the line
lines = example.source.splitlines()
for back in range(100):
new_line_no = line_no - back
m = re.match(r' *print\(', lines[new_line_no - 1])
if m:
line_no = new_line_no
break

m = ast.parse(example.source, filename=example.path.name)
return find_print(m, line_no) or (line_no, 0)

Expand Down
11 changes: 7 additions & 4 deletions pytest_examples/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ def create_example_traceback(exc: Exception, module_path: str, example: CodeExam

Frames outside the example are not included in the new traceback.
"""
if sys.version_info < (3, 8):
# f_code.co_posonlyargcount was added in 3.8
return None
frames = []
tb = exc.__traceback__
while tb is not None:
frame = tb.tb_frame
if frame.f_code.co_filename == module_path:
frames.append((create_custom_frame(frame, example), tb.tb_lasti, tb.tb_lineno + example.start_line))
frames.append(
(
create_custom_frame(frame, example),
tb.tb_lasti,
tb.tb_lineno + example.start_line,
)
)
tb = tb.tb_next

frames.reverse()
Expand Down
50 changes: 33 additions & 17 deletions requirements/linting.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements/linting.txt --resolver=backtracking requirements/linting.in
# pip-compile --output-file=requirements/linting.txt requirements/linting.in
#
black==23.1.0
aiohttp==3.9.1
# via black
aiosignal==1.3.1
# via aiohttp
attrs==23.1.0
# via aiohttp
black==23.12.0
# via -r requirements/linting.in
cfgv==3.3.1
cfgv==3.4.0
# via pre-commit
click==8.1.3
click==8.1.7
# via black
distlib==0.3.6
distlib==0.3.8
# via virtualenv
filelock==3.10.2
filelock==3.13.1
# via virtualenv
identify==2.5.21
frozenlist==1.4.1
# via
# aiohttp
# aiosignal
identify==2.5.33
# via pre-commit
idna==3.6
# via yarl
multidict==6.0.4
# via
# aiohttp
# yarl
mypy-extensions==1.0.0
# via black
nodeenv==1.7.0
nodeenv==1.8.0
# via pre-commit
packaging==23.0
packaging==23.2
# via black
pathspec==0.11.1
pathspec==0.12.1
# via black
platformdirs==3.1.1
platformdirs==4.1.0
# via
# black
# virtualenv
pre-commit==2.21.0
# via -r requirements/linting.in
pyyaml==6.0
pyyaml==6.0.1
# via pre-commit
ruff==0.0.258
ruff==0.1.8
# via -r requirements/linting.in
tomli==2.0.1
# via black
virtualenv==20.21.0
virtualenv==20.25.0
# via pre-commit
yarl==1.9.4
# via aiohttp

# The following packages are considered to be unsafe in a requirements file:
# setuptools
46 changes: 29 additions & 17 deletions requirements/pyproject.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements/pyproject.txt --resolver=backtracking pyproject.toml
# pip-compile --output-file=requirements/pyproject.txt pyproject.toml
#
attrs==22.2.0
# via pytest
black==23.1.0
aiohttp==3.9.1
# via black
aiosignal==1.3.1
# via aiohttp
attrs==23.1.0
# via aiohttp
black==23.12.0
# via pytest-examples (pyproject.toml)
click==8.1.3
click==8.1.7
# via black
exceptiongroup==1.1.1
# via pytest
frozenlist==1.4.1
# via
# aiohttp
# aiosignal
idna==3.6
# via yarl
iniconfig==2.0.0
# via pytest
multidict==6.0.4
# via
# aiohttp
# yarl
mypy-extensions==1.0.0
# via black
packaging==23.0
packaging==23.2
# via
# black
# pytest
pathspec==0.11.1
pathspec==0.12.1
# via black
platformdirs==3.1.1
platformdirs==4.1.0
# via black
pluggy==1.0.0
pluggy==1.3.0
# via pytest
pytest==7.2.2
pytest==7.4.3
# via pytest-examples (pyproject.toml)
tomli==2.0.1
# via
# black
# pytest
ruff==0.1.8
# via pytest-examples (pyproject.toml)
yarl==1.9.4
# via aiohttp
24 changes: 10 additions & 14 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements/testing.txt requirements/testing.in
#
coverage==7.2.2
coverage==7.3.4
# via -r requirements/testing.in
exceptiongroup==1.1.1
# via pytest
iniconfig==2.0.0
# via pytest
markdown-it-py==2.2.0
markdown-it-py==3.0.0
# via rich
mdurl==0.1.2
# via markdown-it-py
packaging==23.0
packaging==23.2
# via pytest
pluggy==1.0.0
pluggy==1.3.0
# via pytest
pygments==2.14.0
pygments==2.17.2
# via rich
pytest==7.4.2
pytest==7.4.3
# via pytest-pretty
pytest-pretty==1.1.1
pytest-pretty==1.2.0
# via -r requirements/testing.in
rich==13.3.2
rich==13.7.0
# via pytest-pretty
ruff==0.0.258
ruff==0.1.8
# via -r requirements/testing.in
tomli==2.0.1
# via pytest
2 changes: 1 addition & 1 deletion tests/test_run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample):
' my_file.md:2:8: F401 [*] `sys` imported but unused\n'
' my_file.md:3:7: F821 Undefined name `missing`\n'
' Found 2 errors.\n'
' [*] 1 potentially fixable with the --fix option.\n'
' [*] 1 fixable with the `--fix` option.\n'
'=== short test summary info ===\n'
) in output

Expand Down