-
Notifications
You must be signed in to change notification settings - Fork 62
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
Allow Optional type annotation #307
Comments
This might not be exactly what you are expecting nor the best solution but for a similar case, in our Harbor API integration, we use the following code to deal with that: class Success(BaseModel):
"""Placeholder model for successful operations.
Some operations do not return any data yet `uplink` requires model objects
to map the response to.
"""
class OptionalJsonStrategy(returns.JsonStrategy):
"""Converts response to object if there is data, else return Success."""
# pylint: disable=too-few-public-methods
def __call__(self, response: _ClientResponse) -> BaseModel: # noqa: D102
if response.content:
content = super().__call__(response)
else:
content = Success()
return content
class optional_json(returns.json): # pylint: disable=invalid-name
"""Specifies that the decorated consumer method should return a JSON object.
See :class:`uplink.returns.json`.
""" # noqa: E501
def _make_strategy(self, converter: Converter) -> OptionalJsonStrategy:
return OptionalJsonStrategy(converter, self._key)
@optional_json
class HarborAPI(Consumer):
"""These APIs provide services for manipulating Harbor project."""
@delete(
"projects/{project_name}/repositories/{repository_name}/"
"artifacts/{reference}"
)
def delete_artifact(
self,
project_name: Path,
repository_name: Path,
reference: Path,
) -> Union[Errors, Success]:
"""Delete the specific artifact.""" Hope it helps. |
For reference, here is the excerpt from Harbor spec: {
"responses": {
"200": {
"$ref": "#/responses/200"
},
"404": {
"$ref": "#/responses/404"
},
"403": {
"$ref": "#/responses/403"
},
"412": {
"$ref": "#/responses/412"
},
"400": {
"$ref": "#/responses/400"
},
"500": {
"$ref": "#/responses/500"
}
},
"parameters": [
{
"$ref": "#/parameters/requestId"
},
{
"$ref": "#/parameters/isResourceName"
},
{
"$ref": "#/parameters/projectNameOrId"
}
],
"tags": [
"project"
],
"operationId": "deleteProject",
"summary": "Delete project by projectID",
"description": "This endpoint is aimed to delete project by project ID."
}
{
"headers": {
"X-Request-Id": {
"type": "string",
"description": "The ID of the corresponding request for the response"
}
},
"description": "Success"
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error when calling Consumer method with return type annotation Optional[...] if response is None:
To Reproduce
Create a Consumer with method that consumes API endpoint that returns None (it is shown as "null" if using curl). Add Optional[SomeType] return type annotation to this method. Call this method to get the error
Expected behavior
Absence of this error. Instead, simply return None
The text was updated successfully, but these errors were encountered: