1616# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
1818import urllib .parse
19- from typing import Any , Callable , Dict , Optional , overload , Union
19+ from typing import Any , Callable , Dict , Optional , Union
2020
2121import requests
2222
@@ -76,19 +76,18 @@ class EncodedId(str):
7676 # URL-encoded value each time.
7777 original_str : str
7878
79- def __new__ (cls , value : Union [str , int , "EncodedId" ]) -> "EncodedId" :
79+ # mypy complains if return type other than the class type. So we ignore issue.
80+ def __new__ ( # type: ignore
81+ cls , value : Union [str , int , "EncodedId" ]
82+ ) -> Union [int , "EncodedId" ]:
8083 # __new__() gets called before __init__()
81- if isinstance (value , int ):
82- value = str (value )
83- # Make sure isinstance() for `EncodedId` comes before check for `str` as
84- # `EncodedId` is an instance of `str` and would pass that check.
85- elif isinstance (value , EncodedId ):
86- # We use the original string value to URL-encode
87- value = value .original_str
88- elif isinstance (value , str ):
84+ if isinstance (value , (int , EncodedId )):
85+ return value
86+
87+ if isinstance (value , str ):
8988 pass
9089 else :
91- raise ValueError (f"Unsupported type received: { type (value )} " )
90+ raise TypeError (f"Unsupported type received: { type (value )} " )
9291 # Set the value our string will return
9392 value = urllib .parse .quote (value , safe = "" )
9493 return super ().__new__ (cls , value )
@@ -111,43 +110,10 @@ def __init__(self, value: Union[int, str]) -> None:
111110 elif isinstance (value , str ):
112111 pass
113112 else :
114- raise ValueError (f"Unsupported type received: { type (value )} " )
113+ raise TypeError (f"Unsupported type received: { type (value )} " )
115114 self .original_str = value
116115 super ().__init__ ()
117116
118117
119- @overload
120- def _url_encode (id : int ) -> int :
121- ...
122-
123-
124- @overload
125- def _url_encode (id : Union [str , EncodedId ]) -> EncodedId :
126- ...
127-
128-
129- def _url_encode (id : Union [int , str , EncodedId ]) -> Union [int , EncodedId ]:
130- """Encode/quote the characters in the string so that they can be used in a path.
131-
132- Reference to documentation on why this is necessary.
133-
134- https://docs.gitlab.com/ee/api/index.html#namespaced-path-encoding
135-
136- If using namespaced API requests, make sure that the NAMESPACE/PROJECT_PATH is
137- URL-encoded. For example, / is represented by %2F
138-
139- https://docs.gitlab.com/ee/api/index.html#path-parameters
140-
141- Path parameters that are required to be URL-encoded must be followed. If not, it
142- doesn’t match an API endpoint and responds with a 404. If there’s something in front
143- of the API (for example, Apache), ensure that it doesn’t decode the URL-encoded path
144- parameters.
145-
146- """
147- if isinstance (id , (int , EncodedId )):
148- return id
149- return EncodedId (id )
150-
151-
152118def remove_none_from_dict (data : Dict [str , Any ]) -> Dict [str , Any ]:
153119 return {k : v for k , v in data .items () if v is not None }
0 commit comments