Skip to content

Commit

Permalink
Use LBYL for imports where EAFP is a mypy type error
Browse files Browse the repository at this point in the history
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 gitpython-developers#1861 and gitpython-developers#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.
  • Loading branch information
EliahKagan committed Mar 14, 2024
1 parent 74f3c2e commit 5778b7a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
7 changes: 4 additions & 3 deletions git/objects/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",)
Expand Down
38 changes: 19 additions & 19 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# ------------------------------------------------------------------------

Expand Down
9 changes: 4 additions & 5 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
sm_section,
)


# typing ----------------------------------------------------------------------

from typing import (
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions git/objects/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
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
from ..compat import defenc

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:
Expand Down
25 changes: 12 additions & 13 deletions git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------------------------------------

Expand All @@ -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]

Expand Down

0 comments on commit 5778b7a

Please sign in to comment.