-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix types, introduce type tests (#2562)
- Loading branch information
Showing
14 changed files
with
157 additions
and
19 deletions.
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