Skip to content

Add support for optional decorator parentheses - #1896

Merged
davidism merged 1 commit into
pallets:mainfrom
angus-lherrou:1359-uncalled-decorators
Feb 19, 2022
Merged

Add support for optional decorator parentheses#1896
davidism merged 1 commit into
pallets:mainfrom
angus-lherrou:1359-uncalled-decorators

Conversation

@angus-lherrou

@angus-lherrouangus-lherrou commented May 14, 2021

Copy link
Copy Markdown
Contributor

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@angus-lherrou

Copy link
Copy Markdown
ContributorAuthor

Maintainers, I'm not sure if this change needs any docs updates?

@ThiefMaster

Copy link
Copy Markdown
Member

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...

@davidism

davidism commented May 14, 2021

Copy link
Copy Markdown
Member

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 @pass_context() wouldn't cause an error either...)

@ThiefMaster

Copy link
Copy Markdown
Member

I know pytest does it for example, but no idea if they use type hints..

@ThiefMaster

Copy link
Copy Markdown
Member

@davidism

Copy link
Copy Markdown
Member

@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.

@angus-lherrou

Copy link
Copy Markdown
ContributorAuthor

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.

@davidismdavidism added this to the 8.1.0 milestone May 14, 2021
@angus-lherrou
angus-lherrouforce-pushed the 1359-uncalled-decorators branch from 2ea1569 to e61d54eCompareMay 14, 2021 16:15
@angus-lherrouangus-lherrou changed the title Add descriptive error messages for when parentheses are omitted on decoratorsAdd support for optional decorator parenthesesMay 14, 2021
@angus-lherrou

Copy link
Copy Markdown
ContributorAuthor

@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.

@angus-lherrou

Copy link
Copy Markdown
ContributorAuthor

I suppose it would be good to add this new capability in the documentation somewhere

Comment threadsrc/click/decorators.py Outdated
Comment threadsrc/click/decorators.py
@angus-lherrouangus-lherrou mentioned this pull request Oct 13, 2021
@davidism
davidismforce-pushed the 1359-uncalled-decorators branch from 333d596 to 74e7f70CompareFebruary 19, 2022 18:14
@davidism
davidism merged commit 24992ce into pallets:mainFeb 19, 2022
@github-actionsgithub-actionsBot locked as resolved and limited conversation to collaborators Mar 6, 2022
@angus-lherrou
angus-lherrou deleted the 1359-uncalled-decorators branch March 29, 2022 15:17
@kdeldyckekdeldycke added the parsing Parsing, parameters, commands, chaining, context label Aug 8, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

parsingParsing, parameters, commands, chaining, context

Projects

None yet

Development

Successfully merging this pull request may close these issues.

raise a more descriptive exception when @command decorator isn't called

4 participants

@angus-lherrou@ThiefMaster@davidism@kdeldycke