Skip to content

Improve type hints#2212

Merged
davidism merged 1 commit into
pallets:mainfrom
matejcik:type-hints
Mar 17, 2022
Merged

Improve type hints#2212
davidism merged 1 commit into
pallets:mainfrom
matejcik:type-hints

Conversation

@matejcik
Copy link
Copy Markdown
Contributor

This PR improves the typpe annotation on @command decorator so that when cls is submitted, the return value is an instance of cls.

In addition, as a small tweak, I lowered the requirement on choices of Choice to Collection -- we only need iteration and length. This way Choice(choices=somedict.keys()) is valid.

Checklist:

  • (n/a) Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • (n/a) 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.
  • (n/a) Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

Comment thread src/click/types.py Outdated
name = "choice"

def __init__(self, choices: t.Sequence[str], case_sensitive: bool = True) -> None:
def __init__(self, choices: t.Collection[str], case_sensitive: bool = True) -> None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this. Requiring a sequence was deliberate on my part, as the order may matter for display purposes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easy enough; force-pushed back to a0bbbf2

@davidism davidism merged commit 5ba2320 into pallets:main Mar 17, 2022
@davidism davidism added this to the 8.1.0 milestone Mar 17, 2022
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect type hint when @command uses a custom class

2 participants