Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ not add one without discussion.

## Workflow

`dev` is the integration branch. Do each feature on its own branch in a
worktree under `.worktrees/`, and merge it into `dev` with a descriptive
merge commit when tests and lint pass.
Do each feature on its own branch from `main`, in a worktree under
`.worktrees/`, and open a pull request when tests and lint pass.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

> **PyCG is maintained again.** The repository was archived in November
> 2023. Development restarted in 2026 with a new analysis for Python 3.12
> and later. Issues and pull requests are welcome.
> and later.

PyCG generates call graphs for Python code with a fully static analysis:
it never imports or runs the code that it analyses. The analysis follows
Expand Down
19 changes: 19 additions & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,14 @@ contains itself, or tuples that share their elements, end
resolve and give more edges: on hypothesis, 24,737 more, nearly all of
them outside names built past a function of an extension module, at 13
sites that call a drawn value.
- Limit: an assert narrows as if it always runs. The analysis follows the
default interpreter, where asserts run. Under `python -O` an assert does
not run, so the statements after `assert isinstance(x, C)` also run with
values of `x` that are not instances of `C`, and the analysis misses the
calls on them (`test_limit_an_assert_narrows_as_if_it_always_runs`). On
the 30 benchmark packages this misses 4 edges, all in h11, whose ground
truth also counts runs under `-O`: `normalize_and_validate` and
`get_comma_header` call `str.lower` and `str.split`.

### Code that cannot run

Expand Down Expand Up @@ -2121,6 +2129,17 @@ obj.attr = Field(obj, attr) the instance's own field
analysis gives those unbound.
- Limit: a `__getattr__` stored on a class after its body (`C.__getattr__
= fetch`, or through `setattr`) is not called, as it would be in Python.
- Limit: where a property's getter or a descriptor's `__get__` raises
`AttributeError`, Python then calls the class's `__getattr__` with the
name. The analysis calls `__getattr__` only where no class of the MRO
defines the name, so that call, and what it calls, are missing
(`test_limit_a_getter_that_raises_attribute_error_does_not_call_getattr`).
On the 30 benchmark packages, 32 missed edges have this cause (bottle
19, structlog 8, anytree 4, asyncstdlib 1). A rule that makes each load
of a name bound to a property or a descriptor also call the class's
`__getattr__` was measured: it adds 31 of them and 21 wrong edges
(anytree 19, bottle 2). That is more than one wrong edge for every ten
true ones, the bound for admitting a rule, so the rule is not built.

**Records on demand.** Whether a class has a name depends on the class
only; only what the class gives is bound to the instance. So a rule that
Expand Down
2 changes: 1 addition & 1 deletion pycg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""PyCG: practical Python call graphs, by a fully static analysis."""

# The version of PyCG, its one source: the build backend reads it here.
__version__ = "0.1.0.dev0"
__version__ = "2.0.0"
27 changes: 27 additions & 0 deletions tests/test_class_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,33 @@ def waits(x):
assert methods(graph, "main.block", "after") == []


def test_limit_an_assert_narrows_as_if_it_always_runs(tmp_path):
"""Python: `use` calls `str.strip`, and the assert then fails. Under
`python -O` the assert does not run, so `use` also calls `str.lower`
and `str.split` on the str. The analysis follows the default
interpreter: after the assert, `name` holds only bytes (see *Class
tests*, limits)."""
graph = graph_of(
tmp_path,
"""
def use(name):
name.strip()
assert isinstance(name, bytes)
name.lower()
name.split()
def caller():
use("Content-Length")
""",
)

assert callees(graph, "main.use") == [
"<builtin>.bytes.lower",
"<builtin>.bytes.split",
"<builtin>.isinstance",
"<builtin>.str.strip",
]


def test_nested_functions_read_the_variable_and_comprehensions_the_branch(tmp_path):
graph = graph_of(
tmp_path,
Expand Down
30 changes: 30 additions & 0 deletions tests/test_decorators_and_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,36 @@ class Sub(Base):
assert callees(graph, "main") == ["main.Base.__getattr__", "main.target"]


def test_limit_a_getter_that_raises_attribute_error_does_not_call_getattr(tmp_path):
"""Python: a property's getter or a descriptor's `__get__` that raises
AttributeError makes the load call the class's `__getattr__` with the
name, so `use` also calls `Node.__getattr__` and, through its result,
`fetch`. The analysis calls `__getattr__` only where no class of the
MRO defines the name (see *Lookup through an instance*, limits)."""
graph = main_graph(
tmp_path,
"""
def fetch(): pass
class Lazy:
def __get__(self, obj, owner=None):
raise AttributeError("value")
class Node:
value = Lazy()
@property
def parent(self):
raise AttributeError("parent")
def __getattr__(self, name):
return fetch
def use():
node = Node()
node.parent()
node.value()
""",
)

assert callees(graph, "main.use") == ["main.Lazy.__get__", "main.Node.parent"]


@pytest.mark.parametrize("name", sorted(ASSIGNED_HOOKS))
def test_a_hook_named_late_still_runs_for_earlier_accesses(tmp_path, name):
# The name is a constant f-string in a variable, so setattr stores the
Expand Down
Loading