Skip to content

Fix self being None for a method of a temporary instance - #909

Merged
craigbarratt merged 1 commit into
masterfrom
fix-bound-method-temp-instance
Nov 21, 2026
Merged

craigbarratt merged 1 commit into
masterfrom
fix-bound-method-temp-instance

Conversation

@craigbarratt

Copy link
Copy Markdown
Member

What was broken

A method called on a temporary instance got self as None:

class Foo:
    def __init__(self):
        self.v = 4

    def value(self):
        return self.v

Foo().value()        # AttributeError: 'NoneType' object has no attribute 'v'
method = Foo().value
method()             # same error

Storing the instance first (foo = Foo(); foo.value()) worked, which is why this went unnoticed. I found it while reviewing #907.

Why

EvalFuncVar.__get__ binds a method to its instance by creating an EvalFuncVarClassInst, and that held only a weakref to the instance. In Foo().value(), nothing else refers to the temporary instance once the method has been looked up. So the instance is freed before the call, and the weak reference returns None.

The weak reference goes back to the design before #790. Back then, call_func bound every method when the instance was created and stored the bound copies on the instance with setattr. A strong reference from those copies back to the instance would have created a reference cycle, so instances would only be freed by the cyclic garbage collector and not as soon as the last reference went away. #790 switched to the descriptor protocol: a new bound method is made on each attribute access, and it isn't stored on the instance. That removed the cycle, but the weak reference stayed.

What changed

EvalFuncVarClassInst now holds a strong reference to the instance, as Python's own bound methods do. Nothing else used the weak reference, so the weakref import is gone. Storing a bound method on its own instance (self.cb = self.method) now creates a cycle, exactly as it does in Python.

Testing

Two new cases in evalTests, with expected values checked against CPython:

  • Temp().name(), Temp().value(), and a bound method of a temporary instance called later. Without the fix, this fails with 'NoneType' object has no attribute 'v'.
  • __del__ of a temporary instance runs right after Temp().method(), and a stored bound method keeps its instance alive until it's deleted. This guards against the strong reference keeping instances alive too long.

The existing del c test also checks that __del__ runs promptly. As a mutation check, I made __get__ cache bound methods on the instance, which creates a cycle, and that test failed. So prompt deletion remains covered.

The full suite (206 tests), ruff, pylint and mypy pass.

🤖 Generated with Claude Code

A bound method held only a weak reference to its instance, so a method
called on a temporary instance, such as Foo().method(), got self as
None once the instance was freed before the call.

The weak reference dates from when every method was bound and stored on
the instance at construction, which would have made a reference cycle.
Since #790, a new bound method is made on each attribute access and
isn't stored on the instance, so hold a strong reference, as Python's
bound methods do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@craigbarratt

Copy link
Copy Markdown
Member Author

@dmamelin, could you take a look at this when you have a chance? It changes the weak instance reference that #790 carried over into the descriptor binding. I think it's safe since #790, because bound methods aren't stored on the instance anymore, but you know that code best.

@dmamelin

Copy link
Copy Markdown
Contributor

Looks good to me. Both new tests fail without the change and pass with it; full suite, ruff, pylint and mypy are clean.

I'm working on a rework of class handling and arrived at the same fix there.

@craigbarratt
craigbarratt merged commit c20990b into master Nov 21, 2026
11 checks passed
@craigbarratt

Copy link
Copy Markdown
Member Author

Thanks @dmamelin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants