Skip to content

Commit 0a79a96

Browse files
refactor: upgrade type annotations to Python 3.10+ syntax
Replace Union[X, Y] with X | Y and add future annotations to version template. Changes: - vcs-versioning/src/vcs_versioning/_pyproject_reading.py: - GivenPyProjectResult: Union[...] -> ... | ... | None - Remove Union import - vcs-versioning/src/vcs_versioning/_types.py: - PathT: Union[PathLike, str] -> PathLike | str - Remove Union import - vcs-versioning/src/vcs_versioning/_dump_version.py: - Template: Union[int, str] -> int | str - Template: Union[str, None] -> str | None - Remove Tuple, Union imports from template - Add 'from __future__ import annotations' to generated .py template Generated version files now include future annotations for backward compatibility with Python 3.10+ pipe operator syntax.
1 parent 48b4596 commit 0a79a96

File tree

4 files changed

+12
-23
lines changed

4 files changed

+12
-23
lines changed

vcs-versioning/src/vcs_versioning/_dump_version.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
".py": """\
2121
# file generated by vcs-versioning
2222
# don't change, don't track in version control
23+
from __future__ import annotations
2324
2425
__all__ = [
2526
"__version__",
@@ -32,11 +33,8 @@
3233
3334
TYPE_CHECKING = False
3435
if TYPE_CHECKING:
35-
from typing import Tuple
36-
from typing import Union
37-
38-
VERSION_TUPLE = Tuple[Union[int, str], ...]
39-
COMMIT_ID = Union[str, None]
36+
VERSION_TUPLE = tuple[int | str, ...]
37+
COMMIT_ID = str | None
4038
else:
4139
VERSION_TUPLE = object
4240
COMMIT_ID = object

vcs-versioning/src/vcs_versioning/_pyproject_reading.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections.abc import Sequence
1010
from dataclasses import dataclass
1111
from pathlib import Path
12-
from typing import TypeAlias, Union
12+
from typing import TypeAlias
1313

1414
if sys.version_info >= (3, 11):
1515
from typing import Self
@@ -27,9 +27,9 @@
2727
DEFAULT_PYPROJECT_PATH = Path("pyproject.toml")
2828

2929
# Testing injection type for configuration reading
30-
GivenPyProjectResult: TypeAlias = Union[
31-
"PyProjectData", InvalidTomlError, FileNotFoundError, None
32-
]
30+
GivenPyProjectResult: TypeAlias = (
31+
"PyProjectData" | InvalidTomlError | FileNotFoundError | None
32+
)
3333

3434

3535
@dataclass

vcs-versioning/src/vcs_versioning/_run_cmd.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,10 @@
77
import textwrap
88
import warnings
99
from collections.abc import Callable, Mapping, Sequence
10-
from typing import TYPE_CHECKING, TypeVar, overload
10+
from typing import TypeVar, overload
1111

1212
from . import _types as _t
1313

14-
if TYPE_CHECKING:
15-
BaseCompletedProcess = subprocess.CompletedProcess[str]
16-
else:
17-
BaseCompletedProcess = subprocess.CompletedProcess
18-
19-
# pick 40 seconds
20-
# unfortunately github CI for windows sometimes needs
21-
# up to 30 seconds to start a command
22-
2314

2415
def _get_timeout(env: Mapping[str, str]) -> int:
2516
"""Get subprocess timeout from override context or environment.
@@ -38,10 +29,10 @@ def _get_timeout(env: Mapping[str, str]) -> int:
3829
T = TypeVar("T")
3930

4031

41-
class CompletedProcess(BaseCompletedProcess):
32+
class CompletedProcess(subprocess.CompletedProcess[str]):
4233
@classmethod
4334
def from_raw(
44-
cls, input: BaseCompletedProcess, strip: bool = True
35+
cls, input: subprocess.CompletedProcess[str], strip: bool = True
4536
) -> CompletedProcess:
4637
return cls(
4738
args=input.args,

vcs-versioning/src/vcs_versioning/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import os
44
from collections.abc import Callable, Sequence
5-
from typing import TYPE_CHECKING, TypeAlias, Union
5+
from typing import TYPE_CHECKING, TypeAlias
66

77
if TYPE_CHECKING:
88
from . import _version_schemes as version
99

10-
PathT: TypeAlias = Union["os.PathLike[str]", str]
10+
PathT: TypeAlias = "os.PathLike[str]" | str
1111

1212
CMD_TYPE: TypeAlias = Sequence[PathT] | str
1313

0 commit comments

Comments
 (0)