Skip to content

Finish initial typing of Index and Submodule #1285

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 73 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
16f0607
Improve typing of config_levels, add assert_never()
Yobmod Jul 5, 2021
9400246
Fix IndexFile forwardref
Yobmod Jul 5, 2021
e4caa80
put typing_extensions.get_types() behind python version guard
Yobmod Jul 5, 2021
0939e38
fix is_config_level for < 3.8
Yobmod Jul 5, 2021
0fc93b5
Rmv is_config_level() and get_args(), not worth the trouble
Yobmod Jul 5, 2021
53f1195
Add Literal_config_levels.__args__
Yobmod Jul 5, 2021
41e9781
Improve BlameEntry.commit typing
Yobmod Jul 5, 2021
23b5d6b
Add types to submodule.util.py
Yobmod Jul 5, 2021
c2317a7
Make bytesIO forwardref
Yobmod Jul 5, 2021
a935134
Add types to submodule.root.py
Yobmod Jul 5, 2021
3ce319f
Add types to submodule.update()
Yobmod Jul 6, 2021
6471018
Improve types of @unbare_repo and @git_working_dir decorators
Yobmod Jul 6, 2021
fb09bfa
Improve types of diff.py
Yobmod Jul 6, 2021
2a6a2e2
Improve types of diff.py
Yobmod Jul 6, 2021
3578355
Add cast(Repo, mrepo) in try block
Yobmod Jul 6, 2021
1bcccd5
Fix Literal Typeguards
Yobmod Jul 6, 2021
278a371
Fix for mrepo
Yobmod Jul 6, 2021
deafa6a
Fix for mrepo2
Yobmod Jul 6, 2021
e6f340c
Rmv runtime_checkable < py3.8
Yobmod Jul 6, 2021
ed58e2f
Rmv runtime_checkable < py3.8 pt2
Yobmod Jul 6, 2021
eecf148
Rmv root.py types
Yobmod Jul 6, 2021
a857b97
Merge branch 'gitpython-developers:main' into main
Yobmod Jul 6, 2021
b78cca1
Rmv base.py types
Yobmod Jul 6, 2021
8ef1adb
Merge branch 'main' of https://github.com/Yobmod/gitpython
Yobmod Jul 6, 2021
6aebb73
Rmv submodule types
Yobmod Jul 6, 2021
c0ab23e
Rmv submodule types2
Yobmod Jul 6, 2021
8d2a770
Rmv diff typeguard
Yobmod Jul 6, 2021
1fd9e8c
Re-add submodule.util.py types
Yobmod Jul 6, 2021
1eceb89
Fix submodule.util.py types
Yobmod Jul 6, 2021
215abfd
Readd typeguard to Diff.py
Yobmod Jul 6, 2021
94c2ae4
Readd submodule.base.py types
Yobmod Jul 6, 2021
06eca0b
Make subodule a forward ref in Index.base
Yobmod Jul 6, 2021
33ffd0b
Make subodule a forward ref in Index.base2
Yobmod Jul 6, 2021
af7cee5
Make Repo a forward ref in Submodule.base
Yobmod Jul 6, 2021
f372187
Make subodule a forward ref in Index.base3
Yobmod Jul 6, 2021
de36cb6
UnMake subodule a forward ref in Index.base
Yobmod Jul 6, 2021
3cc0edc
UnMake subodule a forward ref in Index.base2
Yobmod Jul 6, 2021
28bde39
Type index _items_to_rela_paths()
Yobmod Jul 6, 2021
1d0e666
Check change_levels (should fail)
Yobmod Jul 6, 2021
e985851
Add 'U' to change_levels (should pass)
Yobmod Jul 6, 2021
873ebe6
Make diff.DiffIndex generic List['Diff']
Yobmod Jul 6, 2021
2e2fe18
Increase mypy strictness (no_implicit_optional & warn_redundant_casts…
Yobmod Jul 8, 2021
5d3818e
Finish initial typing of index folder
Yobmod Jul 8, 2021
9f88796
Mak GitCmdObjectDB a froward ref
Yobmod Jul 8, 2021
1533596
Mak EntryTup a froward ref
Yobmod Jul 8, 2021
4333dcb
Mmmmm
Yobmod Jul 8, 2021
fe5fef9
Mmmmmm
Yobmod Jul 8, 2021
d344abf
Fix traverse_trees_recursive()
Yobmod Jul 8, 2021
dfbc0f4
Fix traverse_trees_recursive() again
Yobmod Jul 8, 2021
c27d2b0
Use Tuple not tuple
Yobmod Jul 8, 2021
4f13b4e
fix base,ours,theirs typing
Yobmod Jul 8, 2021
627deff
Change List to MutableSequence in fun.py _find_by_name()
Yobmod Jul 8, 2021
f271c58
tests TraversableIterableObj typeguard
Yobmod Jul 8, 2021
4802a36
improve TraversableIterableObj typeguard
Yobmod Jul 8, 2021
1faa25f
Rmv typeguard from list_traverse(), was wrong
Yobmod Jul 8, 2021
f4cb7db
Change type of list_traverse() again.
Yobmod Jul 9, 2021
030b1fd
Add list_traverse() to Tree and TraversableIterableObj.
Yobmod Jul 9, 2021
3710e24
Rmv circular import, create Has_id_attribute Protocol instead
Yobmod Jul 9, 2021
5eea891
Fix list_traverse() docstring for Autodoc
Yobmod Jul 9, 2021
9377462
Make has_repo protocol runtime checkable and use in Diffable
Yobmod Jul 9, 2021
3c6deb0
Flatten list_traverse()
Yobmod Jul 9, 2021
a024bdd
Move TraverseNT to global, cos mypy complained on testing
Yobmod Jul 9, 2021
6271660
Rmv submodule.base Repo assert
Yobmod Jul 9, 2021
7c6ae2b
Try to distinguation git.diff module from diff.Diff.diff and diff.Daf…
Yobmod Jul 9, 2021
f916c14
Improve Diffable method typing
Yobmod Jul 9, 2021
e7b685d
Rmv Diffable assert, add Remoote.url property
Yobmod Jul 9, 2021
9bb630f
Add remote.url type
Yobmod Jul 9, 2021
b03af05
Remove defsult_index decorator from diff() and do check within functi…
Yobmod Jul 9, 2021
797e962
Make IndexFile and Diffable .diff() types agree
Yobmod Jul 9, 2021
09053c5
Improve IndexFile_process_diff_args() to get checks to rerun
Yobmod Jul 9, 2021
2ea528e
Fix typing of index.fun.write_tree_from_cache()
Yobmod Jul 9, 2021
e6a27ad
Use TreeCacheTup type alias throughout
Yobmod Jul 9, 2021
94c6652
Make TreeCacheTup forward ref
Yobmod Jul 9, 2021
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
Increase mypy strictness (no_implicit_optional & warn_redundant_casts…
…) and fix errors
  • Loading branch information
Yobmod committed Jul 8, 2021
commit 2e2fe186d09272c3cb6c96467fff362deb90994f
2 changes: 1 addition & 1 deletion git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def execute(self,
except cmd_not_found_exception as err:
raise GitCommandNotFound(redacted_command, err) from err
else:
proc = cast(Popen, proc)
# replace with a typeguard for Popen[bytes]?
proc.stdout = cast(BinaryIO, proc.stdout)
proc.stderr = cast(BinaryIO, proc.stderr)

Expand Down
5 changes: 3 additions & 2 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from typing import (Any, Callable, IO, List, Dict, Sequence,
TYPE_CHECKING, Tuple, Union, cast, overload)

from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never
from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never, is_config_level

if TYPE_CHECKING:
from git.repo.base import Repo
Expand All @@ -54,6 +54,7 @@

CONFIG_LEVELS: ConfigLevels_Tup = ("system", "user", "global", "repository")


# Section pattern to detect conditional includes.
# https://git-scm.com/docs/git-config#_conditional_includes
CONDITIONAL_INCLUDE_REGEXP = re.compile(r"(?<=includeIf )\"(gitdir|gitdir/i|onbranch):(.+)\"")
Expand Down Expand Up @@ -310,7 +311,7 @@ def __init__(self, file_or_files: Union[None, PathLike, 'BytesIO', Sequence[Unio
if read_only:
self._file_or_files = [get_config_path(f)
for f in CONFIG_LEVELS
if f != 'repository']
if is_config_level(f) and f != 'repository']
else:
raise ValueError("No configuration level or configuration files specified")
else:
Expand Down
10 changes: 6 additions & 4 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
_VERSION = 2 # latest version we support
S_IFGITLINK = S_IFGITLINK # a submodule

def __init__(self, repo: 'Repo', file_path: PathLike = None) -> None:
def __init__(self, repo: 'Repo', file_path: Union[PathLike, None] = None) -> None:
"""Initialize this Index instance, optionally from the given ``file_path``.
If no file_path is given, we will be created from the current index file.

Expand Down Expand Up @@ -665,7 +665,7 @@ def _entries_for_paths(self, paths: List[str], path_rewriter: Callable, fprogres
return entries_added

def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], force: bool = True,
fprogress: Callable = lambda *args: None, path_rewriter: Callable = None,
fprogress: Callable = lambda *args: None, path_rewriter: Union[Callable[..., PathLike], None] = None,
write: bool = True, write_extension_data: bool = False) -> List[BaseIndexEntry]:
"""Add files from the working tree, specific blobs or BaseIndexEntries
to the index.
Expand Down Expand Up @@ -970,7 +970,8 @@ def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule'
return out

def commit(self, message: str, parent_commits=None, head: bool = True, author: Union[None, 'Actor'] = None,
committer: Union[None, 'Actor'] = None, author_date: str = None, commit_date: str = None,
committer: Union[None, 'Actor'] = None, author_date: Union[str, None] = None,
commit_date: Union[str, None] = None,
skip_hooks: bool = False) -> Commit:
"""Commit the current default index file, creating a commit object.
For more information on the arguments, see tree.commit.
Expand Down Expand Up @@ -1265,7 +1266,8 @@ def reset(self, commit: Union[Commit, 'Reference', str] = 'HEAD', working_tree:

@ default_index
def diff(self, other: Union[diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = diff.Diffable.Index,
paths: Union[str, List[PathLike], Tuple[PathLike, ...]] = None, create_patch: bool = False, **kwargs: Any
paths: Union[str, List[PathLike], Tuple[PathLike, ...], None] = None,
create_patch: bool = False, **kwargs: Any
) -> diff.DiffIndex:
"""Diff this index against the working copy or a Tree or Commit object

Expand Down
2 changes: 1 addition & 1 deletion git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable):
"message", "parents", "encoding", "gpgsig")
_id_attribute_ = "hexsha"

def __init__(self, repo: 'Repo', binsha: bytes, tree: 'Tree' = None,
def __init__(self, repo: 'Repo', binsha: bytes, tree: Union['Tree', None] = None,
author: Union[Actor, None] = None,
authored_date: Union[int, None] = None,
author_tz_offset: Union[None, float] = None,
Expand Down
6 changes: 3 additions & 3 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, repo: 'Repo', binsha: bytes,
path: Union[PathLike, None] = None,
name: Union[str, None] = None,
parent_commit: Union[Commit_ish, None] = None,
url: str = None,
url: Union[str, None] = None,
branch_path: Union[PathLike, None] = None
) -> None:
"""Initialize this instance with its attributes. We only document the ones
Expand Down Expand Up @@ -339,7 +339,7 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab
@classmethod
def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = None,
branch: Union[str, None] = None, no_checkout: bool = False, depth: Union[int, None] = None,
env: Mapping[str, str] = None, clone_multi_options: Union[Sequence[TBD], None] = None
env: Union[Mapping[str, str], None] = None, clone_multi_options: Union[Sequence[TBD], None] = None
) -> 'Submodule':
"""Add a new submodule to the given repository. This will alter the index
as well as the .gitmodules file, but will not create a new commit.
Expand Down Expand Up @@ -481,7 +481,7 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No

def update(self, recursive: bool = False, init: bool = True, to_latest_revision: bool = False,
progress: Union['UpdateProgress', None] = None, dry_run: bool = False,
force: bool = False, keep_going: bool = False, env: Mapping[str, str] = None,
force: bool = False, keep_going: bool = False, env: Union[Mapping[str, str], None] = None,
clone_multi_options: Union[Sequence[TBD], None] = None):
"""Update the repository of this submodule to point to the checkout
we point at with the binsha of this instance.
Expand Down
1 change: 0 additions & 1 deletion git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def __init__(self, repo: 'Repo', binsha: bytes, mode: int = tree_id << 12, path:
def _get_intermediate_items(cls, index_object: 'Tree',
) -> Union[Tuple['Tree', ...], Tuple[()]]:
if index_object.type == "tree":
index_object = cast('Tree', index_object)
return tuple(index_object._iter_convert_to_object(index_object._cache))
return ()

Expand Down
8 changes: 4 additions & 4 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish
from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish, is_config_level
from typing import (Any, BinaryIO, Callable, Dict,
Iterator, List, Mapping, Optional, Sequence,
TextIO, Tuple, Type, Union,
Expand Down Expand Up @@ -498,7 +498,7 @@ def config_reader(self, config_level: Optional[Lit_config_levels] = None) -> Git
unknown, instead the global path will be used."""
files = None
if config_level is None:
files = [self._get_config_path(f) for f in self.config_level]
files = [self._get_config_path(f) for f in self.config_level if is_config_level(f)]
else:
files = [self._get_config_path(config_level)]
return GitConfigParser(files, read_only=True, repo=self)
Expand Down Expand Up @@ -623,7 +623,7 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool:
raise
return True

def is_valid_object(self, sha: str, object_type: str = None) -> bool:
def is_valid_object(self, sha: str, object_type: Union[str, None] = None) -> bool:
try:
complete_sha = self.odb.partial_to_complete_sha_hex(sha)
object_info = self.odb.info(complete_sha)
Expand Down Expand Up @@ -976,7 +976,7 @@ def blame(self, rev: TBD, file: TBD, incremental: bool = False, **kwargs: Any
return blames

@classmethod
def init(cls, path: PathLike = None, mkdir: bool = True, odbt: Type[GitCmdObjectDB] = GitCmdObjectDB,
def init(cls, path: Union[PathLike, None] = None, mkdir: bool = True, odbt: Type[GitCmdObjectDB] = GitCmdObjectDB,
expand_vars: bool = True, **kwargs: Any) -> 'Repo':
"""Initialize a git repository at the given path if specified

Expand Down
15 changes: 3 additions & 12 deletions git/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,11 @@


def is_config_level(inp: str) -> TypeGuard[Lit_config_levels]:
return inp in ('system', 'global', 'user', 'repository')
# return inp in get_args(Lit_config_level) # only py >= 3.8
return inp in ("system", "user", "global", "repository")


class ConfigLevels_NT(NamedTuple):
"""NamedTuple of allowed CONFIG_LEVELS"""
# works for pylance, but not mypy
system: Literal['system']
user: Literal['user']
global_: Literal['global']
repository: Literal['repository']


ConfigLevels_Tup = Tuple[Lit_config_levels, Lit_config_levels, Lit_config_levels, Lit_config_levels]
# Typing this as specific literals breaks for mypy
ConfigLevels_Tup = Tuple[Literal['system'], Literal['user'], Literal['global'], Literal['repository']]


def assert_never(inp: NoReturn, exc: Union[Exception, None] = None) -> NoReturn:
Expand Down
5 changes: 5 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

# TODO: enable when we've fully annotated everything
# disallow_untyped_defs = True
no_implicit_optional = True
warn_redundant_casts = True
# warn_unused_ignores = True
# warn_unreachable = True
pretty = True

# TODO: remove when 'gitdb' is fully annotated
[mypy-gitdb.*]
Expand Down