Skip to content

Indexer fails to create symbols for assignments with type annotations #161

Open
@ofeki-neosec

Description

@ofeki-neosec

When running the indexer against lines with type annotations, like:

a: int = 1

The end result looks like this:

documents {
  relative_path: "a.py"
  occurrences {
    range: 0
    range: 0
    range: 0
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    symbol_roles: 1
  }
  occurrences {
    range: 0
    range: 0
    range: 1
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/a.a."
    symbol_roles: 1
  }
  occurrences {
    range: 0
    range: 3
    range: 6
    symbol: "scip-python python python-stdlib 3.11 builtins/int#"
    symbol_roles: 8
  }
  symbols {
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    documentation: "(module) example_projects.poc.a"
  }
}
external_symbols {
  symbol: "scip-python python python-stdlib 3.11 builtins/int#"
  documentation: "```python\n(class) int\n```"
}

Compared to when the code is:

a = 1

and the index is:

metadata {
  tool_info {
    name: "scip-python"
    version: "0.6.0"
  }
  project_root: "file:///Users/oinger/Projects/source_code_analysis_llm/example_projects/poc"
  text_document_encoding: UTF8
}
documents {
  relative_path: "a.py"
  occurrences {
    range: 0
    range: 0
    range: 0
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    symbol_roles: 1
  }
  occurrences {
    range: 0
    range: 0
    range: 1
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/a."
    symbol_roles: 1
  }
  symbols {
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    documentation: "(module) example_projects.poc.a"
  }
  symbols {
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/a."
    documentation: "```python\nbuiltins.int\n```"
  }
}

Notice how in the first example the int was mentioned as an external_symbol and not included as a symbol in its own document.

Another example:

from fastapi import FastAPI

app: FastAPI = FastAPI()

Returns

documents {
  relative_path: "a.py"
  occurrences {
    range: 0
    range: 0
    range: 0
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    symbol_roles: 1
  }
  occurrences {
    range: 0
    range: 5
    range: 12
    symbol: "scip-python python fastapi 0.95.0 fastapi/__init__:"
    symbol_roles: 8
  }
  occurrences {
    range: 0
    range: 20
    range: 27
    symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
    symbol_roles: 8
  }
  occurrences {
    range: 2
    range: 0
    range: 3
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app.app."
    symbol_roles: 1
  }
  occurrences {
    range: 2
    range: 5
    range: 12
    symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
    symbol_roles: 8
  }
  occurrences {
    range: 2
    range: 15
    range: 22
    symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
    symbol_roles: 8
  }
  symbols {
    symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
    documentation: "(module) example_projects.poc.a"
  }
}

Which doesn't include the assignment symbol at all. If I remove the type annotation, I get the following result:

{'scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:': {'doc': relative_path: "a.py"
occurrences {
  range: 0
  range: 0
  range: 0
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
  symbol_roles: 1
}
occurrences {
  range: 0
  range: 5
  range: 12
  symbol: "scip-python python fastapi 0.95.0 fastapi/__init__:"
  symbol_roles: 8
}
occurrences {
  range: 0
  range: 20
  range: 27
  symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
  symbol_roles: 8
}
occurrences {
  range: 2
  range: 0
  range: 3
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app."
  symbol_roles: 1
}
occurrences {
  range: 2
  range: 6
  range: 13
  symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
  symbol_roles: 8
}
symbols {
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
  documentation: "(module) example_projects.poc.a"
}
symbols {
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app."
  documentation: "```python\nfastapi.applications.FastAPI\n```"
}
, 'documentation': ['(module) example_projects.poc.a'], 'definition': range: 0
range: 0
range: 0
symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
symbol_roles: 1
, 'occs': []}, 'scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app.': {'doc': relative_path: "a.py"
occurrences {
  range: 0
  range: 0
  range: 0
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
  symbol_roles: 1
}
occurrences {
  range: 0
  range: 5
  range: 12
  symbol: "scip-python python fastapi 0.95.0 fastapi/__init__:"
  symbol_roles: 8
}
occurrences {
  range: 0
  range: 20
  range: 27
  symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
  symbol_roles: 8
}
occurrences {
  range: 2
  range: 0
  range: 3
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app."
  symbol_roles: 1
}
occurrences {
  range: 2
  range: 6
  range: 13
  symbol: "scip-python python fastapi 0.95.0 `fastapi.applications`/FastAPI#"
  symbol_roles: 8
}
symbols {
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/__init__:"
  documentation: "(module) example_projects.poc.a"
}
symbols {
  symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app."
  documentation: "```python\nfastapi.applications.FastAPI\n```"
}
, 'documentation': ['```python\nfastapi.applications.FastAPI\n```'], 'definition': range: 2
range: 0
range: 3
symbol: "scip-python python . 413fd4425d5005faf43761c299ce32b3afa32876 `example_projects.poc.a`/app."
symbol_roles: 1
, 'occs': []}}

Which contains the proper symbol.

I believe this is due to a bug in visitAssignment inside treeVisitor.ts where if node.leftExpression is ParseNodeType.TypeAnnotation, the function should evaluate whether its valueExpression is ParseNodeType.Name and not the annotation node itself. Unfortunately I wasn't able to debug this locally but this is my best guess.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions