-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for optional decorator parentheses #1896
Conversation
Maintainers, I'm not sure if this change needs any docs updates? |
Wouldn't it be better if the decorators were smart enough to work even without parentheses, ie if there's exactly one positional argument and that argument is a callable, assume it's the decorated function... |
I go back and forth on that pattern. Right now I'm on the side of "there should be one way to do things". Even if we can detect it, better to tell everyone to do something consistently. I'd have to look at the mypy docs, but I have a feeling the "decorator or factory" pattern makes typing more complicated too. For example, non-factory decorators don't typically also support the factory pattern. (Although maybe they should, then PyCharm incorrectly doing |
I know pytest does it for example, but no idea if they use type hints.. |
@angus-lherrou if you're up for it, want to implement this dual option instead of showing an error? If not, we can merge this then make another issue about supporting plain decorators. |
My first thought was actually the same, but I spent some time trying to implement the optional parentheses and it just wasn't working the way I was hoping it would. I can take a look at how pytest implements it and take another crack at it. |
2ea1569
to
e61d54e
Compare
@davidism I figured it out! When I tried before I didn't realize that I needed to call the inner function before returning it if the decorator isn't called at the invocation site. |
I suppose it would be good to add this new capability in the documentation somewhere |
333d596
to
74e7f70
Compare
Checklist:
CHANGES.rst
summarizing the change and linking to the issue... versionchanged::
entries in any relevant code docs.pre-commit
hooks and fix any issues.pytest
andtox
, no tests failed.