pylint-dev / pylint-dev/astroid

Caching property values is unsafe with mutable ASTs

Open
#169 5 comments 0 reactions 0 assignees View on GitHub
Bug 🪳
Dominant language
Python
Stars
582
Forks
357
Avg merge
1d 1h
Merged PRs (30d)
23

Description

Originally reported by: **BitBucket: [ceridwenv](http://bitbucket.org/ceridwenv), GitHub: @ceridwen**

---

As an example, take _newstyle_impl in scoped_nodes.py. It caches its result in self._newstyle and checks that first before returning a value. If you then create a class 'B' that inherits from another class 'A', call anything that accesses B.newstyle, and change what class 'A' inherits from a new-style class to an old-style class or vice-versa, B.newstyle will return an incorrect value. The two obvious solutions here are not to cache property values or to make ASTs immutable.

The deeper issue is that there's no real separation between properties that are properties of individual nodes, for instance a class's name, and properties that are properties of entire ASTs, like a class being new-style or old-style. I don't know what to do about this in general.

---
- Bitbucket: https://bitbucket.org/logilab/astroid/issue/169

Contributor guide

No contributing guide indexed for this repository

Research direction

Read scoped_nodes.py, starting with _newstyle_impl and the _newstyle cache. Reproduce the example by changing class A's base after accessing B.newstyle, then trace which cached properties depend on the wider AST. Done requires an agreed design that avoids incorrect values after AST mutation, with the affected behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.