summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYobmod <yobmod@gmail.com>2021-07-08 11:30:16 +0100
committerYobmod <yobmod@gmail.com>2021-07-08 11:30:16 +0100
commit2e2fe186d09272c3cb6c96467fff362deb90994f (patch)
treeb2ffef306dd111d633e1b3fa66464eda09c5f903
parent873ebe61431c50bb39afd5cafff498b3e1879342 (diff)
downloadgitpython-2e2fe186d09272c3cb6c96467fff362deb90994f.tar.gz
Increase mypy strictness (no_implicit_optional & warn_redundant_casts) and fix errors
-rw-r--r--git/cmd.py2
-rw-r--r--git/config.py5
-rw-r--r--git/index/base.py10
-rw-r--r--git/objects/commit.py2
-rw-r--r--git/objects/submodule/base.py6
-rw-r--r--git/objects/tree.py1
-rw-r--r--git/repo/base.py8
-rw-r--r--git/types.py15
-rw-r--r--mypy.ini5
9 files changed, 26 insertions, 28 deletions
diff --git a/git/cmd.py b/git/cmd.py
index 7df85581..dd887a18 100644
--- a/git/cmd.py
+++ b/git/cmd.py
@@ -831,7 +831,7 @@ class Git(LazyMixin):
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)
diff --git a/git/config.py b/git/config.py
index 0ce3e831..19ce1f84 100644
--- a/git/config.py
+++ b/git/config.py
@@ -34,7 +34,7 @@ import configparser as cp
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
@@ -54,6 +54,7 @@ log.addHandler(logging.NullHandler())
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):(.+)\"")
@@ -310,7 +311,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
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:
diff --git a/git/index/base.py b/git/index/base.py
index c6d92526..d6670b2a 100644
--- a/git/index/base.py
+++ b/git/index/base.py
@@ -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.
@@ -665,7 +665,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
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.
@@ -970,7 +970,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
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.
@@ -1265,7 +1266,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
@ 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
diff --git a/git/objects/commit.py b/git/objects/commit.py
index 81978ae8..65a87591 100644
--- a/git/objects/commit.py
+++ b/git/objects/commit.py
@@ -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,
diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py
index 847b4325..5539069c 100644
--- a/git/objects/submodule/base.py
+++ b/git/objects/submodule/base.py
@@ -115,7 +115,7 @@ class Submodule(IndexObject, TraversableIterableObj):
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
@@ -339,7 +339,7 @@ class Submodule(IndexObject, TraversableIterableObj):
@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.
@@ -481,7 +481,7 @@ class Submodule(IndexObject, TraversableIterableObj):
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.
diff --git a/git/objects/tree.py b/git/objects/tree.py
index 2e8d8a79..34fb93dc 100644
--- a/git/objects/tree.py
+++ b/git/objects/tree.py
@@ -216,7 +216,6 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
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 ()
diff --git a/git/repo/base.py b/git/repo/base.py
index a6f91aee..3214b528 100644
--- a/git/repo/base.py
+++ b/git/repo/base.py
@@ -36,7 +36,7 @@ import gitdb
# 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,
@@ -498,7 +498,7 @@ class Repo(object):
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)
@@ -623,7 +623,7 @@ class Repo(object):
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)
@@ -976,7 +976,7 @@ class Repo(object):
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
diff --git a/git/types.py b/git/types.py
index 00f1ae57..f15db3b4 100644
--- a/git/types.py
+++ b/git/types.py
@@ -39,20 +39,11 @@ Lit_config_levels = Literal['system', 'global', 'user', 'repository']
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:
diff --git a/mypy.ini b/mypy.ini
index 8f86a6af..67397d40 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -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.*]