Skip to content

Commit 5778b7a

Browse files
committed
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 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.
1 parent 74f3c2e commit 5778b7a

File tree

5 files changed

+43
-42
lines changed

5 files changed

+43
-42
lines changed

git/objects/blob.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

66
from mimetypes import guess_type
7-
from . import base
7+
import sys
88

9+
from . import base
910

10-
try:
11+
if sys.version_info >= (3, 8):
1112
from typing import Literal
12-
except ImportError:
13+
else:
1314
from typing_extensions import Literal
1415

1516
__all__ = ("Blob",)

git/objects/commit.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,57 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6+
from collections import defaultdict
67
import datetime
8+
from io import BytesIO
9+
import logging
10+
import os
711
import re
812
from subprocess import Popen, PIPE
13+
import sys
14+
from time import altzone, daylight, localtime, time, timezone
15+
916
from gitdb import IStream
10-
from git.util import hex_to_bin, Actor, Stats, finalize_process
11-
from git.diff import Diffable
1217
from git.cmd import Git
18+
from git.diff import Diffable
19+
from git.util import hex_to_bin, Actor, Stats, finalize_process
1320

1421
from .tree import Tree
15-
from . import base
1622
from .util import (
1723
Serializable,
1824
TraversableIterableObj,
19-
parse_date,
2025
altz_to_utctz_str,
21-
parse_actor_and_date,
2226
from_timestamp,
27+
parse_actor_and_date,
28+
parse_date,
2329
)
24-
25-
from time import time, daylight, altzone, timezone, localtime
26-
import os
27-
from io import BytesIO
28-
import logging
29-
from collections import defaultdict
30-
30+
from . import base
3131

3232
# typing ------------------------------------------------------------------
3333

3434
from typing import (
3535
Any,
36+
Dict,
3637
IO,
3738
Iterator,
3839
List,
3940
Sequence,
4041
Tuple,
41-
Union,
4242
TYPE_CHECKING,
43+
Union,
4344
cast,
44-
Dict,
4545
)
4646

47-
from git.types import PathLike
48-
49-
try:
47+
if sys.version_info >= (3, 8):
5048
from typing import Literal
51-
except ImportError:
49+
else:
5250
from typing_extensions import Literal
5351

52+
from git.types import PathLike
53+
5454
if TYPE_CHECKING:
55-
from git.repo import Repo
5655
from git.refs import SymbolicReference
56+
from git.repo import Repo
5757

5858
# ------------------------------------------------------------------------
5959

git/objects/submodule/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
sm_section,
4040
)
4141

42-
4342
# typing ----------------------------------------------------------------------
4443

4544
from typing import (
@@ -54,13 +53,13 @@
5453
cast,
5554
)
5655

57-
from git.types import Commit_ish, PathLike, TBD
58-
59-
try:
56+
if sys.version_info >= (3, 8):
6057
from typing import Literal
61-
except ImportError:
58+
else:
6259
from typing_extensions import Literal
6360

61+
from git.types import Commit_ish, PathLike, TBD
62+
6463
if TYPE_CHECKING:
6564
from git.index import IndexFile
6665
from git.objects.commit import Commit

git/objects/tag.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
For lightweight tags, see the :mod:`git.refs.tag` module.
1010
"""
1111

12+
import sys
13+
1214
from . import base
1315
from .util import get_object_type_by_name, parse_actor_and_date
1416
from ..util import hex_to_bin
1517
from ..compat import defenc
1618

1719
from typing import List, TYPE_CHECKING, Union
1820

19-
try:
21+
if sys.version_info >= (3, 8):
2022
from typing import Literal
21-
except ImportError:
23+
else:
2224
from typing_extensions import Literal
2325

2426
if TYPE_CHECKING:

git/objects/tree.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
# This module is part of GitPython and is released under the
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

6-
from git.util import IterableList, join_path
6+
import sys
7+
78
import git.diff as git_diff
8-
from git.util import to_bin_sha
9+
from git.util import IterableList, join_path, to_bin_sha
910

10-
from . import util
11-
from .base import IndexObject, IndexObjUnion
11+
from .base import IndexObjUnion, IndexObject
1212
from .blob import Blob
13-
from .submodule.base import Submodule
14-
1513
from .fun import tree_entries_from_data, tree_to_stream
16-
14+
from .submodule.base import Submodule
15+
from . import util
1716

1817
# typing -------------------------------------------------
1918

@@ -25,22 +24,22 @@
2524
Iterator,
2625
List,
2726
Tuple,
27+
TYPE_CHECKING,
2828
Type,
2929
Union,
3030
cast,
31-
TYPE_CHECKING,
3231
)
3332

34-
from git.types import PathLike
35-
36-
try:
33+
if sys.version_info >= (3, 8):
3734
from typing import Literal
38-
except ImportError:
35+
else:
3936
from typing_extensions import Literal
4037

38+
from git.types import PathLike
39+
4140
if TYPE_CHECKING:
42-
from git.repo import Repo
4341
from io import BytesIO
42+
from git.repo import Repo
4443

4544
TreeCacheTup = Tuple[bytes, int, str]
4645

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy