-
Notifications
You must be signed in to change notification settings - Fork 20.1k
feat(core): add PEP 702 __deprecated__ attribute support to @deprecated
#34257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #34257 will not alter performanceComparing
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds PEP 702 __deprecated__ attribute support to the @deprecated decorator, enabling IDEs and type checkers (like Pyright and VS Code Pylance) to display deprecation warnings inline without requiring code execution. This improves the developer experience by showing strikethrough text and hover messages for deprecated APIs.
Key Changes:
- Added
_build_deprecation_message()helper function to generate PEP 702-compliant deprecation messages - Set
__deprecated__attribute on deprecated classes, properties, and functions/methods - Messages include alternative APIs when specified via
alternativeoralternative_importparameters
| alternative=_alternative, | ||
| alternative_import=_alternative_import, |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The __deprecated__ attribute is being set with _alternative and _alternative_import values that have been modified to include markdown backticks (lines 389-401). This means the PEP 702 deprecation message shown in IDEs will contain backticks like "Use some_function instead."
PEP 702's __deprecated__ attribute is meant for display in IDEs and type checkers, not for documentation, so it should use plain text without markdown formatting. The _build_deprecation_message calls should use the original, unmodified values of alternative and alternative_import parameters from the outer scope, not _alternative and _alternative_import which have been wrapped in backticks.
| alternative=_alternative, | |
| alternative_import=_alternative_import, | |
| alternative=alternative, | |
| alternative_import=alternative_import, |
| # Set __deprecated__ for PEP 702 (IDE/type checker support) | ||
| prop.__deprecated__ = _build_deprecation_message( # type: ignore[attr-defined] | ||
| alternative=_alternative, | ||
| alternative_import=_alternative_import, | ||
| ) |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as lines 247-251: _alternative and _alternative_import have been modified to include markdown backticks before being passed to _build_deprecation_message. Use the original alternative and alternative_import parameters instead.
| alternative=_alternative, | ||
| alternative_import=_alternative_import, |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as lines 247-251 and 347-351: _alternative and _alternative_import have been modified to include markdown backticks before being passed to _build_deprecation_message. Use the original alternative and alternative_import parameters instead.
| alternative=_alternative, | |
| alternative_import=_alternative_import, | |
| alternative=alternative, | |
| alternative_import=alternative_import, |
| def _build_deprecation_message( | ||
| *, | ||
| alternative: str = "", | ||
| alternative_import: str = "", | ||
| ) -> str: | ||
| """Build a simple deprecation message for __deprecated__ attribute (PEP 702). | ||
| Args: | ||
| alternative: An alternative API name. | ||
| alternative_import: A fully qualified import path for the alternative. | ||
| Returns: | ||
| A deprecation message string for IDE/type checker display. | ||
| """ | ||
| if alternative_import: | ||
| return f"Use {alternative_import} instead." | ||
| if alternative: | ||
| return f"Use {alternative} instead." | ||
| return "Deprecated." |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new __deprecated__ attribute functionality lacks test coverage. Tests should verify that:
- The
__deprecated__attribute is set correctly on deprecated functions, classes, and properties - The attribute contains the expected message format (plain text without markdown backticks)
- The attribute is set with the correct alternative/alternative_import values when provided
Example test:
def test_deprecated_function_has_pep702_attribute():
@deprecated(since="2.0.0", alternative="new_function")
def old_function():
pass
assert hasattr(old_function, '__deprecated__')
assert old_function.__deprecated__ == "Use new_function instead."
IDEs and type checkers that support PEP 702 (e.g., Pyright, VS Code Pylance) can now display deprecation warnings inline—showing strikethrough text and hover messages—without requiring code execution. Improves DX.