Skip to content

Commit

Permalink
Merge pull request #11143 from tushar-deepsource/patch-1
Browse files Browse the repository at this point in the history
(cherry picked from commit 084d756)

[ran: adapted to 7.4.x, fixed changelog issue number]
  • Loading branch information
Zac-HD authored and bluetech committed Dec 31, 2023
1 parent 2cdd619 commit a0f58fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ Tomer Keren
Tony Narlock
Tor Colvin
Trevor Bekolay
Tushar Sadhwani
Tyler Goodlet
Tyler Smart
Tzu-ping Chung
Expand Down
1 change: 1 addition & 0 deletions changelog/11140.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix non-string constants at the top of file being detected as docstrings on Python>=3.8.
14 changes: 10 additions & 4 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,13 @@ def _write_and_reset() -> None:
return ret


def _get_ast_constant_value(value: astStr) -> object:
if sys.version_info >= (3, 8):
return value.value

Check warning on line 609 in src/_pytest/assertion/rewrite.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/assertion/rewrite.py#L609

Added line #L609 was not covered by tests
else:
return value.s


class AssertionRewriter(ast.NodeVisitor):
"""Assertion rewriting implementation.
Expand Down Expand Up @@ -700,11 +707,10 @@ def run(self, mod: ast.Module) -> None:
expect_docstring
and isinstance(item, ast.Expr)
and isinstance(item.value, astStr)
and isinstance(_get_ast_constant_value(item.value), str)
):
if sys.version_info >= (3, 8):
doc = item.value.value
else:
doc = item.value.s
doc = _get_ast_constant_value(item.value)
assert isinstance(doc, str)
if self.is_rewrite_disabled(doc):
return
expect_docstring = False
Expand Down
14 changes: 14 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,3 +2077,17 @@ 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
def test_foo():
pass
"""
)
result = pytester.runpytest()
assert result.ret == 0

0 comments on commit a0f58fa

Please sign in to comment.