| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8ba97aa commit 76ec4b4
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ | |||
| 21 | 21 | import pprint | |
| 22 | 22 | import textwrap | |
| 23 | 23 | 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 | ||
| 25 | 25 | ||
| 26 | 26 | import gitlab | |
| 27 | 27 | from gitlab import types as g_types | |
@@ -245,14 +245,20 @@ def get_id(self) -> Optional[Union[int, str]]: | |||
| 245 | 245 | """Returns the id of the resource.""" | |
| 246 | 246 | if self._id_attr is None or not hasattr(self, self._id_attr): | |
| 247 | 247 | return None | |
| 248 | - return getattr(self, self._id_attr) | ||
| 248 | + id_val = getattr(self, self._id_attr) | ||
| 249 | + if TYPE_CHECKING: | ||
| 250 | + assert id_val is None or isinstance(id_val, (int, str)) | ||
| 251 | + return id_val | ||
| 249 | 252 | ||
| 250 | 253 | @property | |
| 251 | 254 | def _repr_value(self) -> Optional[str]: | |
| 252 | 255 | """Safely returns the human-readable resource name if present.""" | |
| 253 | 256 | if self._repr_attr is None or not hasattr(self, self._repr_attr): | |
| 254 | 257 | return None | |
| 255 | - return getattr(self, self._repr_attr) | ||
| 258 | + repr_val = getattr(self, self._repr_attr) | ||
| 259 | + if TYPE_CHECKING: | ||
| 260 | + assert isinstance(repr_val, str) | ||
| 261 | + return repr_val | ||
| 256 | 262 | ||
| 257 | 263 | @property | |
| 258 | 264 | def encoded_id(self) -> Optional[Union[int, str]]: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,18 @@ | |||
| 23 | 23 | import re | |
| 24 | 24 | import sys | |
| 25 | 25 | 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 | + ) | ||
| 27 | 38 | ||
| 28 | 39 | from requests.structures import CaseInsensitiveDict | |
| 29 | 40 | ||
@@ -113,8 +124,11 @@ def gitlab_resource_to_cls( | |||
| 113 | 124 | ) -> Type[RESTObject]: | |
| 114 | 125 | classes = CaseInsensitiveDict(namespace.__dict__) | |
| 115 | 126 | lowercase_class = gitlab_resource.replace("-", "") | |
| 116 | - | ||
| 117 | - return classes[lowercase_class] | ||
| 127 | + class_type = classes[lowercase_class] | ||
| 128 | + if TYPE_CHECKING: | ||
| 129 | + assert isinstance(class_type, type) | ||
| 130 | + assert issubclass(class_type, RESTObject) | ||
| 131 | + return class_type | ||
| 118 | 132 | ||
| 119 | 133 | ||
| 120 | 134 | def cls_to_gitlab_resource(cls: RESTObject) -> str: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -439,6 +439,7 @@ def markdown( | |||
| 439 | 439 | data = self.http_post("/markdown", post_data=post_data, **kwargs) | |
| 440 | 440 | if TYPE_CHECKING: | |
| 441 | 441 | assert not isinstance(data, requests.Response) | |
| 442 | + assert isinstance(data["html"], str) | ||
| 442 | 443 | return data["html"] | |
| 443 | 444 | ||
| 444 | 445 | @gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError) | |
@@ -808,7 +809,10 @@ def http_get( | |||
| 808 | 809 | and not raw | |
| 809 | 810 | ): | |
| 810 | 811 | try: | |
| 811 | - return result.json() | ||
| 812 | + json_result = result.json() | ||
| 813 | + if TYPE_CHECKING: | ||
| 814 | + assert isinstance(json_result, dict) | ||
| 815 | + return json_result | ||
| 812 | 816 | except Exception as e: | |
| 813 | 817 | raise gitlab.exceptions.GitlabParsingError( | |
| 814 | 818 | error_message="Failed to parse the server message" | |
@@ -989,7 +993,10 @@ def http_post( | |||
| 989 | 993 | ) | |
| 990 | 994 | try: | |
| 991 | 995 | 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 | ||
| 993 | 1000 | except Exception as e: | |
| 994 | 1001 | raise gitlab.exceptions.GitlabParsingError( | |
| 995 | 1002 | error_message="Failed to parse the server message" | |
@@ -1037,7 +1044,10 @@ def http_put( | |||
| 1037 | 1044 | **kwargs, | |
| 1038 | 1045 | ) | |
| 1039 | 1046 | try: | |
| 1040 | - return result.json() | ||
| 1047 | + json_result = result.json() | ||
| 1048 | + if TYPE_CHECKING: | ||
| 1049 | + assert isinstance(json_result, dict) | ||
| 1050 | + return json_result | ||
| 1041 | 1051 | except Exception as e: | |
| 1042 | 1052 | raise gitlab.exceptions.GitlabParsingError( | |
| 1043 | 1053 | error_message="Failed to parse the server message" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -755,7 +755,10 @@ def time_stats(self, **kwargs: Any) -> Dict[str, Any]: | |||
| 755 | 755 | # Use the existing time_stats attribute if it exist, otherwise make an | |
| 756 | 756 | # API call | |
| 757 | 757 | if "time_stats" in self.attributes: | |
| 758 | - return self.attributes["time_stats"] | ||
| 758 | + time_stats = self.attributes["time_stats"] | ||
| 759 | + if TYPE_CHECKING: | ||
| 760 | + assert isinstance(time_stats, dict) | ||
| 761 | + return time_stats | ||
| 759 | 762 | ||
| 760 | 763 | path = f"{self.manager.path}/{self.encoded_id}/time_stats" | |
| 761 | 764 | result = self.manager.gitlab.http_get(path, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,14 +16,14 @@ disallow_untyped_defs = true | |||
| 16 | 16 | no_implicit_reexport = true | |
| 17 | 17 | strict_equality = true | |
| 18 | 18 | warn_redundant_casts = true | |
| 19 | + warn_return_any = true | ||
| 19 | 20 | warn_unused_configs = true | |
| 20 | 21 | warn_unused_ignores = true | |
| 21 | 22 | ||
| 22 | 23 | # The following need to have changes made to be able to enable them: | |
| 23 | 24 | # disallow_any_generics = true | |
| 24 | 25 | # disallow_untyped_calls = true | |
| 25 | 26 | # no_implicit_optional = true | |
| 26 | - # warn_return_any = true | ||
| 27 | 27 | ||
| 28 | 28 | [[tool.mypy.overrides]] # Overrides for currently untyped modules | |
| 29 | 29 | module = [ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -431,7 +431,7 @@ def resp_start_housekeeping(): | |||
| 431 | 431 | rsps.add( | |
| 432 | 432 | method=responses.POST, | |
| 433 | 433 | url="http://localhost/api/v4/projects/1/housekeeping", | |
| 434 | - json="0ee4c430667fb7be8461f310", | ||
| 434 | + json={}, | ||
| 435 | 435 | content_type="application/json", | |
| 436 | 436 | status=201, | |
| 437 | 437 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | ||
| 25 | 25 | import pytest | |
| 26 | 26 | ||
| 27 | + import gitlab.base | ||
| 27 | 28 | from gitlab import cli | |
| 28 | 29 | from gitlab.exceptions import GitlabError | |
| 29 | 30 | ||
@@ -43,7 +44,7 @@ def test_gitlab_resource_to_cls(gitlab_resource, expected_class): | |||
| 43 | 44 | def _namespace(): | |
| 44 | 45 | pass | |
| 45 | 46 | ||
| 46 | - ExpectedClass = type(expected_class, (), {}) | ||
| 47 | + ExpectedClass = type(expected_class, (gitlab.base.RESTObject,), {}) | ||
| 47 | 48 | _namespace.__dict__[expected_class] = ExpectedClass | |
| 48 | 49 | ||
| 49 | 50 | assert cli.gitlab_resource_to_cls(gitlab_resource, _namespace) == ExpectedClass | |
| Back | FazBrowse Home | New Git URL |
0 commit comments