TypeError: missing 1 required positional argument
GarrisonD opened this issue · 1 comments
@decorator
def dump(func, file_path, *args, **kwargs):
result = func(*args, **kwargs)
with open(file_path, "w") as file:
json.dump(result, file)
return result
...
@dump(file_path='stub.txt')
def get_something():
return {}
This piece of code gives me: TypeError: dump() missing 1 required positional argument: 'file_path'
If I change the signature of dump
, then everything is fine:
- def dump(func, file_path, *args, **kwargs):
+ def dump(func, file_path=None, *args, **kwargs):
But I don't want to add a manual check for file_path
presence... want Python to do it for me.
Am I doing something wrong or it's really the limitation of the decorator
library?
If you read the documentation, it says:
Notice that this simplified approach only works with default arguments. Thanks to this restriction, there exists an unique default decorator, i.e. the member of the family which uses the default values for all parameters. Such decorator can be written as decfactory() with no parameters specified; moreover, as a shortcut, it is also possible to elide the parenthesis, a feature much requested by the users. For years I have been opposing the request, since having explicit parenthesis to me is more clear and less magic; however once this feature entered in decorators of the Python standard library (I am referring to the dataclass decorator) I finally gave up.
The limitation is necessary since people wanted to be able to elide the parenthesis :-(