Skip to content
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

Remove return types for paginated endpoints #510

Merged
merged 1 commit into from
Aug 9, 2023

Conversation

adamjmcgrath
Copy link
Contributor

@adamjmcgrath adamjmcgrath commented Aug 8, 2023

Changes

Paginated endpoints, like organizations.all_organization_members return a list when include_totals is None or False and a dict when include_totals is True. Currently the return type of these is list[dict[str, Any]] and so static type checkers will fail when the developer sets include_totals=True.

I've removed the return type for these because:

  • The existing return type list[dict[str, Any]] is not particularly useful
  • Using a Union type will be a breaking change for those using the endpoint without pagination
  • Trying to fix this in a non breaking way would require overloads and the typing-extensions package (as 3.7 doesn't support typing.Literal), which to change the type to return dict[str, Any] as well as list[dict[str, Any]] is not justified.

Type support for this SDK is only very basic at the moment. But we will improve it in the future if/when we can get structured API data (like OpenAPI 3) from the Auth0 Management API.

References

fix #509

Testing

# foo.py

from auth0 import management

auth0 = management.Auth0(domain="", token="")
data = auth0.organizations.all_organization_members(id="foo", include_totals=True)
print(data.get("total"))
$ mypy foo.py
Success: no issues found in 1 source file

Checklist

@adamjmcgrath adamjmcgrath requested a review from a team as a code owner August 8, 2023 14:26
@adamjmcgrath adamjmcgrath merged commit 640a1df into master Aug 9, 2023
11 checks passed
@adamjmcgrath adamjmcgrath deleted the fix-pagination-types branch August 9, 2023 09:43
@@ -58,7 +58,7 @@ def list(
per_page: int = 25,
include_totals: bool = True,
name_filter: str | None = None,
) -> List[dict[str, Any]]:
):
Copy link

@kojiromike kojiromike Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use typing.Literal in combination with typing.overload to get dependent types. For example:

from typing import Literal, overload

@overload
def list(..., include_totals: Literal[True], ...) -> dict[str, Any]:
    ...

@overload
def list(..., include_totals: Literal[False], ...) -> list[dict[str, Any]]:
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong return type for the all_organization_members method
3 participants