gh-107559: Argument Clinic: complain about non-ASCII chars in param docstrings (#107560)

Previously, only function docstrings were checked for non-ASCII characters.
Also, improve the warn() message.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Erlend E. Aasland 2023-08-02 14:40:23 +02:00 committed by GitHub
parent 439466a02b
commit 9ff7b4af13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -1427,6 +1427,25 @@ Couldn't find existing function 'fooooooooooooooooooooooo'!
actual = stdout.getvalue()
self.assertEqual(actual, expected)
def test_non_ascii_character_in_docstring(self):
block = """
module test
test.fn
a: int
á param docstring
docstring bár baß
"""
with support.captured_stdout() as stdout:
self.parse(block)
# The line numbers are off; this is a known limitation.
expected = dedent("""\
Warning on line 0:
Non-ascii characters are not allowed in docstrings: 'á'
Warning on line 0:
Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß'
""")
self.assertEqual(stdout.getvalue(), expected)
class ClinicExternalTest(TestCase):
maxDiff = None

View file

@ -785,9 +785,6 @@ class CLanguage(Language):
self,
f: Function
) -> str:
if re.search(r'[^\x00-\x7F]', f.docstring):
warn("Non-ascii character appear in docstring.")
text, add, output = _text_accumulator()
# turn docstring into a properly quoted C string
for line in f.docstring.split('\n'):
@ -5266,6 +5263,11 @@ class DSLParser:
def docstring_append(self, obj: Function | Parameter, line: str) -> None:
"""Add a rstripped line to the current docstring."""
matches = re.finditer(r'[^\x00-\x7F]', line)
if offending := ", ".join([repr(m[0]) for m in matches]):
warn("Non-ascii characters are not allowed in docstrings:",
offending)
docstring = obj.docstring
if docstring:
docstring += "\n"