Skip to content

[mypyc] Support @deprecated on functions and methods - #22163

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

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

Conversation

@rheard

@rheard rheard commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1243.

The runtime @deprecated decorator (PEP 702, from warnings or typing_extensions) stores the message in the __deprecated__ attribute of the function that it wraps. Compiled functions don't support setting attributes, so a module that uses the decorator on a function or a method fails at import:

AttributeError: 'builtin_function_or_method' object has no attribute '__deprecated__'

With this PR mypyc no longer calls the decorator. The compiled function issues the warning itself when it's called. A method isn't a decorated method for mypyc any more, so it's still called natively, and these work as for any other method: property getters and setters, class and static methods, and overriding a native method (or being overridden by one). The implementation of an overloaded function works too. A generator or a coroutine warns when it's created, like with the runtime decorator.

Differences from interpreted Python:

  • The function has no __deprecated__ attribute. This now also applies to nested and async functions,
    where the runtime decorator happened to work. I did this to keep the behavior uniform, but I can leave
    those on the runtime path if that's preferred. It would seem adding support for attributes on compiled methods like __deprecated__ might be a bigger change than even this large PR.

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.

@deprecated (PEP 702) on a compiled function or method makes the module fail at import

1 participant