Skip to content

[mypyc] Fix calling a property that returns a callable - #22139

Open
rheard wants to merge 1 commit into
python:masterfrom
rheard:fix-mypyc-1212
Open

rheard wants to merge 1 commit into
python:masterfrom
rheard:fix-mypyc-1212

Conversation

@rheard

@rheard rheard commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1212.

mypyc stores a property's getter as a method under the property's name, and gen_method_call checks for a method before it checks for an attribute. So obj.prop(x) on a native class was compiled as a direct call to the getter: x was dropped, and the call returned the property's value instead of calling it. For the example in the issue, the IR was:

r0 = config.sorting_function()
r1 = cast(list, r0)

This usually failed with a TypeError like the one in the issue (list object expected; got builtin_function_or_method). When the result type was object or Any (for example with Callable[[], object]), it silently returned the callable itself. super().prop(x) had the same problem in translate_super_method_call.

Now neither place treats a property getter as the method to call. obj.prop(x) calls the getter and then calls the value it returns, as mypyc already does for an attribute that holds a callable:

r0 = config.sorting_function
r1 = [to_sort]
r2 = load_address r1
r3 = PyObject_Vectorcall(r0, r2, 1, 0)
keep_alive to_sort
r4 = cast(list, r3)

super().prop(x) gets the value through super(), as super().prop already does..

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.

TypeError when calling property that returns Callable that can be assigned as instance variable

1 participant