Add proper deprecation warnings for deprecated module attributes - #974
Add proper deprecation warnings for deprecated module attributes#974Carreau wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Three APIs were documented as deprecated but never warned. Deprecation versions traced through git history: - `traitlets.traitlets.NoDefaultSpecified`, an alias for `Undefined` since 8261449 (2015-06-17, released in 4.0). Turned into a module-level `__getattr__` (PEP 562) so accessing it warns; `__dir__` keeps it discoverable. - `Config.has_key`, a Python-2-era alias for `__contains__` that arrived already marked deprecated in the IPython.config -> traitlets.config split, c34e315 (2015-03-28, released in 4.0). Now a real method that warns before delegating. - `Configurable._get_log_handler`, marked deprecated in its docstring by b5b5085 (2022-02-07, released in 5.2). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AEeaWWBdmyp9x2DaCombXD
76dd927 to
7f791be
Compare
|
The two red downstream checks ( Single failure, in jupyter_server's own suite: Root cause is a assert check_version(1.0, "1.0.1") # type:ignore[arg-type]and jupyter_server's try:
return bool(Version(v) >= Version(check))
except TypeError:
return TrueOlder
Nothing in this PR touches version parsing — the diff only adds The fix belongs in jupyter_server (catch Generated by Claude Code |
|
Failures will be fixed by jupyter-server/jupyter_server#1688 |
Summary
This PR improves deprecation handling across the traitlets codebase by implementing proper
DeprecationWarningemissions for deprecated APIs instead of silent aliases. This follows PEP 562 for module-level__getattr__and ensures users are properly notified when using deprecated features.Key Changes
traitlets/traitlets.py: Replaced silent alias
NoDefaultSpecified = Undefinedwith a module-level__getattr__implementation that emits aDeprecationWarningwhen the deprecated name is accessed. Added__dir__()to keep deprecated attributes discoverable.traitlets/config/configurable.py: Added explicit
DeprecationWarningtoConfigurable._get_log_handler()method with updated docstring indicating the deprecation version and recommended alternative.traitlets/config/loader.py: Converted
Config.has_keyfrom a simple alias to a proper method that emits aDeprecationWarningbefore delegating to__contains__().tests/test_traitlets.py: Added
test_no_default_specified_deprecated()to verify the deprecation warning is emitted and that the deprecated name remains discoverable viadir().tests/config/test_loader.py: Added
test_has_key_deprecated()to verifyConfig.has_key()emits appropriate deprecation warnings.tests/config/test_configurable.py: Added
test_get_log_handler_deprecated()to verify_get_log_handler()emits a deprecation warning.Implementation Details
The changes use Python's standard
warnings.warn()withDeprecationWarningandstacklevel=2to ensure warnings point to the caller's code. The module-level__getattr__approach (PEP 562) allows deprecated names to be accessed with warnings while keeping the module namespace clean and maintaining discoverability through__dir__().https://claude.ai/code/session_01AEeaWWBdmyp9x2DaCombXD