joern icon indicating copy to clipboard operation
joern copied to clipboard

[Bug] [pysrc2cpg] Class inheritance does not recognize generic parent classes

Open wunused opened this issue 1 year ago • 0 comments

Describe the bug

For Python class TypeDecl nodes that inherit from generic classes, Joern does not recognize the inherited class when it is specialized. This is the case for both the inheritedTypeFullName and baseType fields.

To Reproduce

  1. Create analysis test:

generic.py

import typing

T = typing.TypeVar("T")

class GenericFoo(typing.Generic[T]):
    pass

class SomeMixin(object):
    pass

class IntFoo(GenericFoo[int], SomeMixin):
    pass
  1. Analyze with Joern
$ ./joern-parse generic.py -o generic.cpg
$ ./joern generic.cpg
joern> cpg.typeDecl("IntFoo").inheritsFromTypeFullName.l
val res1: List[String] = List(
  ":<module>.py:<module>.SomeMixin",
  "<body>/:<module>.py:<module>.SomeMixin",
  "<metaClassCallHandler>/:<module>.py:<module>.SomeMixin",
  "<fakeNew>/:<module>.py:<module>.SomeMixin"
)
joern> cpg.typeDecl("IntFoo").baseType.l
val res2: List[io.shiftleft.codepropertygraph.generated.nodes.Type] = List(
  Type(
    id = 86L,
    fullName = ":<module>.SomeMixin",
    name = "SomeMixin",
    typeDeclFullName = ":<module>.SomeMixin"
  )
)

For this example, GenericFoo is not recognized as an inherited class of IntFoo. In other examples, I have observed e.g., "GenericFoo[int]" listed in the inheritedTypeFullName field, but without recognizing that "GenericFoo[int]" is the same class as "GenericFoo".

Expected behavior

The inheritedTypeFullName and baseType fields list "GenericFoo" as an inherited class of IntFoo.

Desktop (please complete the following information):

  • OS: Debian 12
  • Joern Version: 2.0.385
  • Java version: 19.0.2

Additional context

I know I am on an outdated version of Joern, but on the most up-to-date build I cannot get a CPG to load when analyzing Python projects. I do not see any recent commit messages related to this, but I apologize if this is a recently fixed issue.

wunused avatar Aug 08 '24 02:08 wunused