Skip to content

Commit f8fad69

Browse files
chore: enable mypy check warn_return_any
Update code so that the `warn_return_any` check passes.
1 parent 9833632 commit f8fad69

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

gitlab/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pprint
2222
import textwrap
2323
from types import ModuleType
24-
from typing import Any, Dict, Iterable, Optional, Type, Union
24+
from typing import Any, Dict, Iterable, Optional, Type, TYPE_CHECKING, Union
2525

2626
import gitlab
2727
from gitlab import types as g_types
@@ -245,14 +245,22 @@ def get_id(self) -> Optional[Union[int, str]]:
245245
"""Returns the id of the resource."""
246246
if self._id_attr is None or not hasattr(self, self._id_attr):
247247
return None
248-
return getattr(self, self._id_attr)
248+
id_val = getattr(self, self._id_attr)
249+
if id_val is None:
250+
return None
251+
if TYPE_CHECKING:
252+
assert isinstance(id_val, (int, str))
253+
return id_val
249254

250255
@property
251256
def _repr_value(self) -> Optional[str]:
252257
"""Safely returns the human-readable resource name if present."""
253258
if self._repr_attr is None or not hasattr(self, self._repr_attr):
254259
return None
255-
return getattr(self, self._repr_attr)
260+
repr_val = getattr(self, self._repr_attr)
261+
if TYPE_CHECKING:
262+
assert isinstance(repr_val, str)
263+
return repr_val
256264

257265
@property
258266
def encoded_id(self) -> Optional[Union[int, str]]:

gitlab/cli.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@
2323
import re
2424
import sys
2525
from types import ModuleType
26-
from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, TypeVar, Union
26+
from typing import (
27+
Any,
28+
Callable,
29+
cast,
30+
Dict,
31+
Optional,
32+
Tuple,
33+
Type,
34+
TYPE_CHECKING,
35+
TypeVar,
36+
Union,
37+
)
2738

2839
from requests.structures import CaseInsensitiveDict
2940

@@ -96,8 +107,11 @@ def gitlab_resource_to_cls(
96107
) -> Type[RESTObject]:
97108
classes = CaseInsensitiveDict(namespace.__dict__)
98109
lowercase_class = gitlab_resource.replace("-", "")
99-
100-
return classes[lowercase_class]
110+
class_type = classes[lowercase_class]
111+
if TYPE_CHECKING:
112+
assert isinstance(class_type, type)
113+
assert issubclass(class_type, RESTObject)
114+
return class_type
101115

102116

103117
def cls_to_gitlab_resource(cls: RESTObject) -> str:

gitlab/client.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ def markdown(
439439
data = self.http_post("/markdown", post_data=post_data, **kwargs)
440440
if TYPE_CHECKING:
441441
assert not isinstance(data, requests.Response)
442+
assert isinstance(data["html"], str)
442443
return data["html"]
443444

444445
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError)
@@ -808,7 +809,10 @@ def http_get(
808809
and not raw
809810
):
810811
try:
811-
return result.json()
812+
json_result = result.json()
813+
if TYPE_CHECKING:
814+
assert isinstance(json_result, dict)
815+
return json_result
812816
except Exception as e:
813817
raise gitlab.exceptions.GitlabParsingError(
814818
error_message="Failed to parse the server message"
@@ -989,7 +993,10 @@ def http_post(
989993
)
990994
try:
991995
if result.headers.get("Content-Type", None) == "application/json":
992-
return result.json()
996+
json_result = result.json()
997+
if TYPE_CHECKING:
998+
assert isinstance(json_result, dict)
999+
return json_result
9931000
except Exception as e:
9941001
raise gitlab.exceptions.GitlabParsingError(
9951002
error_message="Failed to parse the server message"
@@ -1037,7 +1044,10 @@ def http_put(
10371044
**kwargs,
10381045
)
10391046
try:
1040-
return result.json()
1047+
json_result = result.json()
1048+
if TYPE_CHECKING:
1049+
assert isinstance(json_result, dict)
1050+
return json_result
10411051
except Exception as e:
10421052
raise gitlab.exceptions.GitlabParsingError(
10431053
error_message="Failed to parse the server message"

gitlab/mixins.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,10 @@ def time_stats(self, **kwargs: Any) -> Dict[str, Any]:
746746
# Use the existing time_stats attribute if it exist, otherwise make an
747747
# API call
748748
if "time_stats" in self.attributes:
749-
return self.attributes["time_stats"]
749+
time_stats = self.attributes["time_stats"]
750+
if TYPE_CHECKING:
751+
assert isinstance(time_stats, dict)
752+
return time_stats
750753

751754
path = f"{self.manager.path}/{self.encoded_id}/time_stats"
752755
result = self.manager.gitlab.http_get(path, **kwargs)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ disallow_untyped_defs = true
1616
no_implicit_reexport = true
1717
strict_equality = true
1818
warn_redundant_casts = true
19+
warn_return_any = true
1920
warn_unused_configs = true
2021
warn_unused_ignores = true
2122

2223
# The following need to have changes made to be able to enable them:
2324
# disallow_any_generics = true
2425
# disallow_untyped_calls = true
2526
# no_implicit_optional = true
26-
# warn_return_any = true
2727

2828
[[tool.mypy.overrides]] # Overrides for currently untyped modules
2929
module = [

tests/unit/objects/test_projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def resp_start_housekeeping():
431431
rsps.add(
432432
method=responses.POST,
433433
url="http://localhost/api/v4/projects/1/housekeeping",
434-
json="0ee4c430667fb7be8461f310",
434+
json={},
435435
content_type="application/json",
436436
status=201,
437437
)

tests/unit/test_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import pytest
2626

27+
import gitlab.base
2728
from gitlab import cli
2829
from gitlab.exceptions import GitlabError
2930

@@ -43,7 +44,7 @@ def test_gitlab_resource_to_cls(gitlab_resource, expected_class):
4344
def _namespace():
4445
pass
4546

46-
ExpectedClass = type(expected_class, (), {})
47+
ExpectedClass = type(expected_class, (gitlab.base.RESTObject,), {})
4748
_namespace.__dict__[expected_class] = ExpectedClass
4849

4950
assert cli.gitlab_resource_to_cls(gitlab_resource, _namespace) == ExpectedClass

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy