Skip to content

Add EvalFuncVar binding via descriptor and drop manual rebinding - #790

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
dmamelin:evalfuncvar-descriptor-binding
Dec 13, 2025
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
dmamelin:evalfuncvar-descriptor-binding

Conversation

@dmamelin

Copy link
Copy Markdown
Contributor

Looking into #787 I found that Enum overrides __dir__, so dir(inst) inside call_func doesn’t show the method.
I went deep into MRO, inspect, etc., but the actual fix turned out to be much simpler: descriptors solve this problem.
CPython implementation

A quick search shows that __dir__ is rarely overridden, but this PR may also affect other class-related issues in Pyscript - I just don’t know which ones yet :)

The logic for creating EvalFuncVarClassInst has also changed. Previously, an EvalFuncVarClassInst was created for every available EvalFuncVar method when the instance was constructed.
Now, no EvalFuncVarClassInst objects are created during instance construction; instead, a new EvalFuncVarClassInst is created on each access to an EvalFuncVar.

class Test:
    def a(self):
        pass

    def b(self):
        pass

def old():
    test = Test()  # AstEval.call_func: create EvalFuncVarClassInst for a() and b()
    test.a()       # use existing EvalFuncVarClassInst
    test.a()       # use existing EvalFuncVarClassInst

def new():
    test = Test()  # AstEval.call_func: _NO_ EvalFuncVarClassInst created
    test.a()       # create EvalFuncVarClassInst and call it
    test.a()       # create a new EvalFuncVarClassInst and call it
    b = test.a     # create EvalFuncVarClassInst
    b()            # call the existing EvalFuncVarClassInst

@dmamelin

Copy link
Copy Markdown
Contributor Author

The test failure is not related to the modified code.

@craigbarratt

craigbarratt commented Dec 11, 2025 •

Copy link
Copy Markdown
Member

Hmmm, that's a weird error. I notice it's running the tests using python 3.13.11. My last commit 2 days ran tests with python 3.13.9. Is it some issue with python 3.13.11's modules?

pytest is loading pycares 5.0.0 which was released... today. Yikes - not great to be on the bleeding edge for running tests...

@dmamelin

dmamelin commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor Author

aio-libs/aiodns#214
Waiting for Home Assistant 2025.12.3 and pytest-homeassistant-custom-component==0.13.301.

@craigbarratt
craigbarratt merged commit 7008ef3 into custom-components:master Dec 13, 2025
@craigbarratt

craigbarratt commented Dec 13, 2025 •

Copy link
Copy Markdown
Member

Thanks for the PR. I forced pycares < 5.0.0 in the previous commit, and it now passes all the tests after merging.

craigbarratt added a commit that referenced this pull request Nov 21, 2026
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>
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