Fix comment typehints for methods/classmethods (#300)

* Fix comment typehints for methods and classmethods

Co-authored-by: Mathieu <923463-mathbou@users.noreply.gitlab.com>
pull/348/head
Mathieu Bouzard 2 years ago committed by GitHub
parent 517a6be383
commit fc6b8aec77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -479,7 +479,11 @@ def get_args_info(args_node): # pylint: disable=too-many-branches,too-many-stat
]
plain_annotations = args_node.annotations or ()
func_comment_annotations = args_node.parent.type_comment_args or ()
func_comment_annotations = args_node.parent.type_comment_args or []
if args_node.parent.type in ("method", "classmethod"):
func_comment_annotations = [None] + func_comment_annotations
comment_annotations = args_node.type_comment_posonlyargs
comment_annotations += args_node.type_comment_args or []
comment_annotations += args_node.type_comment_kwonlyargs
@ -559,6 +563,9 @@ def get_args_info(args_node): # pylint: disable=too-many-branches,too-many-stat
annotation_offset += 1
result.append(("**", args_node.kwarg, annotation, None))
if args_node.parent.type in ("method", "classmethod") and result:
result.pop(0)
return result

@ -44,3 +44,25 @@ global_a = A() # type: A
def f3(first_arg, **kwargs):
# type: (first_arg, Any) -> None
"""Annotation incorrectly leaves out `**`."""
class B:
"""Annotation keeps self/cls and shift all arg types"""
def __init__(self, a):
# type: (str) -> None
pass
def method(self, b):
# type: (list) -> None
pass
@classmethod
def class_method(cls, c):
# type: (int) -> None
pass
@staticmethod
def static_method(d):
# type: (float) -> Union[str,None]
pass

@ -1,5 +1,5 @@
class A:
def test(a: int) -> bool:
def test(self, a: int) -> bool:
"""Test.
Args:
a: Argument

@ -1,5 +1,5 @@
class B:
def test(a: int) -> bool:
def test(self, a: int) -> bool:
"""Test.
Args:
a: Argument

@ -81,9 +81,9 @@ class TestSimpleModule:
assert "class Meta" in example_file
assert "attr2" in example_file
assert "This is the docstring of an instance attribute." in example_file
assert "method_okay(self, foo=None, bar=None)" in example_file
assert "method_multiline(self, foo=None, bar=None, baz=None)" in example_file
assert "method_tricky(self, foo=None, bar=dict(foo=1, bar=2))" in example_file
assert "method_okay(foo=None, bar=None)" in example_file
assert "method_multiline(foo=None, bar=None, baz=None)" in example_file
assert "method_tricky(foo=None, bar=dict(foo=1, bar=2))" in example_file
# Are constructor arguments from the class docstring parsed?
assert "Set an attribute" in example_file
@ -179,9 +179,9 @@ class TestSimpleStubModule:
assert "class Meta" in example_file
assert "Another class var docstring" in example_file
assert "A class var without a value." in example_file
assert "method_okay(self, foo=None, bar=None)" in example_file
assert "method_multiline(self, foo=None, bar=None, baz=None)" in example_file
assert "method_without_docstring(self)" in example_file
assert "method_okay(foo=None, bar=None)" in example_file
assert "method_multiline(foo=None, bar=None, baz=None)" in example_file
assert "method_without_docstring()" in example_file
# Are constructor arguments from the class docstring parsed?
assert "Set an attribute" in example_file
@ -248,7 +248,7 @@ class TestPy3Module:
assert "global_a :A" in example_file
assert "my_method(self) -> str" in example_file
assert "my_method() -> str" in example_file
assert "class example.SomeMetaclass" in example_file
@ -262,14 +262,14 @@ class TestPy3Module:
assert "overloaded_func(a: Union" not in example_file
assert "Overloaded function" in example_file
assert "overloaded_method(self, a: float" in example_file
assert "overloaded_method(self, a: str" in example_file
assert "overloaded_method(self, a: Union" not in example_file
assert "overloaded_method(a: float" in example_file
assert "overloaded_method(a: str" in example_file
assert "overloaded_method(a: Union" not in example_file
assert "Overloaded method" in example_file
assert "overloaded_class_method(cls, a: float" in example_file
assert "overloaded_class_method(cls, a: str" in example_file
assert "overloaded_class_method(cls, a: Union" not in example_file
assert "overloaded_class_method(a: float" in example_file
assert "overloaded_class_method(a: str" in example_file
assert "overloaded_class_method(a: Union" not in example_file
assert "Overloaded method" in example_file
assert "undoc_overloaded_func" in example_file
@ -337,6 +337,11 @@ class TestAnnotationCommentsModule:
assert "global_a :A" in example_file
assert "class example.B(a: str)" in example_file
assert "method(b: list)" in example_file
assert "classmethod class_method(c: int)" in example_file
assert "static static_method(d: float)" in example_file
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Positional only arguments need Python >=3.8"
@ -405,7 +410,7 @@ class TestSimplePackage:
example_foo_file = example_foo_handle.read()
assert "class example.foo.Foo" in example_foo_file
assert "method_okay(self, foo=None, bar=None)" in example_foo_file
assert "method_okay(foo=None, bar=None)" in example_foo_file
index_path = "_build/text/index.txt"
with io.open(index_path, encoding="utf8") as index_handle:

Loading…
Cancel
Save