Skip to content

Python: AST extractor misses ~53% of Python class inheritance edges (cross-file base classes) #2736

Description

@oskbjo

When a Python class inherits from a base class imported from another file, the AST extractor fails to emit an inherits edge. Instead it produces a weaker uses [INFERRED] edge pointing at the import line rather than the class definition.

Repro:

  # module_a.py
  class BaseDriver:
      ...
  # module_b.py
  from module_a import BaseDriver

  class ChildDriver(BaseDriver):  # <-- should produce inherits [EXTRACTED]
      ...

Actual: ChildDriver --uses [INFERRED]--> BaseDriver (at import line)
Expected: ChildDriver --inherits [EXTRACTED]--> BaseDriver (at class def line)

Same-file inheritance is handled correctly. The issue is that the extractor doesn't resolve imported names back to their class node when the base class comes from another module.

Measurement on a ~900-file Python codebase:

  • 820 inheritance relationships found via ast.ClassDef.bases
  • 389 (47%) correctly emitted as inherits edges (same-file or simple cases)
  • 431 (53%) missing, represented only as uses [INFERRED]

Impact:

  • graphify path between parent and child classes routes through unrelated intermediate nodes instead of showing the direct inheritance chain
  • Community detection separates parent/child classes that should cluster together
  • Driver/plugin hierarchies with deep cross-file inheritance chains are especially affected

Environment: graphify 0.9.42, Python 3.12, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions