Skip to content

Replace all wildcard imports with explicit imports #1880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Mar 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1e5a944
Add a script to validate refactored imports
EliahKagan Mar 17, 2024
5b2771d
Add regression tests of the git.util aliasing situation
EliahKagan Mar 18, 2024
fc86a23
Incompletely change git.index imports to test modattrs.py
EliahKagan Feb 24, 2024
4badc19
Fix git.index imports
EliahKagan Mar 18, 2024
1c9bda2
Improve relative order of import groups, and __all__, in git.index
EliahKagan Mar 18, 2024
8b51af3
Improve order of imports and __all__ in git.refs submodules
EliahKagan Mar 18, 2024
b25dd7e
Replace wildcard imports in git.refs
EliahKagan Mar 18, 2024
b32ef65
Improve order of imports and __all__ in git.repo submodules
EliahKagan Mar 18, 2024
0ba06e9
Add git.repo.__all__ and make submodules explicit
EliahKagan Mar 18, 2024
c946906
Improve order of imports and __all__ in git.objects.*
EliahKagan Mar 18, 2024
4e9a2f2
Improve order of imports and __all__ in git.object.submodule.*
EliahKagan Mar 18, 2024
c58be4c
Remove a bit of old commented-out code in git.objects.*
EliahKagan Mar 18, 2024
01c95eb
Don't patch IndexObject and Object into git.objects.submodule.util
EliahKagan Mar 18, 2024
f89d065
Fix git.objects.__all__ and make submodules explicit
EliahKagan Mar 18, 2024
3786307
Make git.objects.util module docstring more specific
EliahKagan Mar 18, 2024
de540b7
Add __all__ and imports in git.objects.submodule
EliahKagan Mar 18, 2024
a05597a
Improve how imports and __all__ are written in git.util
EliahKagan Mar 18, 2024
2053a3d
Remove old commented-out change_type assertions in git.diff
EliahKagan Mar 18, 2024
b8bab43
Remove old commented-out flagKeyLiteral assertions in git.remote
EliahKagan Mar 19, 2024
3d4e476
Improve how second-level imports and __all__ are written
EliahKagan Mar 19, 2024
6318eea
Make F401 "unused import" suppressions more specific
EliahKagan Mar 19, 2024
31bc8a4
Remove unneeded F401 "Unused import" suppressions
EliahKagan Mar 19, 2024
abbe74d
Fix a tiny import sorting nit
EliahKagan Mar 19, 2024
7745250
Replace wildcard imports in top-level git module
EliahKagan Mar 19, 2024
64c9efd
Restore relative order to fix circular import error
EliahKagan Mar 19, 2024
31f89a1
Add the nonpublic indirect submodule aliases back for now
EliahKagan Mar 19, 2024
9bbbcb5
Further improve git.objects.util module docstring
EliahKagan Mar 19, 2024
00f4cbc
Add missing submodule imports in git.objects
EliahKagan Mar 19, 2024
fcc7418
Don't explicitly list direct submodules in __all__
EliahKagan Mar 19, 2024
78055a8
Pick a consistent type for __all__ (for now, list)
EliahKagan Mar 19, 2024
ecdb6aa
Save diff of non-__all__ attributes across import changes
EliahKagan Mar 19, 2024
f705fd6
Remove modattrs.py and related
EliahKagan Mar 19, 2024
4a4d880
Improve test suite import grouping/sorting, __all__ placement
EliahKagan Mar 19, 2024
d524c76
Fix slightly unsorted imports in setup.py
EliahKagan Mar 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve how imports and __all__ are written in git.util
+ Update the detailed comment about the unused import situation.
  • Loading branch information
EliahKagan committed Mar 18, 2024
commit a05597a1aeacec0c18772944208ab2757b0db34b
102 changes: 53 additions & 49 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/

import sys

__all__ = [
"stream_copy",
"join_path",
"to_native_path_linux",
"join_path_native",
"Stats",
"IndexFileSHA1Writer",
"IterableObj",
"IterableList",
"BlockingLockFile",
"LockFile",
"Actor",
"get_user_id",
"assure_directory_exists",
"RemoteProgress",
"CallableRemoteProgress",
"rmtree",
"unbare_repo",
"HIDE_WINDOWS_KNOWN_ERRORS",
]

if sys.platform == "win32":
__all__.append("to_native_path_windows")

from abc import abstractmethod
import contextlib
from functools import wraps
Expand All @@ -16,11 +42,27 @@
import shutil
import stat
import subprocess
import sys
import time
from urllib.parse import urlsplit, urlunsplit
import warnings

# NOTE: Unused imports can be improved now that CI testing has fully resumed. Some of
# these be used indirectly through other GitPython modules, which avoids having to write
# gitdb all the time in their imports. They are not in __all__, at least currently,
# because they could be removed or changed at any time, and so should not be considered
# conceptually public to code outside GitPython. Linters of course do not like it.
from gitdb.util import ( # noqa: F401 # @IgnorePep8
LazyMixin, # @UnusedImport
LockedFD, # @UnusedImport
bin_to_hex, # @UnusedImport
file_contents_ro_filepath, # @UnusedImport
file_contents_ro, # @UnusedImport
hex_to_bin, # @UnusedImport
make_sha,
to_bin_sha, # @UnusedImport
to_hex_sha, # @UnusedImport
)

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

from typing import (
Expand All @@ -37,73 +79,36 @@
Pattern,
Sequence,
Tuple,
TYPE_CHECKING,
TypeVar,
Union,
TYPE_CHECKING,
cast,
overload,
)

if TYPE_CHECKING:
from git.cmd import Git
from git.config import GitConfigParser, SectionConstraint
from git.remote import Remote
from git.repo.base import Repo
from git.config import GitConfigParser, SectionConstraint
from git import Git

from .types import (
from git.types import (
Files_TD,
Has_id_attribute,
HSH_TD,
Literal,
SupportsIndex,
Protocol,
runtime_checkable, # because behind py version guards
PathLike,
HSH_TD,
Protocol,
SupportsIndex,
Total_TD,
Files_TD, # aliases
Has_id_attribute,
runtime_checkable,
)

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

from gitdb.util import ( # noqa: F401 # @IgnorePep8
make_sha,
LockedFD, # @UnusedImport
file_contents_ro, # @UnusedImport
file_contents_ro_filepath, # @UnusedImport
LazyMixin, # @UnusedImport
to_hex_sha, # @UnusedImport
to_bin_sha, # @UnusedImport
bin_to_hex, # @UnusedImport
hex_to_bin, # @UnusedImport
)

T_IterableObj = TypeVar("T_IterableObj", bound=Union["IterableObj", "Has_id_attribute"], covariant=True)
# So IterableList[Head] is subtype of IterableList[IterableObj].

# NOTE: Some of the unused imports might be used/imported by others.
# Handle once test-cases are back up and running.
# Most of these are unused here, but are for use by git-python modules so these
# don't see gitdb all the time. Flake of course doesn't like it.
__all__ = [
"stream_copy",
"join_path",
"to_native_path_linux",
"join_path_native",
"Stats",
"IndexFileSHA1Writer",
"IterableObj",
"IterableList",
"BlockingLockFile",
"LockFile",
"Actor",
"get_user_id",
"assure_directory_exists",
"RemoteProgress",
"CallableRemoteProgress",
"rmtree",
"unbare_repo",
"HIDE_WINDOWS_KNOWN_ERRORS",
]

_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -292,7 +297,6 @@ def to_native_path_linux(path: PathLike) -> str:
path = str(path)
return path.replace("\\", "/")

__all__.append("to_native_path_windows")
to_native_path = to_native_path_windows
else:
# No need for any work on Linux.
Expand Down