Skip to content

Commit e2dcdb4

Browse files
chore: have EncodedId creation always return EncodedId
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 the need to have `mypy` ignore the error raised.
1 parent 37eb8e0 commit e2dcdb4

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-
if isinstance(value, (int, EncodedId)):
116+
def __new__(cls, value: Union[str, int, "EncodedId"]) -> "EncodedId":
117+
if isinstance(value, EncodedId):
121118
return value
122119

123-
if not isinstance(value, str):
120+
if isinstance(value, int):
121+
return_value = str(value)
122+
elif isinstance(value, str):
123+
return_value = urllib.parse.quote(value, safe="")
124+
else:
124125
raise TypeError(f"Unsupported type received: {type(value)}")
125-
value = urllib.parse.quote(value, safe="")
126-
return super().__new__(cls, value)
126+
return super().__new__(cls, return_value)
127127

128128

129129
def remove_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+
assert isinstance(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+
assert isinstance(obj, utils.EncodedId)
5557

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

6264
def test_init_invalid_type_raises(self):
6365
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)