From d2acbce250a9996addaaa72a88a186ce20ca42266e1f69bac015a3c9ed99eb4f Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Mon, 1 Jul 2024 18:03:41 +0000 Subject: [PATCH] - add ruff05.patch: fix exception with ruff 0.5.0 - add test-ruff-0.1.0.patch to fix tests with ruvv-0.1.0 - initial package OBS-URL: https://build.opensuse.org/package/show/devel:languages:python:pytest/python-pytest-examples?expand=0&rev=12 --- .gitattributes | 23 ++++ .gitignore | 1 + fix-traceback.patch | 223 +++++++++++++++++++++++++++++++++ pytest-examples-0.0.10.tar.gz | 3 + python-pytest-examples.changes | 34 +++++ python-pytest-examples.spec | 75 +++++++++++ ruff05.patch | 13 ++ support-python-312.patch | 68 ++++++++++ test-ruff-0.1.0.patch | 22 ++++ 9 files changed, 462 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 fix-traceback.patch create mode 100644 pytest-examples-0.0.10.tar.gz create mode 100644 python-pytest-examples.changes create mode 100644 python-pytest-examples.spec create mode 100644 ruff05.patch create mode 100644 support-python-312.patch create mode 100644 test-ruff-0.1.0.patch diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9b03811 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,23 @@ +## Default LFS +*.7z filter=lfs diff=lfs merge=lfs -text +*.bsp filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.gem filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.jar filter=lfs diff=lfs merge=lfs -text +*.lz filter=lfs diff=lfs merge=lfs -text +*.lzma filter=lfs diff=lfs merge=lfs -text +*.obscpio filter=lfs diff=lfs merge=lfs -text +*.oxt filter=lfs diff=lfs merge=lfs -text +*.pdf filter=lfs diff=lfs merge=lfs -text +*.png filter=lfs diff=lfs merge=lfs -text +*.rpm filter=lfs diff=lfs merge=lfs -text +*.tbz filter=lfs diff=lfs merge=lfs -text +*.tbz2 filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.ttf filter=lfs diff=lfs merge=lfs -text +*.txz filter=lfs diff=lfs merge=lfs -text +*.whl filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57affb6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.osc diff --git a/fix-traceback.patch b/fix-traceback.patch new file mode 100644 index 0000000..9a7e35e --- /dev/null +++ b/fix-traceback.patch @@ -0,0 +1,223 @@ +From 3fe64dc479d318fa58fd1952bd37ce9db2beb23f Mon Sep 17 00:00:00 2001 +From: Alex Hall +Date: Sun, 17 Sep 2023 17:35:21 +0200 +Subject: [PATCH 1/4] Use tb_lineno to point to correct line in traceback + +--- + pytest_examples/traceback.py | 14 +++++++------- + tests/test_run_examples.py | 9 ++++++--- + 2 files changed, 13 insertions(+), 10 deletions(-) + +diff --git a/pytest_examples/traceback.py b/pytest_examples/traceback.py +index 7dedfa1..591f995 100644 +--- a/pytest_examples/traceback.py ++++ b/pytest_examples/traceback.py +@@ -1,7 +1,6 @@ + from __future__ import annotations as _annotations + + import sys +-import traceback + from types import CodeType, FrameType, TracebackType + from typing import TYPE_CHECKING + +@@ -21,16 +20,17 @@ def create_example_traceback(exc: Exception, module_path: str, example: CodeExam + # f_code.co_posonlyargcount was added in 3.8 + return None + frames = [] +- for frame, _ in traceback.walk_tb(exc.__traceback__): ++ 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)) ++ frames.append((create_custom_frame(frame, example), tb.tb_lasti, tb.tb_lineno + example.start_line)) ++ tb = tb.tb_next + + frames.reverse() + new_tb = None +- for altered_frame in frames: +- new_tb = TracebackType( +- tb_next=new_tb, tb_frame=altered_frame, tb_lasti=altered_frame.f_lasti, tb_lineno=altered_frame.f_lineno +- ) ++ for altered_frame, lasti, lineno in frames: ++ new_tb = TracebackType(tb_next=new_tb, tb_frame=altered_frame, tb_lasti=lasti, tb_lineno=lineno) + return new_tb + + +diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py +index 6943329..9f077d7 100644 +--- a/tests/test_run_examples.py ++++ b/tests/test_run_examples.py +@@ -224,7 +224,10 @@ def test_run_directly(tmp_path, eval_example): + x = 4 + + def div(y): +- return x / y ++ try: ++ return x / y ++ finally: ++ str(y) + + div(2) + div(0)""" +@@ -244,10 +247,10 @@ def div(y): + + # debug(exc_info.traceback) + assert exc_info.traceback[-1].frame.code.path == md_file +- assert exc_info.traceback[-1].lineno == 6 ++ assert exc_info.traceback[-1].lineno == 7 + + assert exc_info.traceback[-2].frame.code.path == md_file +- assert exc_info.traceback[-2].lineno == 9 ++ assert exc_info.traceback[-2].lineno == 12 + + + def test_print_sub(pytester: pytest.Pytester): + +From e07b1538df6eb662c2b92b9da9b768a2cc1f3ed3 Mon Sep 17 00:00:00 2001 +From: Alex Hall +Date: Sun, 17 Sep 2023 19:08:32 +0200 +Subject: [PATCH 2/4] Keep the original co_firstlineno + +--- + pytest_examples/traceback.py | 10 ++++------ + tests/test_run_examples.py | 2 +- + 2 files changed, 5 insertions(+), 7 deletions(-) + +diff --git a/pytest_examples/traceback.py b/pytest_examples/traceback.py +index 591f995..9e797ed 100644 +--- a/pytest_examples/traceback.py ++++ b/pytest_examples/traceback.py +@@ -36,12 +36,10 @@ def create_example_traceback(exc: Exception, module_path: str, example: CodeExam + + def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + """ +- Create a new frame that mostly matches `frame` but with a filename from `example` and line number +- altered to match the example. ++ Create a new frame that mostly matches `frame` but with a filename from `example`. + + Taken mostly from https://naleraphael.github.io/blog/posts/devlog_create_a_builtin_frame_object/ +- With the CodeType creation inspired by https://stackoverflow.com/a/16123158/949890. However, we use +- `frame.f_lineno` for the line number instead of `f_code.co_firstlineno` as that seems to work. ++ With the CodeType creation inspired by https://stackoverflow.com/a/16123158/949890. + """ + import ctypes + +@@ -77,7 +75,7 @@ def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + str(example.path), + f_code.co_name, + f_code.co_qualname, +- frame.f_lineno + example.start_line, ++ f_code.co_firstlineno, + f_code.co_lnotab, + f_code.co_exceptiontable, + ) +@@ -95,7 +93,7 @@ def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + f_code.co_varnames, + str(example.path), + f_code.co_name, +- frame.f_lineno + example.start_line, ++ f_code.co_firstlineno, + f_code.co_lnotab, + ) + +diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py +index 9f077d7..9bf3dcc 100644 +--- a/tests/test_run_examples.py ++++ b/tests/test_run_examples.py +@@ -54,7 +54,7 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample): + + # assert 'my_file_9_13.py:12: AssertionError' in '\n'.join(result.outlines) + assert result.outlines[-8:-3] == [ +- '', ++ ' b = 2', + '> assert a + b == 4', + 'E AssertionError', + '', + +From 089cf8a4f0a91a5b85108205d0f58cf603af3ac5 Mon Sep 17 00:00:00 2001 +From: Alex Hall +Date: Wed, 20 Sep 2023 15:46:34 +0200 +Subject: [PATCH 3/4] Adjust co_firstlineno so that the pytest includes the + correct context. + +--- + pytest_examples/traceback.py | 8 +++++--- + tests/test_run_examples.py | 6 ++++-- + 2 files changed, 9 insertions(+), 5 deletions(-) + +diff --git a/pytest_examples/traceback.py b/pytest_examples/traceback.py +index 9e797ed..94ede45 100644 +--- a/pytest_examples/traceback.py ++++ b/pytest_examples/traceback.py +@@ -36,7 +36,9 @@ def create_example_traceback(exc: Exception, module_path: str, example: CodeExam + + def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + """ +- Create a new frame that mostly matches `frame` but with a filename from `example`. ++ Create a new frame that mostly matches `frame` but with a code object that has ++ a filename from `example` and adjusted an adjusted first line number ++ so that pytest shows the correct code context in the traceback. + + Taken mostly from https://naleraphael.github.io/blog/posts/devlog_create_a_builtin_frame_object/ + With the CodeType creation inspired by https://stackoverflow.com/a/16123158/949890. +@@ -75,7 +77,7 @@ def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + str(example.path), + f_code.co_name, + f_code.co_qualname, +- f_code.co_firstlineno, ++ f_code.co_firstlineno + example.start_line, + f_code.co_lnotab, + f_code.co_exceptiontable, + ) +@@ -93,7 +95,7 @@ def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + f_code.co_varnames, + str(example.path), + f_code.co_name, +- f_code.co_firstlineno, ++ f_code.co_firstlineno + example.start_line, + f_code.co_lnotab, + ) + +diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py +index 9bf3dcc..ad878ea 100644 +--- a/tests/test_run_examples.py ++++ b/tests/test_run_examples.py +@@ -52,8 +52,10 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample): + result = pytester.runpytest('-p', 'no:pretty', '-v') + result.assert_outcomes(passed=1, failed=1) + +- # assert 'my_file_9_13.py:12: AssertionError' in '\n'.join(result.outlines) +- assert result.outlines[-8:-3] == [ ++ assert result.outlines[-11:-3] == [ ++ '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ', ++ '', ++ ' a = 1', + ' b = 2', + '> assert a + b == 4', + 'E AssertionError', + +From e07ec3de414c00afa11a94bd136b33f77608c767 Mon Sep 17 00:00:00 2001 +From: Alex Hall +Date: Sat, 23 Sep 2023 12:08:07 +0200 +Subject: [PATCH 4/4] Loosen testing result.outlines + +--- + tests/test_run_examples.py | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py +index ad878ea..1dcfba1 100644 +--- a/tests/test_run_examples.py ++++ b/tests/test_run_examples.py +@@ -52,8 +52,8 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample): + result = pytester.runpytest('-p', 'no:pretty', '-v') + result.assert_outcomes(passed=1, failed=1) + +- assert result.outlines[-11:-3] == [ +- '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ', ++ assert result.outlines[-11].startswith('_ _ _ _ ') ++ assert result.outlines[-10:-3] == [ + '', + ' a = 1', + ' b = 2', diff --git a/pytest-examples-0.0.10.tar.gz b/pytest-examples-0.0.10.tar.gz new file mode 100644 index 0000000..8719b21 --- /dev/null +++ b/pytest-examples-0.0.10.tar.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dce3ca92ef6ac225387e9e38cac45cf81776f01a7b69fbacff4012b62984611f +size 22058 diff --git a/python-pytest-examples.changes b/python-pytest-examples.changes new file mode 100644 index 0000000..f318aed --- /dev/null +++ b/python-pytest-examples.changes @@ -0,0 +1,34 @@ +------------------------------------------------------------------- +Mon Jul 1 18:03:28 UTC 2024 - Dirk Müller + +- add ruff05.patch: fix exception with ruff 0.5.0 + +------------------------------------------------------------------- +Sat Mar 9 13:16:58 UTC 2024 - ecsos + +- Add %{?sle15_python_module_pythons} + +------------------------------------------------------------------- +Thu Mar 7 01:15:34 UTC 2024 - Steve Kowalik + +- Drop patch suppot-python-312.patch. +- Add patch support-python-312.patch, which is a rename of the previous + patch. +- Add patch fix-traceback.patch, support traceback changes of + Python 3.12. + +------------------------------------------------------------------- +Wed Jan 24 06:44:56 UTC 2024 - Steve Kowalik + +- Add patch suppot-python-312.patch: + * Support code changes in Python 3.12+. + +------------------------------------------------------------------- +Thu Oct 19 16:35:18 UTC 2023 - Ondřej Súkup + +- add test-ruff-0.1.0.patch to fix tests with ruvv-0.1.0 + +------------------------------------------------------------------- +Mon Oct 16 13:59:40 UTC 2023 - Ondřej Súkup + +- initial package diff --git a/python-pytest-examples.spec b/python-pytest-examples.spec new file mode 100644 index 0000000..66c6cd0 --- /dev/null +++ b/python-pytest-examples.spec @@ -0,0 +1,75 @@ +# +# spec file for package python-pytest-examples +# +# Copyright (c) 2024 SUSE LLC +# +# All modifications and additions to the file contributed by third parties +# remain the property of their copyright owners, unless otherwise agreed +# upon. The license for this file, and modifications and additions to the +# file, is the same license as for the pristine package itself (unless the +# license for the pristine package is not an Open Source License, in which +# case the license is the MIT License). An "Open Source License" is a +# license that conforms to the Open Source Definition (Version 1.9) +# published by the Open Source Initiative. + +# Please submit bugfixes or comments via https://bugs.opensuse.org/ +# + + +%{?sle15_python_module_pythons} +Name: python-pytest-examples +Version: 0.0.10 +Release: 0 +Summary: Pytest plugin for testing examples in docstrings and markdown files +License: MIT +URL: https://github.com/pydantic/pytest-examples +# sdist without tests +Source: https://github.com/pydantic/pytest-examples/archive/refs/tags/v%{version}.tar.gz#/pytest-examples-%{version}.tar.gz +# PATCH-FIX-UPSTREAM gh#pydantic/pytest-examples#17 +Patch0: fix-traceback.patch +# PATCH-FIX-UPSTREAM test-ruff-0.1.0.patch - fix tests for ruff-0.1.0 +Patch1: test-ruff-0.1.0.patch +# PATCH-FIX-UPSTREAM gh#pydantic/pytest-examples#22 +Patch2: support-python-312.patch +Patch3: ruff05.patch +BuildRequires: %{python_module black} +BuildRequires: %{python_module hatchling} +BuildRequires: %{python_module pip} +BuildRequires: %{python_module pytest} +BuildRequires: %{python_module ruff} +BuildRequires: fdupes +BuildRequires: python-rpm-macros +Requires: python-black +Requires: python-pytest +Requires: python-ruff +BuildArch: noarch +%python_subpackages + +%description +Pytest plugin for testing Python code examples in docstrings and markdown files. + +`pytest-examples` can: +* lint code examples using `ruff` and `black` +* run code examples +* run code examples and check print statements are inlined correctly in the code + +It can also update code examples in place to format them and insert or update print statements. + +%prep +%autosetup -p1 -n pytest-examples-%{version} + +%build +%pyproject_wheel + +%install +%pyproject_install +%python_expand %fdupes %{buildroot}%{$python_sitelib} + +%check +%pytest + +%files %{python_files} +%{python_sitelib}/pytest_examples +%{python_sitelib}/pytest_examples-%{version}.dist-info + +%changelog diff --git a/ruff05.patch b/ruff05.patch new file mode 100644 index 0000000..72c5ba5 --- /dev/null +++ b/ruff05.patch @@ -0,0 +1,13 @@ +Index: pytest-examples-0.0.10/pytest_examples/lint.py +=================================================================== +--- pytest-examples-0.0.10.orig/pytest_examples/lint.py ++++ pytest-examples-0.0.10/pytest_examples/lint.py +@@ -47,7 +47,7 @@ def ruff_check( + *, + extra_ruff_args: tuple[str, ...] = (), + ) -> str: +- args = 'ruff', '-', *config.ruff_config(), *extra_ruff_args ++ args = 'ruff', 'check', '-', *config.ruff_config(), *extra_ruff_args + + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True) + stdout, stderr = p.communicate(example.source, timeout=2) diff --git a/support-python-312.patch b/support-python-312.patch new file mode 100644 index 0000000..9df43fe --- /dev/null +++ b/support-python-312.patch @@ -0,0 +1,68 @@ +From 35f93d668c8a4a337d1c9d477fa5732fa439b1b3 Mon Sep 17 00:00:00 2001 +From: Alex Hall +Date: Wed, 27 Sep 2023 17:47:51 +0200 +Subject: [PATCH 1/2] Fix deprecation warnings in Python 3.12 + +--- + .github/workflows/ci.yml | 2 +- + pytest_examples/traceback.py | 21 ++++++++++++++++++++- + requirements/testing.txt | 6 ++---- + 3 files changed, 23 insertions(+), 6 deletions(-) + +diff --git a/pytest_examples/traceback.py b/pytest_examples/traceback.py +index 94ede45..41880fe 100644 +--- a/pytest_examples/traceback.py ++++ b/pytest_examples/traceback.py +@@ -62,7 +62,26 @@ def create_custom_frame(frame: FrameType, example: CodeExample) -> FrameType: + ctypes.pythonapi.PyThreadState_Get.restype = P_MEM_TYPE + + f_code = frame.f_code +- if sys.version_info >= (3, 11): ++ if sys.version_info >= (3, 12): ++ code = CodeType( ++ f_code.co_argcount, ++ f_code.co_posonlyargcount, ++ f_code.co_kwonlyargcount, ++ f_code.co_nlocals, ++ f_code.co_stacksize, ++ f_code.co_flags, ++ f_code.co_code, ++ f_code.co_consts, ++ f_code.co_names, ++ f_code.co_varnames, ++ str(example.path), ++ f_code.co_name, ++ f_code.co_qualname, ++ f_code.co_firstlineno + example.start_line, ++ f_code.co_linetable, ++ f_code.co_exceptiontable, ++ ) ++ elif sys.version_info >= (3, 11): + code = CodeType( + f_code.co_argcount, + f_code.co_posonlyargcount, +diff --git a/requirements/testing.txt b/requirements/testing.txt +index 805175d..4b42722 100644 +--- a/requirements/testing.txt ++++ b/requirements/testing.txt +@@ -2,10 +2,8 @@ + # This file is autogenerated by pip-compile with Python 3.10 + # by the following command: + # +-# pip-compile --output-file=requirements/testing.txt --resolver=backtracking requirements/testing.in ++# pip-compile --output-file=requirements/testing.txt requirements/testing.in + # +-attrs==22.2.0 +- # via pytest + coverage==7.2.2 + # via -r requirements/testing.in + exceptiongroup==1.1.1 +@@ -22,7 +20,7 @@ pluggy==1.0.0 + # via pytest + pygments==2.14.0 + # via rich +-pytest==7.2.2 ++pytest==7.4.2 + # via pytest-pretty + pytest-pretty==1.1.1 + # via -r requirements/testing.in diff --git a/test-ruff-0.1.0.patch b/test-ruff-0.1.0.patch new file mode 100644 index 0000000..cca5fae --- /dev/null +++ b/test-ruff-0.1.0.patch @@ -0,0 +1,22 @@ +From cade1306918e9984d0d45d5e274d01b734b5fd3a Mon Sep 17 00:00:00 2001 +From: Anton Zhukharev +Date: Wed, 18 Oct 2023 09:38:34 +0300 +Subject: [PATCH] update tests for ruff>=0.1.0 + +--- + tests/test_run_examples.py | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py +index 1dcfba1..4330d71 100644 +--- a/tests/test_run_examples.py ++++ b/tests/test_run_examples.py +@@ -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 +