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,23 @@ 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__()
8184 if isinstance (value , int ):
82- value = str (value )
85+ return value
86+
8387 # Make sure isinstance() for `EncodedId` comes before check for `str` as
8488 # `EncodedId` is an instance of `str` and would pass that check.
85- elif isinstance (value , EncodedId ):
89+ if isinstance (value , EncodedId ):
8690 # We use the original string value to URL-encode
8791 value = value .original_str
8892 elif isinstance (value , str ):
8993 pass
9094 else :
91- raise ValueError (f"Unsupported type received: { type (value )} " )
95+ raise TypeError (f"Unsupported type received: { type (value )} " )
9296 # Set the value our string will return
9397 value = urllib .parse .quote (value , safe = "" )
9498 return super ().__new__ (cls , value )
@@ -111,43 +115,10 @@ def __init__(self, value: Union[int, str]) -> None:
111115 elif isinstance (value , str ):
112116 pass
113117 else :
114- raise ValueError (f"Unsupported type received: { type (value )} " )
118+ raise TypeError (f"Unsupported type received: { type (value )} " )
115119 self .original_str = value
116120 super ().__init__ ()
117121
118122
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-
152123def remove_none_from_dict (data : Dict [str , Any ]) -> Dict [str , Any ]:
153124 return {k : v for k , v in data .items () if v is not None }
0 commit comments