-
-
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
Fix types, introduce type tests #2562
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1781061
Fix types, introduce type tests
Tinche 6833ebc
Tidy up tox.ini
Tinche b5fa4b9
More tests, more fixes
Tinche 06bead4
Tests for `password_option`
Tinche eac3e75
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 876b6cf
Test for `confirmation_option`
Tinche 694fd19
Test `version_option`
Tinche 1b258ce
Test `help_option`
Tinche 2b3f24e
Add `pyright --verifytypes`
Tinche d3fc283
Tweak line length
Tinche 2eccd6a
pin pyright
davidism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mypy | ||
pyright |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Example from https://click.palletsprojects.com/en/8.1.x/advanced/#command-aliases""" | ||
from __future__ import annotations | ||
|
||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
class AliasedGroup(click.Group): | ||
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None: | ||
rv = click.Group.get_command(self, ctx, cmd_name) | ||
if rv is not None: | ||
return rv | ||
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)] | ||
if not matches: | ||
return None | ||
elif len(matches) == 1: | ||
return click.Group.get_command(self, ctx, matches[0]) | ||
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}") | ||
|
||
def resolve_command( | ||
self, ctx: click.Context, args: list[str] | ||
) -> tuple[str | None, click.Command, list[str]]: | ||
# always return the full command name | ||
_, cmd, args = super().resolve_command(ctx, args) | ||
assert cmd is not None | ||
return cmd.name, cmd, args | ||
|
||
|
||
@click.command(cls=AliasedGroup) | ||
def cli() -> None: | ||
pass | ||
|
||
|
||
assert_type(cli, AliasedGroup) | ||
|
||
|
||
@cli.command() | ||
def push() -> None: | ||
pass | ||
|
||
|
||
@cli.command() | ||
def pop() -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""From https://click.palletsprojects.com/en/8.1.x/options/#yes-parameters""" | ||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.confirmation_option(prompt="Are you sure you want to drop the db?") | ||
def dropdb() -> None: | ||
click.echo("Dropped all tables!") | ||
|
||
|
||
assert_type(dropdb, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.help_option("-h", "--help") | ||
def hello() -> None: | ||
"""Simple program that greets NAME for a total of COUNT times.""" | ||
click.echo("Hello!") | ||
|
||
|
||
assert_type(hello, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""From https://click.palletsprojects.com/en/8.1.x/quickstart/#adding-parameters""" | ||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.option("--count", default=1, help="number of greetings") | ||
@click.argument("name") | ||
def hello(count: int, name: str) -> None: | ||
for _ in range(count): | ||
click.echo(f"Hello {name}!") | ||
|
||
|
||
assert_type(hello, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import codecs | ||
|
||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.password_option() | ||
def encrypt(password: str) -> None: | ||
click.echo(f"encoded: to {codecs.encode(password, 'rot13')}") | ||
|
||
|
||
assert_type(encrypt, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""The simple example from https://github.com/pallets/click#a-simple-example.""" | ||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.option("--count", default=1, help="Number of greetings.") | ||
@click.option("--name", prompt="Your name", help="The person to greet.") | ||
def hello(count: int, name: str) -> None: | ||
"""Simple program that greets NAME for a total of COUNT times.""" | ||
for _ in range(count): | ||
click.echo(f"Hello, {name}!") | ||
|
||
|
||
assert_type(hello, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
From https://click.palletsprojects.com/en/8.1.x/options/#callbacks-and-eager-options. | ||
""" | ||
from typing_extensions import assert_type | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.version_option("0.1") | ||
def hello() -> None: | ||
click.echo("Hello World!") | ||
|
||
|
||
assert_type(hello, click.Command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the same change should be made to
group
's third overload.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but I think it's going to be up to the community to fill out the tests and fix the fallout. This PR is mainly to set up the infrastructure for it.