Create a Flexible Python Decorator with Advanced Features
Generate a production-grade Python decorator with async support, type hints, logging, error handling, and pytest coverage.
๐ The Prompt
Write a Python decorator called `[DECORATOR_NAME]` that accomplishes the following: [DECORATOR_PURPOSE].
Follow these detailed requirements:
1. **Decorator Signature**: The decorator should accept the following optional configuration parameters:
- [PARAM_1_NAME] ([PARAM_1_TYPE], default=[PARAM_1_DEFAULT]): [PARAM_1_DESCRIPTION]
- [PARAM_2_NAME] ([PARAM_2_TYPE], default=[PARAM_2_DEFAULT]): [PARAM_2_DESCRIPTION]
The decorator must work both with and without parentheses (i.e., `@[DECORATOR_NAME]` and `@[DECORATOR_NAME](param=value)` should both be valid).
2. **Core Logic**: Implement the main functionality as follows: [DETAILED_BEHAVIOR_DESCRIPTION]. The decorator should handle both synchronous and asynchronous functions using `asyncio.iscoroutinefunction` detection.
3. **Metadata Preservation**: Use `functools.wraps` to preserve the original function's name, docstring, and signature. Additionally, attach the decorator's configuration as a `_[DECORATOR_NAME]_config` attribute on the wrapped function for introspection.
4. **Error Handling**: Implement robust error handling for the following scenarios:
- Invalid parameter types or values passed to the decorator
- Exceptions raised by the decorated function (specify behavior: [RERAISE/SUPPRESS/LOG])
- Include custom exception class `[DECORATOR_NAME]Error` for decorator-specific failures
5. **Logging**: Integrate Python's `logging` module. Log at the following levels:
- DEBUG: Entry and exit of the decorated function with arguments
- INFO: [SPECIFIC_INFO_LEVEL_EVENT]
- WARNING/ERROR: Any caught exceptions or unusual behavior
6. **Type Hints**: Provide complete type hints using `typing` module (ParamSpec, TypeVar, Callable, overload) so that IDEs and mypy correctly infer the return type of decorated functions.
7. **Testing**: Write at least 4 pytest test cases covering:
- Basic decoration with default parameters
- Custom parameter configuration
- Async function decoration
- Error/edge case handling
8. **Documentation**: Include a module-level docstring, a detailed docstring for the decorator with Args/Returns/Raises/Example sections, and inline comments explaining non-obvious logic.
Provide the complete code in a single Python file, organized with imports at the top, followed by exceptions, the decorator, and tests at the bottom.
๐ก Tips for Better Results
Describe the DETAILED_BEHAVIOR_DESCRIPTION with concrete examples โ e.g., 'retry the function up to 3 times with exponential backoff starting at 1 second' rather than 'retry on failure'.
Specify whether exceptions should be reraised, suppressed, or logged to get error handling logic that matches your application's needs.
If you only need sync support, mention that explicitly to get simpler, more focused code without the async complexity.
๐ฏ Use Cases
Python developers who need a robust, reusable decorator for cross-cutting concerns like caching, retrying, rate limiting, or logging, and want production-quality code with full test coverage.
๐ Related Prompts
๐ป Coding
beginner
Explain Code Like Im a Beginner
Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.
๐ป Coding
beginner
Debug My Code and Explain the Fix
Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.
๐ป Coding
intermediate
Write Unit Tests for My Code
Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.
๐ป Coding
intermediate
Convert Code Between Languages
Convert code between any programming languages while maintaining idiomatic patterns and best practices.
๐ป Coding
intermediate
Write a REST API Endpoint
Generate production-ready REST API endpoints with validation, error handling, and documentation.
๐ป Coding
advanced
Refactor Code for Better Performance
Get your code refactored for better performance with Big O analysis and design pattern suggestions.