Skip to content

Commit bb97936

Browse files
373 add from future import annotations where possible (#375)
* Add `from __future__ import annotations` where possible * Fix broken imports * Fix pydantic model builds Don't use schema store to validate pyproject.toml * Remove `from __future__ import annotations` from typer files to try and avoid performance regression
1 parent 2c58170 commit bb97936

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+317
-110
lines changed

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ repos:
33
rev: v0.23
44
hooks:
55
- id: validate-pyproject
6-
additional_dependencies: ["validate-pyproject-schema-store[all]"]
76
- repo: https://github.com/tox-dev/pyproject-fmt
87
rev: v2.5.0
98
hooks:

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ lint.select = [
108108
]
109109
lint.ignore = [ "D417", "PLR2004", "SIM108" ]
110110
lint.per-file-ignores."tests/**/*.py" = [ "D", "INP", "S101" ]
111+
lint.flake8-type-checking.runtime-evaluated-base-classes = [ "pydantic.BaseModel" ]
111112
lint.pydocstyle.convention = "google"
112113

113114
[tool.codespell]

src/usethis/__main__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""The CLI application for usethis."""
22

3+
from __future__ import annotations
4+
35
from usethis._app import app
46

57
app(prog_name="usethis")

src/usethis/_ci.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import re
24
from pathlib import Path
35

src/usethis/_config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
from collections.abc import Generator
1+
from __future__ import annotations
2+
23
from contextlib import contextmanager
4+
from typing import TYPE_CHECKING
35

46
import typer
57
from pydantic import BaseModel
68

9+
if TYPE_CHECKING:
10+
from collections.abc import Generator
11+
712

813
class UsethisConfig(BaseModel):
914
"""Global-state for command options which affect low level behaviour."""

src/usethis/_console.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import codecs
24
import sys
35

src/usethis/_core/badge.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
from __future__ import annotations
2+
13
import re
24
from pathlib import Path
5+
from typing import TYPE_CHECKING
36

47
import typer
58
from pydantic import BaseModel
6-
from typing_extensions import Self
79

810
from usethis._console import err_print, tick_print, warn_print
911
from usethis._core.readme import add_readme, get_readme_path
10-
from usethis._integrations.pyproject_toml.errors import (
11-
PyprojectTOMLError,
12-
)
12+
from usethis._integrations.pyproject_toml.errors import PyprojectTOMLError
1313
from usethis._integrations.pyproject_toml.name import get_name
1414

15+
if TYPE_CHECKING:
16+
from typing_extensions import Self
17+
1518

1619
class Badge(BaseModel):
1720
markdown: str

src/usethis/_core/browse.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import typer
24

35
from usethis._console import box_print

src/usethis/_core/ci.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from usethis._ci import update_bitbucket_pytest_steps
24
from usethis._console import box_print, info_print
35
from usethis._integrations.bitbucket.config import (

src/usethis/_core/readme.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
from usethis._console import box_print, tick_print

src/usethis/_core/show.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import typer
24

35
from usethis._config import usethis_config

src/usethis/_core/tool.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
4+
from typing import TYPE_CHECKING
25

36
from usethis._ci import (
47
is_bitbucket_used,
@@ -16,7 +19,10 @@
1619
remove_pre_commit_config,
1720
uninstall_pre_commit_hooks,
1821
)
19-
from usethis._integrations.pre_commit.hooks import add_placeholder_hook, get_hook_names
22+
from usethis._integrations.pre_commit.hooks import (
23+
add_placeholder_hook,
24+
get_hook_names,
25+
)
2026
from usethis._integrations.pyproject_toml.valid import ensure_pyproject_validity
2127
from usethis._integrations.pytest.core import add_pytest_dir, remove_pytest_dir
2228
from usethis._integrations.ruff.rules import (
@@ -37,9 +43,11 @@
3743
PytestTool,
3844
RequirementsTxtTool,
3945
RuffTool,
40-
Tool,
4146
)
4247

48+
if TYPE_CHECKING:
49+
from usethis._tool import Tool
50+
4351

4452
def use_codespell(*, remove: bool = False) -> None:
4553
tool = CodespellTool()

src/usethis/_integrations/bitbucket/anchor.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import Literal, TypeAlias
24

35
from pydantic import BaseModel

src/usethis/_integrations/bitbucket/cache.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
from usethis._console import tick_print
26
from usethis._integrations.bitbucket.dump import bitbucket_fancy_dump
37
from usethis._integrations.bitbucket.io_ import (
4-
BitbucketPipelinesYAMLDocument,
58
edit_bitbucket_pipelines_yaml,
69
)
7-
from usethis._integrations.bitbucket.schema import Cache, Definitions
10+
from usethis._integrations.bitbucket.schema import Definitions
811
from usethis._integrations.yaml.update import update_ruamel_yaml_map
912

13+
if TYPE_CHECKING:
14+
from usethis._integrations.bitbucket.io_ import BitbucketPipelinesYAMLDocument
15+
from usethis._integrations.bitbucket.schema import Cache
16+
1017

1118
def get_cache_by_name() -> dict[str, Cache]:
1219
with edit_bitbucket_pipelines_yaml() as doc:

src/usethis/_integrations/bitbucket/config.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from pathlib import Path
22

33
from usethis._console import tick_print
4-
from usethis._integrations.bitbucket.steps import (
5-
add_placeholder_step_in_default,
6-
)
4+
from usethis._integrations.bitbucket.steps import add_placeholder_step_in_default
75

86

97
def add_bitbucket_pipeline_config(report_placeholder: bool = True) -> None:

src/usethis/_integrations/bitbucket/dump.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
from pydantic import BaseModel
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
from usethis._integrations.bitbucket.schema import (
46
PipelinesConfiguration,
57
Step,
68
Step2,
79
StepBase,
810
)
9-
from usethis._integrations.pydantic.dump import ModelRepresentation, fancy_model_dump
11+
from usethis._integrations.pydantic.dump import fancy_model_dump
12+
13+
if TYPE_CHECKING:
14+
from pydantic import BaseModel
15+
16+
from usethis._integrations.pydantic.dump import ModelRepresentation
1017

1118
ORDER_BY_CLS: dict[type[BaseModel], list[str]] = {
1219
PipelinesConfiguration: ["image", "clone", "definitions"],

src/usethis/_integrations/bitbucket/errors.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from usethis.errors import UsethisError
24

35

src/usethis/_integrations/bitbucket/io_.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
from collections.abc import Generator
1+
from __future__ import annotations
2+
23
from contextlib import contextmanager
34
from dataclasses import dataclass
45
from pathlib import Path
6+
from typing import TYPE_CHECKING
57

68
from pydantic import ValidationError
7-
from ruamel.yaml.comments import CommentedMap
89

910
from usethis._console import tick_print
1011
from usethis._integrations.bitbucket.schema import PipelinesConfiguration
11-
from usethis._integrations.yaml.io_ import YAMLLiteral, edit_yaml
12+
from usethis._integrations.yaml.io_ import edit_yaml
13+
14+
if TYPE_CHECKING:
15+
from collections.abc import Generator
16+
17+
from ruamel.yaml.comments import CommentedMap
18+
19+
from usethis._integrations.yaml.io_ import YAMLLiteral
1220

1321

1422
class BitbucketPipelinesYAMLConfigError(Exception):

src/usethis/_integrations/bitbucket/pipeweld.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
from functools import singledispatch
4+
from typing import TYPE_CHECKING
25
from uuid import uuid4
36

47
from typing_extensions import assert_never
@@ -7,7 +10,6 @@
710
from usethis._integrations.bitbucket.dump import bitbucket_fancy_dump
811
from usethis._integrations.bitbucket.errors import UnexpectedImportPipelineError
912
from usethis._integrations.bitbucket.io_ import (
10-
BitbucketPipelinesYAMLDocument,
1113
edit_bitbucket_pipelines_yaml,
1214
)
1315
from usethis._integrations.bitbucket.schema import (
@@ -18,15 +20,22 @@
1820
ParallelSteps,
1921
Pipeline,
2022
Pipelines,
21-
PipelinesConfiguration,
2223
StageItem,
23-
Step,
2424
StepItem,
2525
)
2626
from usethis._integrations.bitbucket.schema_utils import step1tostep
2727
from usethis._integrations.yaml.update import update_ruamel_yaml_map
2828
from usethis._pipeweld.ops import Instruction
2929

30+
if TYPE_CHECKING:
31+
from usethis._integrations.bitbucket.io_ import (
32+
BitbucketPipelinesYAMLDocument,
33+
)
34+
from usethis._integrations.bitbucket.schema import (
35+
PipelinesConfiguration,
36+
Step,
37+
)
38+
3039

3140
def get_pipeweld_step(step: Step) -> str:
3241
if step.name is not None:

src/usethis/_integrations/bitbucket/schema_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from usethis._integrations.bitbucket.schema import Step, Step1
24

35

src/usethis/_integrations/bitbucket/steps.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from functools import singledispatch
24
from pathlib import Path
35
from typing import TYPE_CHECKING
@@ -8,14 +10,11 @@
810

911
import usethis._pipeweld.func
1012
from usethis._console import box_print, tick_print
11-
from usethis._integrations.bitbucket.anchor import ScriptItemAnchor, ScriptItemName
13+
from usethis._integrations.bitbucket.anchor import ScriptItemAnchor
1214
from usethis._integrations.bitbucket.cache import _add_caches_via_doc, remove_cache
1315
from usethis._integrations.bitbucket.dump import bitbucket_fancy_dump
1416
from usethis._integrations.bitbucket.errors import UnexpectedImportPipelineError
15-
from usethis._integrations.bitbucket.io_ import (
16-
BitbucketPipelinesYAMLDocument,
17-
edit_bitbucket_pipelines_yaml,
18-
)
17+
from usethis._integrations.bitbucket.io_ import edit_bitbucket_pipelines_yaml
1918
from usethis._integrations.bitbucket.pipeweld import (
2019
apply_pipeweld_instruction_via_doc,
2120
get_pipeweld_pipeline_from_default,
@@ -29,7 +28,6 @@
2928
ParallelExpanded,
3029
ParallelItem,
3130
ParallelSteps,
32-
Pipeline,
3331
Script,
3432
StageItem,
3533
Step,
@@ -42,6 +40,10 @@
4240
if TYPE_CHECKING:
4341
from ruamel.yaml.anchor import Anchor
4442

43+
from usethis._integrations.bitbucket.anchor import ScriptItemName
44+
from usethis._integrations.bitbucket.io_ import BitbucketPipelinesYAMLDocument
45+
from usethis._integrations.bitbucket.schema import Pipeline
46+
4547
_CACHE_LOOKUP = {
4648
"uv": CachePath("~/.cache/uv"),
4749
"pre-commit": CachePath("~/.cache/pre-commit"),

src/usethis/_integrations/github/errors.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from usethis.errors import UsethisError
24

35

src/usethis/_integrations/github/tags.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import requests
24

35
from usethis._integrations.github.errors import GitHubTagError, NoGitHubTagsFoundError

src/usethis/_integrations/pre_commit/core.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
from usethis._config import usethis_config

src/usethis/_integrations/pre_commit/dump.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
from pydantic import BaseModel
1+
from __future__ import annotations
22

3-
from usethis._integrations.pre_commit.schema import (
4-
JsonSchemaForPreCommitConfigYaml,
5-
)
6-
from usethis._integrations.pydantic.dump import ModelRepresentation, fancy_model_dump
3+
from typing import TYPE_CHECKING
4+
5+
from usethis._integrations.pydantic.dump import fancy_model_dump
6+
7+
if TYPE_CHECKING:
8+
from pydantic import BaseModel
9+
10+
from usethis._integrations.pre_commit.schema import JsonSchemaForPreCommitConfigYaml
11+
from usethis._integrations.pydantic.dump import ModelRepresentation
712

813
ORDER_BY_CLS: dict[type[BaseModel], list[str]] = {}
914

src/usethis/_integrations/pre_commit/errors.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from usethis.errors import UsethisError
24

35

src/usethis/_integrations/pre_commit/hooks.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
from __future__ import annotations
2+
13
from collections import Counter
24
from pathlib import Path
5+
from typing import TYPE_CHECKING
36

47
from usethis._console import box_print, tick_print
58
from usethis._integrations.pre_commit.dump import pre_commit_fancy_dump
69
from usethis._integrations.pre_commit.io_ import edit_pre_commit_config_yaml
710
from usethis._integrations.pre_commit.schema import (
811
HookDefinition,
9-
JsonSchemaForPreCommitConfigYaml,
1012
Language,
1113
LocalRepo,
1214
MetaRepo,
13-
UriRepo,
1415
)
1516
from usethis._integrations.yaml.update import update_ruamel_yaml_map
1617

18+
if TYPE_CHECKING:
19+
from usethis._integrations.pre_commit.schema import (
20+
JsonSchemaForPreCommitConfigYaml,
21+
UriRepo,
22+
)
23+
1724
_HOOK_ORDER = [
1825
"validate-pyproject",
1926
"uv-export",

0 commit comments

Comments
 (0)