From 5778b7a01b988e711216fa5541cfdc50c0460476 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Thu, 14 Mar 2024 02:37:50 -0400 Subject: [PATCH] Use LBYL for imports where EAFP is a mypy type error The conditional imports of Literal from either typing or typing_extensions have to be done with if-else on the Python version rather than with try-except, or it is a static type error. This makes that change, checking sys.version_info. (See discussion in #1861 and #1862 for broader context and background on why this logic, before and after this change, is repeated across multiple modules.) This also reorders/regroups imports for consistency in some places, especially where a new import of the sys module (for version_info) would otherwise exacerbate inconsistency. Since the merge commit 0b99041, the number of mypy errors had increased from 5 to 10. This fixes all the new mypy errors, so the count is back to 5. --- git/objects/blob.py | 7 ++++--- git/objects/commit.py | 38 +++++++++++++++++------------------ git/objects/submodule/base.py | 9 ++++----- git/objects/tag.py | 6 ++++-- git/objects/tree.py | 25 +++++++++++------------ 5 files changed, 43 insertions(+), 42 deletions(-) diff --git a/git/objects/blob.py b/git/objects/blob.py index 0a8527407..b49930edf 100644 --- a/git/objects/blob.py +++ b/git/objects/blob.py @@ -4,12 +4,13 @@ # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ from mimetypes import guess_type -from . import base +import sys +from . import base -try: +if sys.version_info >= (3, 8): from typing import Literal -except ImportError: +else: from typing_extensions import Literal __all__ = ("Blob",) diff --git a/git/objects/commit.py b/git/objects/commit.py index f17e3fdf5..473eae8cc 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -3,57 +3,57 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ +from collections import defaultdict import datetime +from io import BytesIO +import logging +import os import re from subprocess import Popen, PIPE +import sys +from time import altzone, daylight, localtime, time, timezone + from gitdb import IStream -from git.util import hex_to_bin, Actor, Stats, finalize_process -from git.diff import Diffable from git.cmd import Git +from git.diff import Diffable +from git.util import hex_to_bin, Actor, Stats, finalize_process from .tree import Tree -from . import base from .util import ( Serializable, TraversableIterableObj, - parse_date, altz_to_utctz_str, - parse_actor_and_date, from_timestamp, + parse_actor_and_date, + parse_date, ) - -from time import time, daylight, altzone, timezone, localtime -import os -from io import BytesIO -import logging -from collections import defaultdict - +from . import base # typing ------------------------------------------------------------------ from typing import ( Any, + Dict, IO, Iterator, List, Sequence, Tuple, - Union, TYPE_CHECKING, + Union, cast, - Dict, ) -from git.types import PathLike - -try: +if sys.version_info >= (3, 8): from typing import Literal -except ImportError: +else: from typing_extensions import Literal +from git.types import PathLike + if TYPE_CHECKING: - from git.repo import Repo from git.refs import SymbolicReference + from git.repo import Repo # ------------------------------------------------------------------------ diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 8c4a7356e..4e5a2a964 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -39,7 +39,6 @@ sm_section, ) - # typing ---------------------------------------------------------------------- from typing import ( @@ -54,13 +53,13 @@ cast, ) -from git.types import Commit_ish, PathLike, TBD - -try: +if sys.version_info >= (3, 8): from typing import Literal -except ImportError: +else: from typing_extensions import Literal +from git.types import Commit_ish, PathLike, TBD + if TYPE_CHECKING: from git.index import IndexFile from git.objects.commit import Commit diff --git a/git/objects/tag.py b/git/objects/tag.py index 83cf4ae18..e7ecfa62b 100644 --- a/git/objects/tag.py +++ b/git/objects/tag.py @@ -9,6 +9,8 @@ For lightweight tags, see the :mod:`git.refs.tag` module. """ +import sys + from . import base from .util import get_object_type_by_name, parse_actor_and_date from ..util import hex_to_bin @@ -16,9 +18,9 @@ from typing import List, TYPE_CHECKING, Union -try: +if sys.version_info >= (3, 8): from typing import Literal -except ImportError: +else: from typing_extensions import Literal if TYPE_CHECKING: diff --git a/git/objects/tree.py b/git/objects/tree.py index 225061bb7..308dd47a0 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -3,17 +3,16 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ -from git.util import IterableList, join_path +import sys + import git.diff as git_diff -from git.util import to_bin_sha +from git.util import IterableList, join_path, to_bin_sha -from . import util -from .base import IndexObject, IndexObjUnion +from .base import IndexObjUnion, IndexObject from .blob import Blob -from .submodule.base import Submodule - from .fun import tree_entries_from_data, tree_to_stream - +from .submodule.base import Submodule +from . import util # typing ------------------------------------------------- @@ -25,22 +24,22 @@ Iterator, List, Tuple, + TYPE_CHECKING, Type, Union, cast, - TYPE_CHECKING, ) -from git.types import PathLike - -try: +if sys.version_info >= (3, 8): from typing import Literal -except ImportError: +else: from typing_extensions import Literal +from git.types import PathLike + if TYPE_CHECKING: - from git.repo import Repo from io import BytesIO + from git.repo import Repo TreeCacheTup = Tuple[bytes, int, str]