From 3a5fec6e402950c04601f987561387b10b39e1e6 Mon Sep 17 00:00:00 2001 From: babyvitbot Date: Wed, 18 Nov 2026 07:27:17 +0200 Subject: [PATCH 1/4] README: drop the invitation for issues and pull requests The note that PyCG is maintained again keeps its other sentences. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a249dd0..4cbc64c 100644 --- a/README.md +++ b/README.md @@ -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 From 0fa828906a76da53c93d551d457d5aa6b86aee8a Mon Sep 17 00:00:00 2001 From: babyvitbot Date: Wed, 18 Nov 2026 07:32:43 +0200 Subject: [PATCH 2/4] Design: two stated limits, each pinned by a test The evaluation labelled every false negative on its 30 packages with a cause. Two causes were mechanisms that the design did not state. The design now states them as limits, and a test pins what the analysis reports. The analysis does not change. A getter that raises AttributeError (Lookup through an instance). Python calls the class's __getattr__ when a property's getter or a descriptor's __get__ raises AttributeError. The analysis calls __getattr__ only where no class of the MRO defines the name. Measured on the macro-benchmark of pycg-evaluation (30 packages, PyCG c5c6a89): 32 missed edges have this cause (bottle 19, structlog 8, anytree 4, asyncstdlib 1). A rule "a load of a name bound to a property or descriptor also calls the class's __getattr__", measured on a copy of the analysis, 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. Test: test_limit_a_getter_that_raises_attribute_error_does_not_call_getattr. An assert narrows as if it always runs (Class tests). Under python -O an assert does not run, so the code after assert isinstance(x, C) also runs with values that are not instances of C. The analysis follows the default interpreter. Measured: 4 missed edges of h11 (normalize_and_validate and get_comma_header calling str.lower and str.split), in a ground truth that also counts runs under -O. Test: test_limit_an_assert_narrows_as_if_it_always_runs. Both programs were run under Python 3.15.2 (the assert one also with -O) to confirm what Python does, and under PyCG to confirm what the analysis reports. Co-Authored-By: Claude Opus 5.5 --- docs/design.md | 19 +++++++++++++++ tests/test_class_tests.py | 27 +++++++++++++++++++++ tests/test_decorators_and_descriptors.py | 30 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/docs/design.md b/docs/design.md index 8aeb96d..c67b5f3 100644 --- a/docs/design.md +++ b/docs/design.md @@ -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 @@ -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 diff --git a/tests/test_class_tests.py b/tests/test_class_tests.py index 3b7ff15..7bd0241 100644 --- a/tests/test_class_tests.py +++ b/tests/test_class_tests.py @@ -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") == [ + ".bytes.lower", + ".bytes.split", + ".isinstance", + ".str.strip", + ] + + def test_nested_functions_read_the_variable_and_comprehensions_the_branch(tmp_path): graph = graph_of( tmp_path, diff --git a/tests/test_decorators_and_descriptors.py b/tests/test_decorators_and_descriptors.py index 233882a..c062e27 100644 --- a/tests/test_decorators_and_descriptors.py +++ b/tests/test_decorators_and_descriptors.py @@ -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 From 4927249f5afbb3e38494b94f58313cb2f53bf8fc Mon Sep 17 00:00:00 2001 From: babyvitbot Date: Wed, 18 Nov 2026 07:37:30 +0200 Subject: [PATCH 3/4] AGENTS.md: features go to main by pull request There is no `dev` branch in the published repository: `main` is the branch, and a change reaches it through a pull request. Co-Authored-By: Claude Fable 5.2 --- AGENTS.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6fbfacb..ad20f89 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. From 23b3756af5ab0aefce04e2c8dd119c81bc721602 Mon Sep 17 00:00:00 2001 From: babyvitbot Date: Wed, 18 Nov 2026 07:38:40 +0200 Subject: [PATCH 4/4] Version 2.0.0 The redesigned analysis is PyCG 2. The version was the placeholder 0.1.0.dev0; the releases of the 2021 analysis on PyPI are 0.0.x. The output document records this version in its `generator` field. Co-Authored-By: Claude Fable 5.2 --- pycg/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycg/__init__.py b/pycg/__init__.py index 2ca79de..14fafe5 100644 --- a/pycg/__init__.py +++ b/pycg/__init__.py @@ -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"