From c6d16d01c54d45be20de40dedae4e9471b96c8ab Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Sat, 7 Jun 2025 11:35:08 -0400 Subject: [PATCH 1/2] Start on fixing AutoInterrupt/CatFileContentStream aliases This uses `TypeAlias` from the `typing` module, to make it so the assignment statments introduced in #2037 (to set `Git.AutoInterrupt` and `Git.CatFileContentStream` to nonpublic module-level implementations `_AutoInterrupt` and `_CatFileContentStream`) are treated by `mypy` as type aliases rather than as class variables. For details on the problem this partially fixes, see #2038 and: https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases The fix won't work in this form, however, because it attempts to import `TypeAlias` unconditionally from the standard-library `typing` module, which only gained it in Python 3.10. --- git/cmd.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index a6195880d..7015d376f 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -55,6 +55,7 @@ TYPE_CHECKING, TextIO, Tuple, + TypeAlias, Union, cast, overload, @@ -952,9 +953,9 @@ def check_unsafe_options(cls, options: List[str], unsafe_options: List[str]) -> f"{unsafe_option} is not allowed, use `allow_unsafe_options=True` to allow it." ) - AutoInterrupt = _AutoInterrupt + AutoInterrupt: TypeAlias = _AutoInterrupt - CatFileContentStream = _CatFileContentStream + CatFileContentStream: TypeAlias = _CatFileContentStream def __init__(self, working_dir: Union[None, PathLike] = None) -> None: """Initialize this instance with: From c6c081230f4b96ca9efce4c9e2478396eaf348c9 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Sat, 7 Jun 2025 11:52:36 -0400 Subject: [PATCH 2/2] Import `TypeAlias` from `typing_extensions` where needed The standard library `typing` module introduced `TypeAlias` in Python 3.10. This uses it from `typing_extensions` where neederd, by making three changes: - Change the version lower bound for `typing-extensions` from 3.7.4.3 to 3.10.0.2, since 3.7.4.3 doesn't offer `TypeAlias`. (The reason not to go higher, to major version 4, is that it no longer supports versions of Python lower than 3.9, but we currently support Python 3.7 and Python 3.8.) - Require the `typing-extensions` dependency when using Python versions lower than 3.10, rather than only lower than 3.7 as before. - Conditionally import `TypeAlias` (in the `git.cmd` module) from either `typing` or `type_extensions` depending on the Python version, using a pattern that `mypy` and other type checkers recognize statically. Together with the preceding commit, this fixes #2038. (This is approach (2) described there.) --- git/cmd.py | 6 +++++- requirements.txt | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 7015d376f..6deded04b 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -55,12 +55,16 @@ TYPE_CHECKING, TextIO, Tuple, - TypeAlias, Union, cast, overload, ) +if sys.version_info >= (3, 10): + from typing import TypeAlias +else: + from typing_extensions import TypeAlias + from git.types import Literal, PathLike, TBD if TYPE_CHECKING: diff --git a/requirements.txt b/requirements.txt index 7159416a9..61d8403b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ gitdb>=4.0.1,<5 -typing-extensions>=3.7.4.3;python_version<"3.8" +typing-extensions>=3.10.0.2;python_version<"3.10"