Skip to content

Commit

Permalink
Decorator fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Apr 1, 2022
1 parent 3a25d9c commit 7450dc5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ def _parse_overloads(self) -> List[FunctionNode]:
if not func.decorators:
continue
for node in func.decorators.nodes:
if node.name == "overload":
overload_node = FunctionNode(self.namespace, self, node=func)
overload_nodes.append(overload_node)
try:
if node.name == "overload":
overload_node = FunctionNode(self.namespace, self, node=func)
overload_nodes.append(overload_node)
except AttributeError:
continue
return overload_nodes

def _inspect(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ def _inspect(self):
self.namespace_id += ":async"

# Turn any decorators into annotation
try:
self.annotations = [f"@{x.name}" for x in self.node.decorators.nodes if hasattr(x, "name")]
except:
pass
if self.node.decorators:
self.annotations = [f"@{x.as_string()}" for x in self.node.decorators.nodes]

self.is_class_method = "@classmethod" in self.annotations
self._parse_function()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ def test_api_view_diagnostic_warnings(self):
stub_gen = StubGenerator(args=args)
apiview = stub_gen.generate_tokens()
# ensure we have only the expected diagnostics when testing apistubgentest
assert len(apiview.diagnostics) == 1
# TODO: These will be removed soon.
assert len(apiview.diagnostics) == 21
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,16 @@ def test_overloads(self):

def test_decorators(self):
class_node = ClassNode(name="SomethingWithDecorators", namespace="test", parent_node=None, obj=SomethingWithDecorators, pkg_root_namespace=self.pkg_namespace)
assert len(class_node.child_nodes) == 2
assert len(class_node.child_nodes) == 4

node1 = class_node.child_nodes[0]
assert node1.annotations == ["@my_decorator"]
assert node1.annotations == ["@another_decorator('Test')"]

node2 = class_node.child_nodes[1]
assert node2.annotations == ["@my_decorator"]
assert node2.annotations == ["@another_decorator('Test')"]

node3 = class_node.child_nodes[2]
assert node3.annotations == ["@my_decorator"]

node4 = class_node.child_nodes[3]
assert node4.annotations == ["@my_decorator"]
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ def wrapper(*args, **kwargs):
return wrapper


def another_decorator(value):
def decorator(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
return value
return wrapper
return decorator


class PublicCaseInsensitiveEnumMeta(EnumMeta):
def __getitem__(self, name: str):
pass
Expand Down Expand Up @@ -196,8 +205,16 @@ class SomethingWithDecorators:

@my_decorator
async def name_async(self):
return "Test"
pass

@my_decorator
def name_sync(self):
return "Test"
pass

@another_decorator("Test")
async def complex_decorator_async(self):
pass

@another_decorator("Test")
def complex_decorator_sync(self):
pass

0 comments on commit 7450dc5

Please sign in to comment.