From 24d17b43da16dd11ab37b2cee561d9392c90f32e Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Mon, 1 Aug 2022 00:29:18 -0700 Subject: [PATCH] chore: enable mypy check `disallow_any_generics` --- gitlab/client.py | 8 +++++--- gitlab/mixins.py | 4 ++-- gitlab/utils.py | 8 ++++---- gitlab/v4/cli.py | 22 +++++++++++++++++----- gitlab/v4/objects/artifacts.py | 4 ++-- gitlab/v4/objects/packages.py | 2 +- gitlab/v4/objects/projects.py | 2 +- pyproject.toml | 2 +- 8 files changed, 33 insertions(+), 19 deletions(-) diff --git a/gitlab/client.py b/gitlab/client.py index 97ca63635..da4036d2b 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -277,7 +277,7 @@ def from_config( @classmethod def merge_config( cls, - options: dict, + options: Dict[str, Any], gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None, ) -> "Gitlab": @@ -330,7 +330,9 @@ def merge_config( ) @staticmethod - def _merge_auth(options: dict, config: gitlab.config.GitlabConfigParser) -> Tuple: + def _merge_auth( + options: Dict[str, Any], config: gitlab.config.GitlabConfigParser + ) -> Tuple[Optional[str], Optional[str], Optional[str]]: """ Return a tuple where at most one of 3 token types ever has a value. Since multiple types of tokens may be present in the environment, @@ -822,7 +824,7 @@ def http_get( def http_head( self, path: str, query_data: Optional[Dict[str, Any]] = None, **kwargs: Any - ) -> requests.structures.CaseInsensitiveDict: + ) -> "requests.structures.CaseInsensitiveDict[Any]": """Make a HEAD request to the Gitlab server. Args: diff --git a/gitlab/mixins.py b/gitlab/mixins.py index d58227a47..9b5f9aae9 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -73,7 +73,7 @@ class HeadMixin(_RestManagerBase): @exc.on_http_error(exc.GitlabHeadError) def head( self, id: Optional[Union[str, int]] = None, **kwargs: Any - ) -> requests.structures.CaseInsensitiveDict: + ) -> "requests.structures.CaseInsensitiveDict[Any]": """Retrieve headers from an endpoint. Args: @@ -622,7 +622,7 @@ class DownloadMixin(_RestObjectBase): def download( self, streamed: bool = False, - action: Optional[Callable] = None, + action: Optional[Callable[[bytes], None]] = None, chunk_size: int = 1024, *, iterator: bool = False, diff --git a/gitlab/utils.py b/gitlab/utils.py index f3d97f789..c8ecd1b56 100644 --- a/gitlab/utils.py +++ b/gitlab/utils.py @@ -34,7 +34,7 @@ def __call__(self, chunk: Any) -> None: def response_content( response: requests.Response, streamed: bool, - action: Optional[Callable], + action: Optional[Callable[[bytes], None]], chunk_size: int, *, iterator: bool, @@ -56,11 +56,11 @@ def response_content( def _transform_types( data: Dict[str, Any], - custom_types: dict, + custom_types: Dict[str, Any], *, transform_data: bool, transform_files: Optional[bool] = True, -) -> Tuple[dict, dict]: +) -> Tuple[Dict[str, Any], Dict[str, Any]]: """Copy the data dict with attributes that have custom types and transform them before being sent to the server. @@ -157,7 +157,7 @@ def remove_none_from_dict(data: Dict[str, Any]) -> Dict[str, Any]: def warn( message: str, *, - category: Optional[Type] = None, + category: Optional[Type[Warning]] = None, source: Optional[Any] = None, ) -> None: """This `warnings.warn` wrapper function attempts to show the location causing the diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index 48369f657..fbc11c2b6 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -210,8 +210,16 @@ def do_update(self) -> Dict[str, Any]: return result +# https://github.com/python/typeshed/issues/7539#issuecomment-1076581049 +if TYPE_CHECKING: + _SubparserType = argparse._SubParsersAction[argparse.ArgumentParser] +else: + _SubparserType = Any + + def _populate_sub_parser_by_class( - cls: Type[gitlab.base.RESTObject], sub_parser: argparse._SubParsersAction + cls: Type[gitlab.base.RESTObject], + sub_parser: _SubparserType, ) -> None: mgr_cls_name = f"{cls.__name__}Manager" mgr_cls = getattr(gitlab.v4.objects, mgr_cls_name) @@ -301,9 +309,11 @@ def _populate_sub_parser_by_class( for action_name in cli.custom_actions[name]: # NOTE(jlvillal): If we put a function for the `default` value of # the `get` it will always get called, which will break things. - sub_parser_action = action_parsers.get(action_name) - if sub_parser_action is None: + action_parser = action_parsers.get(action_name) + if action_parser is None: sub_parser_action = sub_parser.add_parser(action_name) + else: + sub_parser_action = action_parser # Get the attributes for URL/path construction if mgr_cls._from_parent_attrs: for x in mgr_cls._from_parent_attrs: @@ -335,9 +345,11 @@ def _populate_sub_parser_by_class( for action_name in cli.custom_actions[name]: # NOTE(jlvillal): If we put a function for the `default` value of # the `get` it will always get called, which will break things. - sub_parser_action = action_parsers.get(action_name) - if sub_parser_action is None: + action_parser = action_parsers.get(action_name) + if action_parser is None: sub_parser_action = sub_parser.add_parser(action_name) + else: + sub_parser_action = action_parser if mgr_cls._from_parent_attrs: for x in mgr_cls._from_parent_attrs: sub_parser_action.add_argument( diff --git a/gitlab/v4/objects/artifacts.py b/gitlab/v4/objects/artifacts.py index b4a4c0e7d..f091867b6 100644 --- a/gitlab/v4/objects/artifacts.py +++ b/gitlab/v4/objects/artifacts.py @@ -75,7 +75,7 @@ def download( ref_name: str, job: str, streamed: bool = False, - action: Optional[Callable] = None, + action: Optional[Callable[[bytes], None]] = None, chunk_size: int = 1024, *, iterator: bool = False, @@ -125,7 +125,7 @@ def raw( artifact_path: str, job: str, streamed: bool = False, - action: Optional[Callable] = None, + action: Optional[Callable[[bytes], None]] = None, chunk_size: int = 1024, *, iterator: bool = False, diff --git a/gitlab/v4/objects/packages.py b/gitlab/v4/objects/packages.py index 50295e040..9ae2fd52a 100644 --- a/gitlab/v4/objects/packages.py +++ b/gitlab/v4/objects/packages.py @@ -103,7 +103,7 @@ def download( package_version: str, file_name: str, streamed: bool = False, - action: Optional[Callable] = None, + action: Optional[Callable[[bytes], None]] = None, chunk_size: int = 1024, *, iterator: bool = False, diff --git a/gitlab/v4/objects/projects.py b/gitlab/v4/objects/projects.py index 00c0d7e57..36e61f65d 100644 --- a/gitlab/v4/objects/projects.py +++ b/gitlab/v4/objects/projects.py @@ -504,7 +504,7 @@ def snapshot( self, wiki: bool = False, streamed: bool = False, - action: Optional[Callable] = None, + action: Optional[Callable[[bytes], None]] = None, chunk_size: int = 1024, *, iterator: bool = False, diff --git a/pyproject.toml b/pyproject.toml index 544543bc8..a69c6c488 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ exclude = "build/.*" # 'strict = true' is equivalent to the following: check_untyped_defs = true +disallow_any_generics = true disallow_incomplete_defs = true disallow_subclassing_any = true disallow_untyped_decorators = true @@ -21,7 +22,6 @@ warn_unused_configs = true warn_unused_ignores = true # The following need to have changes made to be able to enable them: -# disallow_any_generics = true # disallow_untyped_calls = true # no_implicit_optional = true