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. 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 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/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" 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