From 9859c110cc9851a25802524f09fc58ce2aa018db Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> Date: Mon, 26 Jun 2023 12:14:41 +0530 Subject: [PATCH 1/6] Ensure the docstring is a string --- src/_pytest/assertion/rewrite.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index ab83fee32b2..9ac4d582f2c 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -687,6 +687,7 @@ def run(self, mod: ast.Module) -> None: expect_docstring and isinstance(item, ast.Expr) and isinstance(item.value, astStr) + and isinstance(item.value.value, str) ): if sys.version_info >= (3, 8): doc = item.value.value From 4ebb2b94c289a5d1a5756a4a69c7f2ec3e0def89 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> Date: Mon, 26 Jun 2023 12:19:42 +0530 Subject: [PATCH 2/6] fix code for py37 --- src/_pytest/assertion/rewrite.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 9ac4d582f2c..5d3dfcc3162 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -687,12 +687,13 @@ def run(self, mod: ast.Module) -> None: expect_docstring and isinstance(item, ast.Expr) and isinstance(item.value, astStr) - and isinstance(item.value.value, str) ): if sys.version_info >= (3, 8): doc = item.value.value else: doc = item.value.s + if not isinstance(doc, str): + return if self.is_rewrite_disabled(doc): return expect_docstring = False From 05d7e609040f0af87b2d647ef10b9deb7dd89d3c Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 26 Jun 2023 12:37:23 +0530 Subject: [PATCH 3/6] Add test --- testing/test_assertrewrite.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 778f843e6cf..9b4ca87b2e6 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -2055,3 +2055,14 @@ def test_max_increased_verbosity(self, pytester: Pytester) -> None: self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE * 10) result = pytester.runpytest("-vv") result.stdout.no_fnmatch_line("*xxx...xxx*") + + +class TestIssue11140: + def test_constant_not_picked_as_module_docstring(self, pytester: Pytester) -> None: + pytester.makepyfile( + """\ + 0 + """ + ) + result = pytester.runpytest() + assert result.ret == 0 From a14fc10cac90dcb075221d03c52c76dd714daab1 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 26 Jun 2023 13:06:11 +0530 Subject: [PATCH 4/6] add a test function to the test --- testing/test_assertrewrite.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 9b4ca87b2e6..5833a04838d 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -2062,6 +2062,9 @@ def test_constant_not_picked_as_module_docstring(self, pytester: Pytester) -> No pytester.makepyfile( """\ 0 + + def test_foo(): + pass """ ) result = pytester.runpytest() From 5332656906dc60b29b4288eaf1842452106fce46 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> Date: Mon, 26 Jun 2023 17:02:33 +0530 Subject: [PATCH 5/6] Add changelog and author --- AUTHORS | 1 + changelog/11146.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/11146.bugfix.rst diff --git a/AUTHORS b/AUTHORS index ee4ef203f0c..2acbdb98c39 100644 --- a/AUTHORS +++ b/AUTHORS @@ -370,6 +370,7 @@ Tomer Keren Tony Narlock Tor Colvin Trevor Bekolay +Tushar Sadhwani Tyler Goodlet Tzu-ping Chung Vasily Kuznetsov diff --git a/changelog/11146.bugfix.rst b/changelog/11146.bugfix.rst new file mode 100644 index 00000000000..b1794ad5723 --- /dev/null +++ b/changelog/11146.bugfix.rst @@ -0,0 +1 @@ +- Prevent constants at the top of file from being detected as docstrings. \ No newline at end of file From 4719d998d809a99937fc571c7afba6f8291a97d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Jun 2023 11:34:02 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/11146.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/11146.bugfix.rst b/changelog/11146.bugfix.rst index b1794ad5723..03b468f3018 100644 --- a/changelog/11146.bugfix.rst +++ b/changelog/11146.bugfix.rst @@ -1 +1 @@ -- Prevent constants at the top of file from being detected as docstrings. \ No newline at end of file +- Prevent constants at the top of file from being detected as docstrings.