Skip to content

Commit

Permalink
feat: Use aliases when inspecting too
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 2, 2022
1 parent 2f7969c commit 60439ee
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/griffe/agents/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from __future__ import annotations

from inspect import Parameter as SignatureParameter
from inspect import Signature, getdoc
from inspect import Signature, getdoc, getmodule
from inspect import signature as getsignature
from pathlib import Path
from tokenize import TokenError
Expand All @@ -33,7 +33,17 @@
from griffe.agents.extensions import Extensions
from griffe.agents.nodes import ObjectKind, ObjectNode, get_annotation
from griffe.collections import LinesCollection
from griffe.dataclasses import Attribute, Class, Docstring, Function, Module, Parameter, ParameterKind, Parameters
from griffe.dataclasses import (
Alias,
Attribute,
Class,
Docstring,
Function,
Module,
Parameter,
ParameterKind,
Parameters,
)
from griffe.docstrings.parsers import Parser
from griffe.expressions import Expression, Name
from griffe.importer import dynamic_import
Expand Down Expand Up @@ -159,7 +169,25 @@ def generic_inspect(self, node: ObjectNode) -> None: # noqa: WPS231
"""
for before_inspector in self.extensions.before_children_inspection:
before_inspector.inspect(node)
super().generic_inspect(node)

for child in node.children:
try:
child_module = getmodule(child.obj).__name__ # type: ignore[union-attr]
except AttributeError:
child_module = None

# special case: ast imports from _ast, while getmodule returns ast for objects in _ast -> cyclic
exclude = node.name == "_ast" and child_module == "ast"

if child_module and child_module != self.current.module.path and not exclude:
try:
child_name = child.obj.__name__
except AttributeError:
child_name = child.name
self.current[child.name] = Alias(child.name, f"{child_module}.{child_name}")
else:
self.inspect(child)

for after_inspector in self.extensions.after_children_inspection:
after_inspector.inspect(node)

Expand Down

0 comments on commit 60439ee

Please sign in to comment.