diff --git a/.github/actions/comment-docs-preview-in-pr/app/main.py b/.github/actions/comment-docs-preview-in-pr/app/main.py index c9fb7cbbef..051acb9cdd 100644 --- a/.github/actions/comment-docs-preview-in-pr/app/main.py +++ b/.github/actions/comment-docs-preview-in-pr/app/main.py @@ -62,7 +62,7 @@ class PartialGithubEvent(BaseModel): "body": f"📝 Docs preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}" }, ) - if not (200 <= response.status_code <= 300): + if not 200 <= response.status_code <= 300: logging.error(f"Error posting comment: {response.text}") sys.exit(1) logging.info("Finished") diff --git a/docs_src/subcommands/tutorial001/main.py b/docs_src/subcommands/tutorial001/main.py index c30ccff8f4..be54a34654 100644 --- a/docs_src/subcommands/tutorial001/main.py +++ b/docs_src/subcommands/tutorial001/main.py @@ -1,8 +1,8 @@ -import typer - import items import users +import typer + app = typer.Typer() app.add_typer(users.app, name="users") app.add_typer(items.app, name="items") diff --git a/docs_src/subcommands/tutorial003/lands.py b/docs_src/subcommands/tutorial003/lands.py index 724c206481..9137f6c74a 100644 --- a/docs_src/subcommands/tutorial003/lands.py +++ b/docs_src/subcommands/tutorial003/lands.py @@ -1,8 +1,8 @@ -import typer - import reigns import towns +import typer + app = typer.Typer() app.add_typer(reigns.app, name="reigns") app.add_typer(towns.app, name="towns") diff --git a/docs_src/subcommands/tutorial003/main.py b/docs_src/subcommands/tutorial003/main.py index b3b4ed7ccd..bf4836cd44 100644 --- a/docs_src/subcommands/tutorial003/main.py +++ b/docs_src/subcommands/tutorial003/main.py @@ -1,9 +1,9 @@ -import typer - import items import lands import users +import typer + app = typer.Typer() app.add_typer(users.app, name="users") app.add_typer(items.app, name="items") diff --git a/docs_src/terminating/tutorial001.py b/docs_src/terminating/tutorial001.py index ff6033a3dc..7db5a86bf4 100644 --- a/docs_src/terminating/tutorial001.py +++ b/docs_src/terminating/tutorial001.py @@ -7,8 +7,7 @@ def maybe_create_user(username: str): if username in existing_usernames: print("The user already exists") raise typer.Exit() - else: - print(f"User created: {username}") + print(f"User created: {username}") def send_new_user_notification(username: str): diff --git a/docs_src/using_click/tutorial001.py b/docs_src/using_click/tutorial001.py index 6fbf00649a..b3260c6494 100644 --- a/docs_src/using_click/tutorial001.py +++ b/docs_src/using_click/tutorial001.py @@ -7,7 +7,7 @@ def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): - click.echo("Hello %s!" % name) + click.echo(f"Hello {name}!") if __name__ == "__main__": diff --git a/docs_src/using_click/tutorial003.py b/docs_src/using_click/tutorial003.py index a8f99e4623..5b3967c015 100644 --- a/docs_src/using_click/tutorial003.py +++ b/docs_src/using_click/tutorial003.py @@ -23,7 +23,7 @@ def callback(): @click.option("--name", prompt="Your name", help="The person to greet.") def hello(name): """Simple program that greets NAME for a total of COUNT times.""" - click.echo("Hello %s!" % name) + click.echo(f"Hello {name}!") typer_click_object = typer.main.get_command(app) diff --git a/tests/test_completion/test_completion_complete.py b/tests/test_completion/test_completion_complete.py index e3fbb3154a..390dac6954 100644 --- a/tests/test_completion/test_completion_complete.py +++ b/tests/test_completion/test_completion_complete.py @@ -71,7 +71,7 @@ def test_completion_complete_subcommand_zsh_files(): "_TYPER_COMPLETE_TESTING": "True", }, ) - assert ("_files") in result.stdout + assert "_files" in result.stdout def test_completion_complete_subcommand_fish(): @@ -177,4 +177,4 @@ def test_completion_complete_subcommand_noshell(): "_TYPER_COMPLETE_TESTING": "True", }, ) - assert ("") in result.stdout + assert "" in result.stdout diff --git a/tests/test_completion/test_completion_complete_no_help.py b/tests/test_completion/test_completion_complete_no_help.py index bf5762c06d..2f205f8abd 100644 --- a/tests/test_completion/test_completion_complete_no_help.py +++ b/tests/test_completion/test_completion_complete_no_help.py @@ -52,7 +52,7 @@ def test_completion_complete_subcommand_powershell(): "_TYPER_COMPLETE_TESTING": "True", }, ) - assert ("create::: \ndelete::: ") in result.stdout + assert "create::: \ndelete::: " in result.stdout def test_completion_complete_subcommand_pwsh(): @@ -68,4 +68,4 @@ def test_completion_complete_subcommand_pwsh(): "_TYPER_COMPLETE_TESTING": "True", }, ) - assert ("create::: \ndelete::: ") in result.stdout + assert "create::: \ndelete::: " in result.stdout diff --git a/tests/test_completion/test_completion_install.py b/tests/test_completion/test_completion_install.py index 0856b9b565..485c2f465d 100644 --- a/tests/test_completion/test_completion_install.py +++ b/tests/test_completion/test_completion_install.py @@ -152,7 +152,7 @@ def test_completion_install_fish(): def test_completion_install_powershell(): completion_path: Path = ( - Path.home() / f".config/powershell/Microsoft.PowerShell_profile.ps1" + Path.home() / ".config/powershell/Microsoft.PowerShell_profile.ps1" ) completion_path_bytes = f"{completion_path}\n".encode("windows-1252") text = "" diff --git a/typer/__init__.py b/typer/__init__.py index 26baa7acad..8c1c802004 100644 --- a/typer/__init__.py +++ b/typer/__init__.py @@ -2,38 +2,40 @@ __version__ = "0.7.0" -from shutil import get_terminal_size as get_terminal_size +from shutil import get_terminal_size -from click.exceptions import Abort as Abort -from click.exceptions import BadParameter as BadParameter -from click.exceptions import Exit as Exit -from click.termui import clear as clear -from click.termui import confirm as confirm -from click.termui import echo_via_pager as echo_via_pager -from click.termui import edit as edit -from click.termui import getchar as getchar -from click.termui import launch as launch -from click.termui import pause as pause -from click.termui import progressbar as progressbar -from click.termui import prompt as prompt -from click.termui import secho as secho -from click.termui import style as style -from click.termui import unstyle as unstyle -from click.utils import echo as echo -from click.utils import format_filename as format_filename -from click.utils import get_app_dir as get_app_dir -from click.utils import get_binary_stream as get_binary_stream -from click.utils import get_text_stream as get_text_stream -from click.utils import open_file as open_file +from click.exceptions import Abort, BadParameter, Exit +from click.termui import ( + clear, + confirm, + echo_via_pager, + edit, + getchar, + launch, + pause, + progressbar, + prompt, + secho, + style, + unstyle, +) +from click.utils import ( + echo, + format_filename, + get_app_dir, + get_binary_stream, + get_text_stream, + open_file, +) -from . import colors as colors -from .main import Typer as Typer -from .main import run as run -from .models import CallbackParam as CallbackParam -from .models import Context as Context -from .models import FileBinaryRead as FileBinaryRead -from .models import FileBinaryWrite as FileBinaryWrite -from .models import FileText as FileText -from .models import FileTextWrite as FileTextWrite -from .params import Argument as Argument -from .params import Option as Option +from . import colors +from .main import Typer, run +from .models import ( + CallbackParam, + Context, + FileBinaryRead, + FileBinaryWrite, + FileText, + FileTextWrite, +) +from .params import Argument, Option diff --git a/typer/_compat_utils.py b/typer/_compat_utils.py index 637e8ceb0d..eaea1636df 100644 --- a/typer/_compat_utils.py +++ b/typer/_compat_utils.py @@ -2,4 +2,4 @@ def _get_click_major() -> int: - return int(click.__version__.split(".")[0]) + return int(click.__version__.split(".", maxsplit=1)[0]) diff --git a/typer/_completion_click7.py b/typer/_completion_click7.py index 9f4ad73f30..085f2a0087 100644 --- a/typer/_completion_click7.py +++ b/typer/_completion_click7.py @@ -107,11 +107,11 @@ def do_powershell_complete(cli: click.Command, prog_name: str) -> bool: def do_shell_complete(*, cli: click.Command, prog_name: str, shell: str) -> bool: if shell == "bash": return do_bash_complete(cli, prog_name) - elif shell == "zsh": + if shell == "zsh": return do_zsh_complete(cli, prog_name) - elif shell == "fish": + if shell == "fish": return do_fish_complete(cli, prog_name) - elif shell in {"powershell", "pwsh"}: + if shell in {"powershell", "pwsh"}: return do_powershell_complete(cli, prog_name) return False @@ -130,7 +130,7 @@ def handle_shell_complete( ) ) return True - elif command == "complete": + if command == "complete": return do_shell_complete(cli=cli, prog_name=prog_name, shell=shell) click.echo(f'Completion instruction "{command}" not supported.', err=True) return False diff --git a/typer/_completion_click8.py b/typer/_completion_click8.py index 54e2b03d6f..a307b6dd6d 100644 --- a/typer/_completion_click8.py +++ b/typer/_completion_click8.py @@ -93,8 +93,7 @@ def escape(s: str) -> str: # return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}" if item.help: return f'"{escape(item.value)}":"{escape(item.help)}"' - else: - return f'"{escape(item.value)}"' + return f'"{escape(item.value)}"' def complete(self) -> str: args, incomplete = self.get_completion_args() @@ -103,8 +102,7 @@ def complete(self) -> str: if res: args_str = "\n".join(res) return f"_arguments '*: :(({args_str}))'" - else: - return "_files" + return "_files" class FishComplete(click.shell_completion.FishComplete): @@ -139,8 +137,7 @@ def format_completion(self, item: click.shell_completion.CompletionItem) -> str: if item.help: formatted_help = re.sub(r"\s", " ", item.help) return f"{item.value}\t{formatted_help}" - else: - return f"{item.value}" + return f"{item.value}" def complete(self) -> str: complete_action = os.getenv("_TYPER_COMPLETE_FISH_ACTION", "") diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 7cbaf98d75..8cb492f8e3 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -14,9 +14,6 @@ shellingham = None -from typing import Optional - - class Shells(str, Enum): bash = "bash" zsh = "zsh" @@ -92,7 +89,7 @@ def get_completion_script(*, prog_name: str, complete_var: str, shell: str) -> s return ( script % dict( - complete_func="_{}_completion".format(cf_name), + complete_func=f"_{cf_name}_completion", prog_name=prog_name, autocomplete_var=complete_var, ) @@ -224,21 +221,20 @@ def install( prog_name=prog_name, complete_var=complete_var, shell=shell ) return shell, installed_path - elif shell == "zsh": + if shell == "zsh": installed_path = install_zsh( prog_name=prog_name, complete_var=complete_var, shell=shell ) return shell, installed_path - elif shell == "fish": + if shell == "fish": installed_path = install_fish( prog_name=prog_name, complete_var=complete_var, shell=shell ) return shell, installed_path - elif shell in {"powershell", "pwsh"}: + if shell in {"powershell", "pwsh"}: installed_path = install_powershell( prog_name=prog_name, complete_var=complete_var, shell=shell ) return shell, installed_path - else: - click.echo(f"Shell {shell} is not supported.") - raise click.exceptions.Exit(1) + click.echo(f"Shell {shell} is not supported.") + raise click.exceptions.Exit(1) diff --git a/typer/core.py b/typer/core.py index c0776368a6..19d8e2b319 100644 --- a/typer/core.py +++ b/typer/core.py @@ -400,7 +400,7 @@ def make_metavar(self) -> str: return self.metavar var = (self.name or "").upper() if not self.required: - var = "[{}]".format(var) + var = f"[{var}]" type_var = self.type.get_metavar(self) if type_var: var += f":{type_var}" diff --git a/typer/main.py b/typer/main.py index f493e544e2..d7db50fe20 100644 --- a/typer/main.py +++ b/typer/main.py @@ -352,7 +352,7 @@ def get_command(typer_instance: Typer) -> click.Command: click_command.params.append(click_install_param) click_command.params.append(click_show_param) return click_command - elif len(typer_instance.registered_commands) == 1: + if len(typer_instance.registered_commands) == 1: # Create a single Command single_command = typer_instance.registered_commands[0] @@ -691,7 +691,7 @@ def get_click_type( ) -> click.ParamType: if annotation == str: return click.STRING - elif annotation == int: + if annotation == int: if parameter_info.min is not None or parameter_info.max is not None: min_ = None max_ = None @@ -700,24 +700,22 @@ def get_click_type( if parameter_info.max is not None: max_ = int(parameter_info.max) return click.IntRange(min=min_, max=max_, clamp=parameter_info.clamp) - else: - return click.INT - elif annotation == float: + return click.INT + if annotation == float: if parameter_info.min is not None or parameter_info.max is not None: return click.FloatRange( min=parameter_info.min, max=parameter_info.max, clamp=parameter_info.clamp, ) - else: - return click.FLOAT - elif annotation == bool: + return click.FLOAT + if annotation == bool: return click.BOOL - elif annotation == UUID: + if annotation == UUID: return click.UUID - elif annotation == datetime: + if annotation == datetime: return click.DateTime(formats=parameter_info.formats) - elif ( + if ( annotation == Path or parameter_info.allow_dash or parameter_info.path_type @@ -733,7 +731,7 @@ def get_click_type( allow_dash=parameter_info.allow_dash, path_type=parameter_info.path_type, ) - elif lenient_issubclass(annotation, FileTextWrite): + if lenient_issubclass(annotation, FileTextWrite): return click.File( mode=parameter_info.mode or "w", encoding=parameter_info.encoding, @@ -741,7 +739,7 @@ def get_click_type( lazy=parameter_info.lazy, atomic=parameter_info.atomic, ) - elif lenient_issubclass(annotation, FileText): + if lenient_issubclass(annotation, FileText): return click.File( mode=parameter_info.mode or "r", encoding=parameter_info.encoding, @@ -749,7 +747,7 @@ def get_click_type( lazy=parameter_info.lazy, atomic=parameter_info.atomic, ) - elif lenient_issubclass(annotation, FileBinaryRead): + if lenient_issubclass(annotation, FileBinaryRead): return click.File( mode=parameter_info.mode or "rb", encoding=parameter_info.encoding, @@ -757,7 +755,7 @@ def get_click_type( lazy=parameter_info.lazy, atomic=parameter_info.atomic, ) - elif lenient_issubclass(annotation, FileBinaryWrite): + if lenient_issubclass(annotation, FileBinaryWrite): return click.File( mode=parameter_info.mode or "wb", encoding=parameter_info.encoding, @@ -765,7 +763,7 @@ def get_click_type( lazy=parameter_info.lazy, atomic=parameter_info.atomic, ) - elif lenient_issubclass(annotation, Enum): + if lenient_issubclass(annotation, Enum): return click.Choice( [item.value for item in annotation], case_sensitive=parameter_info.case_sensitive, @@ -850,7 +848,7 @@ def get_click_param( if is_tuple: convertor = generate_tuple_convertor(main_type.__args__) if isinstance(parameter_info, OptionInfo): - if main_type is bool and not (parameter_info.is_flag is False): + if main_type is bool and not parameter_info.is_flag is False: is_flag = True # Click doesn't accept a flag of type bool, only None, and then it sets it # to bool internally @@ -903,7 +901,7 @@ def get_click_param( ), convertor, ) - elif isinstance(parameter_info, ArgumentInfo): + if isinstance(parameter_info, ArgumentInfo): param_decls = [param.name] nargs = None if is_list: diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 3f602ee651..03218f5fd0 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -162,8 +162,7 @@ def _make_rich_rext( return Markdown(text, style=style) if markup_mode == MARKUP_MODE_RICH: return highlighter(Text.from_markup(text, style=style)) - else: - return highlighter(Text(text, style=style)) + return highlighter(Text(text, style=style)) @group()