Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8be32a

Browse files
authored
Merge pull request#2157 from python-gitlab/jlvillal/mypy_step_by_step
chore: enable mypy check `warn_return_any`
2 parents1b7cd31 +76ec4b4 commitb8be32a

File tree

7 files changed

+47
-13
lines changed

7 files changed

+47
-13
lines changed

‎gitlab/base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
importpprint
2222
importtextwrap
2323
fromtypesimportModuleType
24-
fromtypingimportAny,Dict,Iterable,Optional,Type,Union
24+
fromtypingimportAny,Dict,Iterable,Optional,Type,TYPE_CHECKING,Union
2525

2626
importgitlab
2727
fromgitlabimporttypesasg_types
@@ -245,14 +245,20 @@ def get_id(self) -> Optional[Union[int, str]]:
245245
"""Returns the id of the resource."""
246246
ifself._id_attrisNoneornothasattr(self,self._id_attr):
247247
returnNone
248-
returngetattr(self,self._id_attr)
248+
id_val=getattr(self,self._id_attr)
249+
ifTYPE_CHECKING:
250+
assertid_valisNoneorisinstance(id_val, (int,str))
251+
returnid_val
249252

250253
@property
251254
def_repr_value(self)->Optional[str]:
252255
"""Safely returns the human-readable resource name if present."""
253256
ifself._repr_attrisNoneornothasattr(self,self._repr_attr):
254257
returnNone
255-
returngetattr(self,self._repr_attr)
258+
repr_val=getattr(self,self._repr_attr)
259+
ifTYPE_CHECKING:
260+
assertisinstance(repr_val,str)
261+
returnrepr_val
256262

257263
@property
258264
defencoded_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
importre
2424
importsys
2525
fromtypesimportModuleType
26-
fromtypingimportAny,Callable,cast,Dict,Optional,Tuple,Type,TypeVar,Union
26+
fromtypingimport (
27+
Any,
28+
Callable,
29+
cast,
30+
Dict,
31+
Optional,
32+
Tuple,
33+
Type,
34+
TYPE_CHECKING,
35+
TypeVar,
36+
Union,
37+
)
2738

2839
fromrequests.structuresimportCaseInsensitiveDict
2940

@@ -113,8 +124,11 @@ def gitlab_resource_to_cls(
113124
)->Type[RESTObject]:
114125
classes=CaseInsensitiveDict(namespace.__dict__)
115126
lowercase_class=gitlab_resource.replace("-","")
116-
117-
returnclasses[lowercase_class]
127+
class_type=classes[lowercase_class]
128+
ifTYPE_CHECKING:
129+
assertisinstance(class_type,type)
130+
assertissubclass(class_type,RESTObject)
131+
returnclass_type
118132

119133

120134
defcls_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
ifTYPE_CHECKING:
441441
assertnotisinstance(data,requests.Response)
442+
assertisinstance(data["html"],str)
442443
returndata["html"]
443444

444445
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError)
@@ -808,7 +809,10 @@ def http_get(
808809
andnotraw
809810
):
810811
try:
811-
returnresult.json()
812+
json_result=result.json()
813+
ifTYPE_CHECKING:
814+
assertisinstance(json_result,dict)
815+
returnjson_result
812816
exceptExceptionase:
813817
raisegitlab.exceptions.GitlabParsingError(
814818
error_message="Failed to parse the server message"
@@ -989,7 +993,10 @@ def http_post(
989993
)
990994
try:
991995
ifresult.headers.get("Content-Type",None)=="application/json":
992-
returnresult.json()
996+
json_result=result.json()
997+
ifTYPE_CHECKING:
998+
assertisinstance(json_result,dict)
999+
returnjson_result
9931000
exceptExceptionase:
9941001
raisegitlab.exceptions.GitlabParsingError(
9951002
error_message="Failed to parse the server message"
@@ -1037,7 +1044,10 @@ def http_put(
10371044
**kwargs,
10381045
)
10391046
try:
1040-
returnresult.json()
1047+
json_result=result.json()
1048+
ifTYPE_CHECKING:
1049+
assertisinstance(json_result,dict)
1050+
returnjson_result
10411051
exceptExceptionase:
10421052
raisegitlab.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
@@ -755,7 +755,10 @@ def time_stats(self, **kwargs: Any) -> Dict[str, Any]:
755755
# Use the existing time_stats attribute if it exist, otherwise make an
756756
# API call
757757
if"time_stats"inself.attributes:
758-
returnself.attributes["time_stats"]
758+
time_stats=self.attributes["time_stats"]
759+
ifTYPE_CHECKING:
760+
assertisinstance(time_stats,dict)
761+
returntime_stats
759762

760763
path=f"{self.manager.path}/{self.encoded_id}/time_stats"
761764
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
importpytest
2626

27+
importgitlab.base
2728
fromgitlabimportcli
2829
fromgitlab.exceptionsimportGitlabError
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
assertcli.gitlab_resource_to_cls(gitlab_resource,_namespace)==ExpectedClass

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp