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

Commite2dcdb4

Browse files
chore: haveEncodedId creation always returnEncodedId
There is no reason to return an `int` as we can always return a `str`version of the `int`Change `EncodedId` to always return an `EncodedId`. This removes theneed to have `mypy` ignore the error raised.
1 parent37eb8e0 commite2dcdb4

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

‎gitlab/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ class EncodedId(str):
113113
https://docs.gitlab.com/ee/api/index.html#path-parameters
114114
"""
115115

116-
# mypy complains if return type other than the class type. So we ignore issue.
117-
def__new__(# type: ignore
118-
cls,value:Union[str,int,"EncodedId"]
119-
)->Union[int,"EncodedId"]:
120-
ifisinstance(value, (int,EncodedId)):
116+
def__new__(cls,value:Union[str,int,"EncodedId"])->"EncodedId":
117+
ifisinstance(value,EncodedId):
121118
returnvalue
122119

123-
ifnotisinstance(value,str):
120+
ifisinstance(value,int):
121+
return_value=str(value)
122+
elifisinstance(value,str):
123+
return_value=urllib.parse.quote(value,safe="")
124+
else:
124125
raiseTypeError(f"Unsupported type received:{type(value)}")
125-
value=urllib.parse.quote(value,safe="")
126-
returnsuper().__new__(cls,value)
126+
returnsuper().__new__(cls,return_value)
127127

128128

129129
defremove_none_from_dict(data:Dict[str,Any])->Dict[str,Any]:

‎tests/unit/test_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ def test_init_str(self):
4848
assert"Hello"==obj
4949
assert"Hello"==str(obj)
5050
assert"Hello"==f"{obj}"
51+
assertisinstance(obj,utils.EncodedId)
5152

5253
obj=utils.EncodedId("this/is a/path")
5354
assert"this%2Fis%20a%2Fpath"==str(obj)
5455
assert"this%2Fis%20a%2Fpath"==f"{obj}"
56+
assertisinstance(obj,utils.EncodedId)
5557

5658
deftest_init_int(self):
5759
obj=utils.EncodedId(23)
58-
assert23==obj
59-
assert"23"==str(obj)
60+
assert"23"==obj
6061
assert"23"==f"{obj}"
62+
assertisinstance(obj,utils.EncodedId)
6163

6264
deftest_init_invalid_type_raises(self):
6365
withpytest.raises(TypeError):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp