Parameter-decorators is a lightweight Python library providing handy decorators for automatically converting function or method parameters. It currently supports converting string inputs to `pathlib.Path` objects and vice-versa, streamlining argument handling. The library is in an early stage, with version 0.0.2 released, and aims for a release cadence as new conversion decorators are developed.
pip install parameter-decoratorsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `str_to_path` decorator. The `process_file` method is type-hinted to expect a `Path` object, but thanks to the decorator, it seamlessly accepts a string path, which is then converted before the method's execution. It also shows that you can still pass `Path` objects directly.
Be mindful of this discrepancy during development. If strict type checking is critical, consider adding type ignores or using runtime type checks within the decorated function, or document the decorator's effect clearly.
Carefully consider the order of decorators, especially if they both convert parameters and perform other actions (e.g., logging, validation). Test thoroughly with different decorator stacks.
Ensure that the argument passed to the decorated function is compatible with `pathlib.Path`'s constructor. Typically, this means passing a string representing the path.
Ensure the decorator `@str_to_path` is correctly placed directly above the method definition within the class. Always call decorated methods on an *instance* of the class (e.g., `my_instance.my_method('path')`), not the class itself unless it's a `classmethod` or `staticmethod` and the decorator is designed for that.No dependency data recorded yet.