Expand description
Python for the kartta code graph: the functions, classes, fields, calls and imports of Python source.
Python implements Language over the tree-sitter-python grammar. It claims every file whose extension is
py or pyi, in any case.
§What a file extracts to
Every function, method and class is a symbol, and so is a variable assigned to a bare name at module level and an
annotated attribute in a class body, name: str = "", which is a field of its class. A symbol is qualified by the
module its path names and by the classes around it: pkg.service.Service.greet for a method of Service in
pkg/service.py, and pkg.Service for a class in pkg/__init__.py. A definition nested in a function is qualified
by the module alone, is no member of a class around that function, and is local to the function’s body. A decorated
definition starts at its first decorator. A name that starts with _ is not exported.
A call records its callee and, for a member call, its receiver: self.greet() records greet on self, and a
member call on anything but a name, items[0].close(), records the empty receiver. A call is attributed to the
function around it; a class opens no scope. A call before the first definition of the file is dropped.
§Resolution
An import or from ... import that names a module of the repository includes its file, a/b.py or
a/b/__init__.py, or failing both its stub, a/b.pyi or a/b/__init__.pyi: an absolute module is probed from the
repository root and then from each directory around the importing file, nearest first, and a relative one from the
importing file’s directory, one level up for each leading dot after the first. A from-import also includes each
imported name that is a submodule of the package, a namespace package without an __init__.py included, and probes
none below a plain module file. An import that includes no file, such as one of the standard library or a
from __future__ import, is recorded as written.
A file named test_*, *_test or conftest, or below a test or tests directory, is test code. A *_pb2 or
*_pb2_grpc file, or one with a @generated or DO NOT EDIT marker in the comments and module docstring that open
it, outside a backtick span, is generated code. A .pyi stub takes the module name and the role of the .py source
it describes. A free def main at module level is the entry point.
§Fingerprint
Python::fingerprint identifies this crate’s name and version, a digest of its source, and the grammar: its
version, ABI version, node types, and the counts of its node kinds, parse states and fields. A change to
kartta-lang is covered by kartta_lang::FINGERPRINT, which a cache keys beside it. It cannot see a grammar built
from changed sources that keep its version and tables, nor a change to the tree-sitter runtime that keeps its
version.
§Examples
use kartta_lang::Language;
use kartta_lang_python::Python;
let python = Python::new();
assert!(python.claims("pkg/service.py"));
let raw = python.extract(b"class Service:\n def greet(self):\n return helper()\n");
let names: Vec<&str> = raw.symbols.iter().map(|symbol| symbol.qualified_name.as_str()).collect();
assert_eq!(names, ["Service", "Service.greet"]);
assert_eq!(raw.calls[0].callee, "helper");Structs§
- Python
- The Python language.