From 6a233359ce1ec30386f97d4acdf989f1c3570842 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Fri, 23 Apr 2021 11:37:54 +0200 Subject: fix(mypy): properly describe link between parameter and return types This gives mypy all information that it needs to determine what the return type of a function call is *iff* it knows the argument's type. As a result it can now stop complaining about passing None to str.join() in exc.py. --- git/compat.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'git') diff --git a/git/compat.py b/git/compat.py index c9b83ba4..c4bd2aa3 100644 --- a/git/compat.py +++ b/git/compat.py @@ -18,7 +18,16 @@ from gitdb.utils.encoding import ( # typing -------------------------------------------------------------------- -from typing import IO, Any, AnyStr, Dict, Optional, Type, Union +from typing import ( + Any, + AnyStr, + Dict, + IO, + Optional, + Type, + Union, + overload, +) from git.types import TBD # --------------------------------------------------------------------------- @@ -30,6 +39,12 @@ is_darwin = (os.name == 'darwin') defenc = sys.getfilesystemencoding() +@overload +def safe_decode(s: None) -> None: ... + +@overload +def safe_decode(s: Union[IO[str], AnyStr]) -> str: ... + def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]: """Safely decodes a binary string to unicode""" if isinstance(s, str): @@ -42,6 +57,12 @@ def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]: raise TypeError('Expected bytes or text, but got %r' % (s,)) +@overload +def safe_encode(s: None) -> None: ... + +@overload +def safe_encode(s: AnyStr) -> bytes: ... + def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]: """Safely encodes a binary string to unicode""" if isinstance(s, str): @@ -54,6 +75,12 @@ def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]: raise TypeError('Expected bytes or text, but got %r' % (s,)) +@overload +def win_encode(s: None) -> None: ... + +@overload +def win_encode(s: AnyStr) -> bytes: ... + def win_encode(s: Optional[AnyStr]) -> Optional[bytes]: """Encode unicodes for process arguments on Windows.""" if isinstance(s, str): @@ -65,7 +92,6 @@ def win_encode(s: Optional[AnyStr]) -> Optional[bytes]: return None - def with_metaclass(meta: Type[Any], *bases: Any) -> 'metaclass': # type: ignore ## mypy cannot understand dynamic class creation """copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15""" -- cgit v1.2.1 From c885781858ade2f660818e983915a6dae5672241 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Fri, 23 Apr 2021 12:22:12 +0200 Subject: improvement: teach mypy that Object.type is not always supposed to be None --- git/objects/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'git') diff --git a/git/objects/base.py b/git/objects/base.py index cccb5ec6..59f0e836 100644 --- a/git/objects/base.py +++ b/git/objects/base.py @@ -7,6 +7,7 @@ from git.util import LazyMixin, join_path_native, stream_copy, bin_to_hex import gitdb.typ as dbtyp import os.path as osp +from typing import Optional # noqa: F401 unused import from .util import get_object_type_by_name @@ -24,7 +25,7 @@ class Object(LazyMixin): TYPES = (dbtyp.str_blob_type, dbtyp.str_tree_type, dbtyp.str_commit_type, dbtyp.str_tag_type) __slots__ = ("repo", "binsha", "size") - type = None # to be set by subclass + type = None # type: Optional[str] # to be set by subclass def __init__(self, repo, binsha): """Initialize an object by identifying it by its binary sha. -- cgit v1.2.1 From 8470777b44bed4da87aad9474f88e7f0774252a6 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Fri, 23 Apr 2021 12:24:07 +0200 Subject: improvement: teach mypy how to deal with wildcard-imported objects By telling it where it's imported from in one case and telling it to ignore it in another. --- git/exc.py | 1 + git/objects/__init__.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'git') diff --git a/git/exc.py b/git/exc.py index 358e7ae4..6e646921 100644 --- a/git/exc.py +++ b/git/exc.py @@ -5,6 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php """ Module containing all exceptions thrown throughout the git package, """ +from gitdb.exc import BadName # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 from git.compat import safe_decode diff --git a/git/objects/__init__.py b/git/objects/__init__.py index 23b2416a..897eb98f 100644 --- a/git/objects/__init__.py +++ b/git/objects/__init__.py @@ -16,8 +16,8 @@ from .tag import * from .tree import * # Fix import dependency - add IndexObject to the util module, so that it can be # imported by the submodule.base -smutil.IndexObject = IndexObject -smutil.Object = Object +smutil.IndexObject = IndexObject # type: ignore[attr-defined] +smutil.Object = Object # type: ignore[attr-defined] del(smutil) # must come after submodule was made available -- cgit v1.2.1 From 76ba0924be14d55d01db0506b3e6a930cc72bf0d Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Fri, 23 Apr 2021 12:24:53 +0200 Subject: improvement(mypy): ignore false positives --- git/cmd.py | 2 +- git/config.py | 2 +- git/refs/reference.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'git') diff --git a/git/cmd.py b/git/cmd.py index e38261a0..ac3ca2ec 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -138,7 +138,7 @@ CREATE_NO_WINDOW = 0x08000000 ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards, # see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal -PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP +PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore[attr-defined] if is_win else 0) diff --git a/git/config.py b/git/config.py index aadb0aac..1cb80475 100644 --- a/git/config.py +++ b/git/config.py @@ -216,7 +216,7 @@ def get_config_path(config_level: Literal['system', 'global', 'user', 'repositor raise ValueError("Invalid configuration level: %r" % config_level) -class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): +class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): # type: ignore ## mypy does not understand dynamic class creation # noqa: E501 """Implements specifics required to read git style configuration files. diff --git a/git/refs/reference.py b/git/refs/reference.py index aaa9b63f..9014f555 100644 --- a/git/refs/reference.py +++ b/git/refs/reference.py @@ -103,7 +103,7 @@ class Reference(SymbolicReference, LazyMixin, Iterable): #{ Remote Interface - @property + @property # type: ignore ## mypy cannot deal with properties with an extra decorator (2021-04-21) @require_remote_ref_path def remote_name(self): """ @@ -114,7 +114,7 @@ class Reference(SymbolicReference, LazyMixin, Iterable): # /refs/remotes// return tokens[2] - @property + @property # type: ignore ## mypy cannot deal with properties with an extra decorator (2021-04-21) @require_remote_ref_path def remote_head(self): """:return: Name of the remote head itself, i.e. master. -- cgit v1.2.1