Skip to content

Commit a1a246f

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 3fa330c commit a1a246f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

gitlab/utils.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,14 @@ 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 not isinstance(value, (int, str)):
124121
raise TypeError(f"Unsupported type received: {type(value)}")
125-
value = urllib.parse.quote(value, safe="")
122+
if isinstance(value, str):
123+
value = urllib.parse.quote(value, safe="")
126124
return super().__new__(cls, value)
127125

128126

tests/unit/test_utils.py

+4-2
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)