Skip to content

chore: have EncodedId creation always return EncodedId #2040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions gitlab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ class EncodedId(str):
https://docs.gitlab.com/ee/api/index.html#path-parameters
"""

# mypy complains if return type other than the class type. So we ignore issue.
def __new__( # type: ignore
cls, value: Union[str, int, "EncodedId"]
) -> Union[int, "EncodedId"]:
if isinstance(value, (int, EncodedId)):
def __new__(cls, value: Union[str, int, "EncodedId"]) -> "EncodedId":
if isinstance(value, EncodedId):
return value

if not isinstance(value, str):
if not isinstance(value, (int, str)):
raise TypeError(f"Unsupported type received: {type(value)}")
value = urllib.parse.quote(value, safe="")
if isinstance(value, str):
value = urllib.parse.quote(value, safe="")
return super().__new__(cls, value)


Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ def test_init_str(self):
assert "Hello" == obj
assert "Hello" == str(obj)
assert "Hello" == f"{obj}"
assert isinstance(obj, utils.EncodedId)

obj = utils.EncodedId("this/is a/path")
assert "this%2Fis%20a%2Fpath" == str(obj)
assert "this%2Fis%20a%2Fpath" == f"{obj}"
assert isinstance(obj, utils.EncodedId)

def test_init_int(self):
obj = utils.EncodedId(23)
assert 23 == obj
assert "23" == str(obj)
assert "23" == obj
assert "23" == f"{obj}"
assert isinstance(obj, utils.EncodedId)

def test_init_invalid_type_raises(self):
with pytest.raises(TypeError):
Expand Down