Skip to content

[mypyc] Fix crash on a property with another decorator - #22157

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

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

Conversation

@rheard

@rheard rheard commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1238.

mypy only rejects decorators on top of @property, so it accepts a property like this:

class C:
    @property
    @functools.lru_cache(maxsize=None)
    def p(self) -> int: ...

In a native class, prepare_method_def() left the getter out of method_decls, like any decorated method, but still set is_prop_getter and property_types. Reading c.p in compiled code then generated a native GetAttr with no getter behind it, and the compiler crashed in emit_method_call with KeyError: "'C' has no attribute 'p'". If nothing compiled read the property, the module compiled, but the method was missing from the type's method table, so the decorator was called with the property's getset_descriptor and its result replaced the property. With lru_cache the import failed (TypeError: the first argument must be callable).

A getter with other decorators is now handled like other decorated methods. handle_ext_method() applies the decorators, wraps the result with builtins.property and sets it on the class. Non-native classes already did this. The property isn't a native one, so compiled code reads it with a generic attribute lookup and coerces the result to the type mypy inferred, which is the decorated function's return type. @functools.cached_property over another decorator takes the same path.

A setter for such a property would need the native setter exposed as a Python callable, so for now it's reported as an error instead of crashing:

error: Property setter not supported when the getter has other decorators

@property over @abstractmethod, @override or @final isn't affected, since mypy removes those from the decorator list.

A decorated property that overrides a native property still fails like a decorated method that overrides a native method (mypyc/mypyc#767). This PR doesn't change that.

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.

Compiler crash on a property that has another decorator below @property

1 participant