From 997288c55253e27f782d991099490f80205d65bf Mon Sep 17 00:00:00 2001 From: Jakob Zahn Date: Thu, 3 Apr 2025 09:35:41 +0200 Subject: fix(python): highlight all types of docstrings (#7788) --- tests/query/highlights/python/docstrings.py | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/query/highlights/python/docstrings.py (limited to 'tests/query/highlights/python') diff --git a/tests/query/highlights/python/docstrings.py b/tests/query/highlights/python/docstrings.py new file mode 100644 index 000000000..02c0a2cc6 --- /dev/null +++ b/tests/query/highlights/python/docstrings.py @@ -0,0 +1,121 @@ +# Docstrings according to PEP 257 (https://peps.python.org/pep-0257/) +# <- @comment + +"""Module docstring assigned to `__doc__`...""" +# <- @string.documentation +""" +... with an addtional docstring, not part of `__doc__`. +""" +# <- @string.documentation + +""" +Some random docstring in the middle if nowhere... +""" +# <- @string.documentation +""" +... also with not one ... +""" +# <- @string.documentation +""" +... but two addtional docstrings. +""" +# <- @string.documentation + +oneline_string_assignment = "not detected as docstring" +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation +"""Module attribute docstring.""" +# <- @string.documentation + +multiline_string_assignment = """ + also not detected as docstring + """ +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation + +looks_like_implicit_string_concatenation = "abc" +# ^^^^^ @string +"def" +# <- @string.documentation + +single_line_implicit_string_concatenation = "abc" "def" +# ^^^^^ @string +# ^^^^^ @string +# ^^^^^ !@string.documentation + +multiline_implicit_string_concatenation = ( + "not " + # <- @string + # <- !@string.documentation + "detected " + # <- @string + # <- !@string.documentation + "as docstring, " + # <- @string + # <- !@string.documentation + "either." + # <- @string + # <- !@string.documentation +) + + +class A: + """ + Class docstring, assigned to `__doc__`. + """ + # <- @string.documentation + + """ + Some random docstring again, ... + """ + # <- @string.documentation + """ + ... with an "additional" docstring. Again. + """ + # <- @string.documentation + + foo = "class attribute" + # ^^^^^^^^^^^^^^^^^ @string + # ^^^^^^^^^^^^^^^^^ !@string.documentation + """ + Class attribute docstring, but an attribute + does not have a `__doc__` attribute itself. + """ + # <- @string.documentation + + bar: int + """Class attribute docstring, type annotation only.""" + # <- @string.documentation + + baz: str = "type annotated class attribute" + # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string + # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation + """Class attribute docstring, type annotation and assignment.""" + # <- @string.documentation + + def __init__(self): + """Method docstring.""" + # <- @string.documentation + + self.quux = "instance attribute" + # ^^^^^^^^^^^^^^^^^^^^ @string + # ^^^^^^^^^^^^^^^^^^^^ !@string.documentation + """Instance attribute docstring.""" + # <- @string.documentation + + +def f(x): + """Function docstring.""" + # <- @string.documentation + """Addtional function docstring.""" + # <- @string.documentation + return x**2 + + +f.a = 1 +"""Function attribute docstring.""" +# <- @string.documentation + + +"Random docstring with single quotes - legal, but far off standard and confusing." +# <- @string.documentation -- cgit v1.2.3-70-g09d2