Merge pull request #11143 from tushar-deepsource/patch-1

(cherry picked from commit 084d756ae6)

[ran: adapted to 7.4.x, fixed changelog issue number]
This commit is contained in:
Zac Hatfield-Dodds 2023-07-04 10:47:13 -07:00 committed by Ran Benita
parent 2cdd619bf4
commit a0f58fa9e7
4 changed files with 26 additions and 4 deletions

View File

@ -376,6 +376,7 @@ Tomer Keren
Tony Narlock Tony Narlock
Tor Colvin Tor Colvin
Trevor Bekolay Trevor Bekolay
Tushar Sadhwani
Tyler Goodlet Tyler Goodlet
Tyler Smart Tyler Smart
Tzu-ping Chung Tzu-ping Chung

View File

@ -0,0 +1 @@
Fix non-string constants at the top of file being detected as docstrings on Python>=3.8.

View File

@ -604,6 +604,13 @@ def _get_assertion_exprs(src: bytes) -> Dict[int, str]:
return ret return ret
def _get_ast_constant_value(value: astStr) -> object:
if sys.version_info >= (3, 8):
return value.value
else:
return value.s
class AssertionRewriter(ast.NodeVisitor): class AssertionRewriter(ast.NodeVisitor):
"""Assertion rewriting implementation. """Assertion rewriting implementation.
@ -700,11 +707,10 @@ class AssertionRewriter(ast.NodeVisitor):
expect_docstring expect_docstring
and isinstance(item, ast.Expr) and isinstance(item, ast.Expr)
and isinstance(item.value, astStr) and isinstance(item.value, astStr)
and isinstance(_get_ast_constant_value(item.value), str)
): ):
if sys.version_info >= (3, 8): doc = _get_ast_constant_value(item.value)
doc = item.value.value assert isinstance(doc, str)
else:
doc = item.value.s
if self.is_rewrite_disabled(doc): if self.is_rewrite_disabled(doc):
return return
expect_docstring = False expect_docstring = False

View File

@ -2077,3 +2077,17 @@ class TestReprSizeVerbosity:
self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE * 10) self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE * 10)
result = pytester.runpytest("-vv") result = pytester.runpytest("-vv")
result.stdout.no_fnmatch_line("*xxx...xxx*") 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