Skip to content
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

🔧 Migrate from Black, isort, flake8, autoflake, pyupgrade to Ruff #763

Merged
merged 12 commits into from
Mar 26, 2024
40 changes: 5 additions & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,13 @@ repos:
- --unsafe
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: pyupgrade
- id: ruff
args:
- --py3-plus
- --keep-runtime-typing
# This file is more readable without yield from
exclude: ^docs_src/progressbar/tutorial004\.py
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
hooks:
- id: autoflake
args:
- --recursive
- --in-place
- --remove-all-unused-imports
- --remove-unused-variables
- --expand-star-imports
- --exclude
- __init__.py
- --remove-duplicate-keys
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- id: isort
name: isort (cython)
types: [cython]
- id: isort
name: isort (pyi)
types: [pyi]
- repo: https://github.com/psf/black
rev: 23.10.0
hooks:
- id: black
- --fix
- id: ruff-format
ci:
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
2 changes: 1 addition & 1 deletion docs_src/arguments/envvar/tutorial002_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def main(
name: Annotated[str, typer.Argument(envvar=["AWESOME_NAME", "GOD_NAME"])] = "World"
name: Annotated[str, typer.Argument(envvar=["AWESOME_NAME", "GOD_NAME"])] = "World",
):
print(f"Hello Mr. {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/arguments/envvar/tutorial003_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def main(
name: Annotated[
str, typer.Argument(envvar="AWESOME_NAME", show_envvar=False)
] = "World"
] = "World",
):
print(f"Hello Mr. {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/arguments/help/tutorial004_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def main(
name: Annotated[
str, typer.Argument(help="Who to greet", show_default=False)
] = "World"
] = "World",
):
"""
Say hi to NAME very gently, like Dirk.
Expand Down
2 changes: 1 addition & 1 deletion docs_src/arguments/help/tutorial005.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def main(
name: str = typer.Argument(
"Wade Wilson", help="Who to greet", show_default="Deadpoolio the amazing's name"
)
),
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/arguments/help/tutorial005_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def main(
typer.Argument(
help="Who to greet", show_default="Deadpoolio the amazing's name"
),
] = "Wade Wilson"
] = "Wade Wilson",
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/help/tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def delete_all(
...,
prompt="Are you sure you want to delete ALL users?",
help="Force deletion without confirmation.",
)
),
):
"""
Delete ALL users in the database.
Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/help/tutorial001_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def delete_all(
prompt="Are you sure you want to delete ALL users?",
help="Force deletion without confirmation.",
),
]
],
):
"""
Delete ALL users in the database.
Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/help/tutorial004.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def create(
username: str = typer.Argument(
..., help="The username to be [green]created[/green]"
)
),
):
"""
[bold green]Create[/bold green] a new [italic]shiny[/italic] user. :sparkles:
Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/help/tutorial004_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def create(
username: Annotated[
str, typer.Argument(help="The username to be [green]created[/green]")
]
],
):
"""
[bold green]Create[/bold green] a new [italic]shinny[/italic] user. :sparkles:
Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/help/tutorial005_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@app.command()
def create(
username: Annotated[str, typer.Argument(help="The username to be **created**")]
username: Annotated[str, typer.Argument(help="The username to be **created**")],
):
"""
**Create** a new *shinny* user. :sparkles:
Expand Down
4 changes: 3 additions & 1 deletion docs_src/commands/options/tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def delete(

@app.command()
def delete_all(
force: bool = typer.Option(..., prompt="Are you sure you want to delete ALL users?")
force: bool = typer.Option(
..., prompt="Are you sure you want to delete ALL users?"
),
):
if force:
print("Deleting all users")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/commands/options/tutorial001_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def delete(
def delete_all(
force: Annotated[
bool, typer.Option(prompt="Are you sure you want to delete ALL users?")
]
],
):
if force:
print("Deleting all users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def main(
names: Tuple[str, str, str] = typer.Argument(
("Harry", "Hermione", "Ron"), help="Select 3 characters to play with"
)
),
):
for name in names:
print(f"Hello {name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def main(
names: Annotated[
Tuple[str, str, str], typer.Argument(help="Select 3 characters to play with")
] = ("Harry", "Hermione", "Ron")
] = ("Harry", "Hermione", "Ron"),
):
for name in names:
print(f"Hello {name}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/options/help/tutorial004.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def main(
fullname: str = typer.Option(
"Wade Wilson", show_default="Deadpoolio the amazing's name"
)
),
):
print(f"Hello {fullname}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options/help/tutorial004_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def main(
fullname: Annotated[
str, typer.Option(show_default="Deadpoolio the amazing's name")
] = "Wade Wilson"
] = "Wade Wilson",
):
print(f"Hello {fullname}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options/prompt/tutorial003_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def main(
project_name: Annotated[str, typer.Option(prompt=True, confirmation_prompt=True)]
project_name: Annotated[str, typer.Option(prompt=True, confirmation_prompt=True)],
):
print(f"Deleting project {project_name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def complete_name():
def main(
name: str = typer.Option(
"World", help="The name to say hi to.", autocompletion=complete_name
)
),
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial003.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def complete_name(incomplete: str):
def main(
name: str = typer.Option(
"World", help="The name to say hi to.", autocompletion=complete_name
)
),
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial004.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def complete_name(incomplete: str):
def main(
name: str = typer.Option(
"World", help="The name to say hi to.", autocompletion=complete_name
)
),
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial005.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def complete_name(incomplete: str):
def main(
name: str = typer.Option(
"World", help="The name to say hi to.", autocompletion=complete_name
)
),
):
print(f"Hello {name}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial006_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@app.command()
def main(
name: Annotated[List[str], typer.Option(help="The name to say hi to.")] = ["World"]
name: Annotated[List[str], typer.Option(help="The name to say hi to.")] = ["World"],
):
for each_name in name:
print(f"Hello {each_name}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial007.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def complete_name(ctx: typer.Context, incomplete: str):
def main(
name: List[str] = typer.Option(
["World"], help="The name to say hi to.", autocompletion=complete_name
)
),
):
for n in name:
print(f"Hello {n}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial008.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def complete_name(args: List[str], incomplete: str):
def main(
name: List[str] = typer.Option(
["World"], help="The name to say hi to.", autocompletion=complete_name
)
),
):
for n in name:
print(f"Hello {n}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/options_autocompletion/tutorial009.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def complete_name(ctx: typer.Context, args: List[str], incomplete: str):
def main(
name: List[str] = typer.Option(
["World"], help="The name to say hi to.", autocompletion=complete_name
)
),
):
for n in name:
print(f"Hello {n}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/datetime/tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def main(
launch_date: datetime = typer.Argument(
..., formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d %H:%M:%S", "%m/%d/%Y"]
)
),
):
print(f"Launch will be at: {launch_date}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/datetime/tutorial002_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def main(
typer.Argument(
formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d %H:%M:%S", "%m/%d/%Y"]
),
]
],
):
print(f"Launch will be at: {launch_date}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/enum/tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NeuralNetwork(str, Enum):


def main(
network: NeuralNetwork = typer.Option(NeuralNetwork.simple, case_sensitive=False)
network: NeuralNetwork = typer.Option(NeuralNetwork.simple, case_sensitive=False),
):
print(f"Training neural network of type: {network.value}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/enum/tutorial002_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NeuralNetwork(str, Enum):
def main(
network: Annotated[
NeuralNetwork, typer.Option(case_sensitive=False)
] = NeuralNetwork.simple
] = NeuralNetwork.simple,
):
print(f"Training neural network of type: {network.value}")

Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/path/tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def main(
writable=False,
readable=True,
resolve_path=True,
)
),
):
text = config.read_text()
print(f"Config file contents: {text}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/parameter_types/path/tutorial002_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main(
readable=True,
resolve_path=True,
),
]
],
):
text = config.read_text()
print(f"Config file contents: {text}")
Expand Down
50 changes: 50 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,53 @@ filterwarnings = [
# For pytest-xdist
'ignore::DeprecationWarning:xdist',
]

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"C901", # too complex
"W191", # indentation contains tabs
]

[tool.ruff.lint.per-file-ignores]
# "__init__.py" = ["F401"]
# This file is more readable without yield from
"docs_src/progressbar/tutorial004.py" = ["UP028", "B007"]
# Default mutable data structure
"docs_src/options_autocompletion/tutorial006_an.py" = ["B006"]
"docs_src/multiple_values/multiple_options/tutorial002_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial007_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial008_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial009_an.py" = ["B006"]
"docs_src/parameter_types/enum/tutorial003_an.py" = ["B006"]
# Loop control variable `value` not used within loop body
"docs_src/progressbar/tutorial001.py" = ["B007"]
"docs_src/progressbar/tutorial003.py" = ["B007"]
"docs_src/progressbar/tutorial005.py" = ["B007"]
"docs_src/progressbar/tutorial006.py" = ["B007"]
# Local variable `delete` is assigned to but never used
"docs_src/prompt/tutorial003.py" = ["F841"]
# Loop control variable `x` not used within loop body
"docs_src/using_click/tutorial001.py" = ["B007"]

# TODO: refactor _typing.py, remove unnecessary code
"typer/_typing.py" = ["UP036", "F822"]

[tool.ruff.lint.isort]
known-third-party = ["typer", "click"]
# For docs_src/subcommands/tutorial003/
known-first-party = ["reigns", "towns", "lands", "items", "users"]

[tool.ruff.lint.pyupgrade]
# Preserve types, even if a file imports `from __future__ import annotations`.
keep-runtime-typing = true
3 changes: 1 addition & 2 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ coverage[toml] >=6.2,<7.0
pytest-xdist >=1.32.0,<4.0.0
pytest-sugar >=0.9.4,<0.10.0
mypy ==0.971
black >=22.3.0,<23.0.0
isort >=5.0.6,<6.0.0
ruff ==0.2.0
Loading
Loading