Registry / serialization / parameter-decorators

parameter-decorators

JSON →
library0.0.2pypypi✓ verified 84d ago

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-decorators
INSTALL
IMPORT
SIG · PARAMETER-DECORATO
P
parameter-decorators
serializationpythonv0.0.2
Install
1.7s avg
Import
45ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.049s · 17.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.7s · import 0.041s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

str_to_path
from parameter_decorators import str_to_path
Used to convert string arguments to pathlib.Path objects.
path_to_str
from parameter_decorators import path_to_str
Used to convert pathlib.Path arguments to string paths. This would typically be used for return values or if a function expects Path but needs to output str.

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.

from parameter_decorators import str_to_path from pathlib import Path class MyProcessor: @str_to_path def process_file(self, path: Path): """Expects a Path object, but decorator allows string input.""" print(f"Processing: {path.name} (Type: {type(path)})") # Simulate file operation if path.exists(): print(f"File '{path}' exists.") else: print(f"File '{path}' does not exist, creating dummy.") path.touch() # Example usage: processor = MyProcessor() processor.process_file("my_document.txt") processor.process_file(Path("another_file.log")) # Can still pass Path directly
Debug
Known issues
gotchaDecorators that modify parameter types (like `parameter-decorators`) can lead to a mismatch between the function's static type hints and its actual runtime acceptance. While the runtime behavior is correct, static analysis tools (e.g., MyPy) might flag type errors or provide misleading autocomplete suggestions if they don't understand the decorator's effect.
fix
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.
affects: All versions
gotchaWhen stacking multiple decorators, their order of application can significantly impact behavior. Decorators are applied from bottom to top. If multiple decorators interact with or modify function arguments, their sequence can lead to unexpected results.
fix
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.
affects: All versions
Errors
Common errors & fixes
TypeError: expected str, bytes or os.PathLike object, not int (or similar for unsupported types)
The `str_to_path` decorator attempts to convert the input argument into a `pathlib.Path` object. If the argument provided is not a string, bytes, or an object convertible to `Path` (e.g., an integer or an arbitrary custom object), the conversion will fail internally within the decorator.
fix
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.
AttributeError: 'function' object has no attribute 'my_method' (when decorating a method but calling from class) or 'MyClass' object has no attribute 'str_to_path'
This usually indicates a misunderstanding of how decorators are applied to methods vs. standalone functions, or attempting to call a method on the class itself rather than an instance. Decorators modify the function/method in place. Also, sometimes users incorrectly try to import the decorator as a method of the class or module it is applied to.
fix
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.
Upgrade
Version history
0.0.2latest on PyPI · released Jan 31, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
parameter-decorators — pip install parameter-decorators · libregistry