Skip to content

[Design] Deprecation timing for Decorators: Definition-time vs. Call-time #96

@Borda

Description

@Borda

Description

Context

TODO: Reconsider and/or refine the deprecation warning behavior for decorator syntax.
When using @decorator syntax, the warning is currently raised at decoration time
(during function/class definition or module import), not at call time.
This might differ from the intended behavior if warnings should be raised at runtime.
Consider whether the warning should be raised:
1. Only once during import/decoration (current behavior)
2. At every function call (runtime behavior)
3. Once per function call location (hybrid approach)
This could be a future improvement to make decorator deprecation more consistent
with function/class deprecation patterns.
Current test coverage:
- TestDeprecatedFunctionWrappers.test_with_decorator_syntax (line ~348)
- TestDeprecatedClassWrappers.test_with_decorator_syntax (line ~447)

This issue serves to formalize the design discussion regarding when a warning should be raised if the deprecated object is itself a decorator.

The Scenario

When a developer deprecates a decorator (e.g., @my_deprecated_wrapper) and a user applies it to their code:

@my_deprecated_wrapper  # <--- Should the warning fire here? (Definition/Import Time)
def my_function():
    pass

my_function()  # <--- Or should the warning fire here? (Runtime/Call Time)

The Design Decision

We need to determine the expected behavior for this specific use case:

  1. Warn at Definition (Import Time):

    • Behavior: The warning is raised immediately when the Python interpreter processes the @decorator syntax.
    • Pro: Immediate feedback. The "usage" of the decorator technically happens at definition.
    • Con: Can be noisy during imports; harder to suppress via runtime flags if the function is never actually called.
  2. Warn at Execution (Runtime/Call Time):

    • Behavior: The warning is delayed until the decorated function is actually invoked.
    • Pro: Consistent with standard function deprecation; avoids import-side effects.
    • Con: If the function is rarely called, the developer might not realize they are using a deprecated pattern until it is too late.

Action Items

  • Collect Feedback: Gather user expectations on which behavior is less intrusive yet sufficiently visible.
  • Gather Use Cases: Identify real-world scenarios where "Definition Time" warnings are critical versus where they might be a hindrance.
  • Decide & Implement: Finalize the logic in pyDeprecate and update documentation.
  • Cleanup: Remove the TODO note from tests/test_functions.py.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions