Skip to content

Commit 42ff4eb

Browse files
committed
Fix typing for Python 3.9 for Typer
1 parent d9ed642 commit 42ff4eb

File tree

10 files changed

+32
-29
lines changed

10 files changed

+32
-29
lines changed

infrahub_sdk/ctl/check.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from asyncio import run as aiorun
66
from dataclasses import dataclass
77
from pathlib import Path
8-
from typing import TYPE_CHECKING
8+
from typing import TYPE_CHECKING, Optional
99

1010
import typer
1111
from rich.console import Console
@@ -50,8 +50,8 @@ def run(
5050
format_json: bool,
5151
list_available: bool,
5252
variables: dict[str, str],
53-
name: str | None = None,
54-
branch: str | None = None,
53+
name: Optional[str] = None,
54+
branch: Optional[str] = None,
5555
) -> None:
5656
"""Locate and execute all checks under the defined path."""
5757

@@ -88,7 +88,7 @@ async def run_check(
8888
format_json: bool,
8989
path: str,
9090
repository_config: InfrahubRepositoryConfig,
91-
branch: str | None = None,
91+
branch: Optional[str] = None,
9292
params: dict | None = None,
9393
) -> bool:
9494
module_name = check_module.name
@@ -135,7 +135,7 @@ async def run_targeted_check(
135135
path: str,
136136
repository_config: InfrahubRepositoryConfig,
137137
variables: dict[str, str],
138-
branch: str | None = None,
138+
branch: Optional[str] = None,
139139
) -> bool:
140140
filters = {}
141141
param_value = list(check_module.definition.parameters.values())
@@ -187,7 +187,7 @@ async def run_checks(
187187
path: str,
188188
variables: dict[str, str],
189189
repository_config: InfrahubRepositoryConfig,
190-
branch: str | None = None,
190+
branch: Optional[str] = None,
191191
) -> None:
192192
log = logging.getLogger("infrahub")
193193

infrahub_sdk/ctl/cli_commands.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import platform
88
import sys
99
from pathlib import Path
10-
from typing import TYPE_CHECKING, Any, Callable
10+
from typing import TYPE_CHECKING, Any, Callable, Optional
1111

1212
import jinja2
1313
import typer
@@ -74,13 +74,13 @@
7474
@catch_exception(console=console)
7575
def check(
7676
check_name: str = typer.Argument(default="", help="Name of the Python check"),
77-
branch: str | None = None,
77+
branch: Optional[str] = None,
7878
path: str = typer.Option(".", help="Root directory"),
7979
debug: bool = False,
8080
format_json: bool = False,
8181
_: str = CONFIG_PARAM,
8282
list_available: bool = typer.Option(False, "--list", help="Show available Python checks"),
83-
variables: list[str] | None = typer.Argument(
83+
variables: Optional[list[str]] = typer.Argument(
8484
None, help="Variables to pass along with the query. Format key=value key=value."
8585
),
8686
) -> None:
@@ -102,12 +102,12 @@ def check(
102102
@catch_exception(console=console)
103103
async def generator(
104104
generator_name: str = typer.Argument(default="", help="Name of the Generator"),
105-
branch: str | None = None,
105+
branch: Optional[str] = None,
106106
path: str = typer.Option(".", help="Root directory"),
107107
debug: bool = False,
108108
_: str = CONFIG_PARAM,
109109
list_available: bool = typer.Option(False, "--list", help="Show available Generators"),
110-
variables: list[str] | None = typer.Argument(
110+
variables: Optional[list[str]] = typer.Argument(
111111
None, help="Variables to pass along with the query. Format key=value key=value."
112112
),
113113
) -> None:
@@ -130,13 +130,13 @@ async def run(
130130
debug: bool = False,
131131
_: str = CONFIG_PARAM,
132132
branch: str = typer.Option("main", help="Branch on which to run the script."), # TODO: Replace main by None
133-
concurrent: int | None = typer.Option(
133+
concurrent: Optional[int] = typer.Option(
134134
None,
135135
help="Maximum number of requests to execute at the same time.",
136136
envvar="INFRAHUB_MAX_CONCURRENT_EXECUTION",
137137
),
138138
timeout: int = typer.Option(60, help="Timeout in sec", envvar="INFRAHUB_TIMEOUT"),
139-
variables: list[str] | None = typer.Argument(
139+
variables: Optional[list[str]] = typer.Argument(
140140
None, help="Variables to pass along with the query. Format key=value key=value."
141141
),
142142
) -> None:
@@ -259,7 +259,7 @@ def _run_transform(
259259
@catch_exception(console=console)
260260
def render(
261261
transform_name: str = typer.Argument(default="", help="Name of the Python transformation", show_default=False),
262-
variables: list[str] | None = typer.Argument(
262+
variables: Optional[list[str]] = typer.Argument(
263263
None, help="Variables to pass along with the query. Format key=value key=value."
264264
),
265265
branch: str = typer.Option(None, help="Branch on which to render the transform."),
@@ -309,7 +309,7 @@ def render(
309309
@catch_exception(console=console)
310310
def transform(
311311
transform_name: str = typer.Argument(default="", help="Name of the Python transformation", show_default=False),
312-
variables: list[str] | None = typer.Argument(
312+
variables: Optional[list[str]] = typer.Argument(
313313
None, help="Variables to pass along with the query. Format key=value key=value."
314314
),
315315
branch: str = typer.Option(None, help="Branch on which to run the transformation"),

infrahub_sdk/ctl/generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, Optional
55

66
import typer
77
from rich.console import Console
@@ -23,7 +23,7 @@ async def run(
2323
debug: bool, # noqa: ARG001
2424
list_available: bool,
2525
branch: str | None = None,
26-
variables: list[str] | None = None,
26+
variables: Optional[list[str]] = None,
2727
) -> None:
2828
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
2929

infrahub_sdk/ctl/importer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from asyncio import run as aiorun
44
from pathlib import Path
5+
from typing import Optional
56

67
import typer
78
from rich.console import Console
@@ -26,7 +27,7 @@ def load(
2627
quiet: bool = typer.Option(False, help="No console output"),
2728
_: str = CONFIG_PARAM,
2829
branch: str = typer.Option("main", help="Branch from which to export"), # TODO: Replace main by None
29-
concurrent: int | None = typer.Option(
30+
concurrent: Optional[int] = typer.Option(
3031
None,
3132
help="Maximum number of requests to execute at the same time.",
3233
envvar="INFRAHUB_MAX_CONCURRENT_EXECUTION",

infrahub_sdk/ctl/repository.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4+
from typing import Optional
45

56
import typer
67
import yaml
@@ -68,7 +69,7 @@ async def add(
6869
name: str,
6970
location: str,
7071
description: str = "",
71-
username: str | None = None,
72+
username: Optional[str] = None,
7273
password: str = "",
7374
ref: str = "",
7475
read_only: bool = False,
@@ -115,7 +116,7 @@ async def add(
115116

116117
@app.command()
117118
async def list(
118-
branch: str | None = None,
119+
branch: Optional[str] = None,
119120
debug: bool = False,
120121
_: str = CONFIG_PARAM,
121122
) -> None:

infrahub_sdk/ctl/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import Coroutine
77
from functools import wraps
88
from pathlib import Path
9-
from typing import TYPE_CHECKING, Any, Callable, NoReturn, TypeVar
9+
from typing import TYPE_CHECKING, Any, Callable, NoReturn, Optional, TypeVar
1010

1111
import pendulum
1212
import typer
@@ -145,7 +145,7 @@ def print_graphql_errors(console: Console, errors: list) -> None:
145145
console.print(f"[red]{escape(str(error))}")
146146

147147

148-
def parse_cli_vars(variables: list[str] | None) -> dict[str, str]:
148+
def parse_cli_vars(variables: Optional[list[str]]) -> dict[str, str]:
149149
if not variables:
150150
return {}
151151

infrahub_sdk/ctl/validate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
from pathlib import Path
5+
from typing import Optional
56

67
import typer
78
import ujson
@@ -57,7 +58,7 @@ async def validate_schema(schema: Path, _: str = CONFIG_PARAM) -> None:
5758
@catch_exception(console=console)
5859
def validate_graphql(
5960
query: str,
60-
variables: list[str] | None = typer.Argument(
61+
variables: Optional[list[str]] = typer.Argument(
6162
None, help="Variables to pass along with the query. Format key=value key=value."
6263
),
6364
debug: bool = typer.Option(False, help="Display more troubleshooting information."),

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ max-complexity = 17
248248
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed
249249
]
250250

251+
"infrahub_sdk/ctl/**/*.py" = [
252+
"UP007", # Use `X | Y` for type annotations
253+
]
254+
251255
"infrahub_sdk/client.py" = [
252256
##################################################################################################
253257
# Review and change the below later #

tests/unit/ctl/test_cli.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
requires_python_310 = pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10 or higher")
1111

1212

13-
@requires_python_310
1413
def test_main_app():
1514
result = runner.invoke(app, ["--help"])
1615
assert result.exit_code == 0
@@ -29,29 +28,25 @@ def test_validate_all_groups_have_names():
2928
assert group.name
3029

3130

32-
@requires_python_310
3331
def test_version_command():
3432
result = runner.invoke(app, ["version"])
3533
assert result.exit_code == 0
3634
assert "Python SDK: v" in result.stdout
3735

3836

39-
@requires_python_310
4037
def test_info_command_success(mock_query_infrahub_version, mock_query_infrahub_user):
4138
result = runner.invoke(app, ["info"])
4239
assert result.exit_code == 0
4340
for expected in ["Connection Status", "Python Version", "SDK Version", "Infrahub Version"]:
4441
assert expected in result.stdout, f"'{expected}' not found in info command output"
4542

4643

47-
@requires_python_310
4844
def test_info_command_failure():
4945
result = runner.invoke(app, ["info"])
5046
assert result.exit_code == 0
5147
assert "Connection Error" in result.stdout
5248

5349

54-
@requires_python_310
5550
def test_info_detail_command_success(mock_query_infrahub_version, mock_query_infrahub_user):
5651
result = runner.invoke(app, ["info", "--detail"])
5752
assert result.exit_code == 0
@@ -65,7 +60,6 @@ def test_info_detail_command_success(mock_query_infrahub_version, mock_query_inf
6560
assert expected in result.stdout, f"'{expected}' not found in detailed info command output"
6661

6762

68-
@requires_python_310
6963
def test_info_detail_command_failure():
7064
result = runner.invoke(app, ["info", "--detail"])
7165
assert result.exit_code == 0

tests/unit/ctl/test_repository_app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
333333
tracker="mutation-repository-create",
334334
)
335335

336+
@requires_python_310
337+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
336338
def test_repo_list(self, mock_repositories_list) -> None:
337339
result = runner.invoke(app, ["repository", "list", "--branch", "main"])
338340
assert result.exit_code == 0

0 commit comments

Comments
 (0)