You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi we're utilizing a helper class to parse over the paginated lists as so:
fromtypingimportAny, Callable, ParamSpec, TypeVarfrommerge.clientimportMergefrommerge.coreimportApiErrorfrommerge.resources.hrisimportEmployee, Group_T=ParamSpec("_T")
_R=TypeVar("_R", covariant=True)
classMiddleware:
def__init__(self, merge_client: Merge):
self.merge_client=merge_clientdef_executor(self, func: Callable[_T, _R], *args: _T.args, **kwargs: _T.kwargs) ->list[Any]:
"""Exhausts a paginated API until all data is retrieved"""data= []
cursor=""whilecursorisnotNone:
try:
next_page=func(*args, **kwargs, cursor=cursor) # type: ignore[arg-type]forresultinnext_page.results: # type: ignore[attr-defined]data.append(result)
cursor=next_page.next# type: ignore[attr-defined]exceptExceptionase:
logger.error(
f"Error while fetching users from Merge. Breaking pagination early: {e}"
)
breakreturndatadefget_users(self) ->list[Employee]:
returnself._executor(self.merge_client.hris.employees.list)
Noticing the mypy type ignores, we're having issues with the ParamSpec variable and the return type generic TypeVar covariant. It would be great if all return list return types could subclass from a base Pydantic model as all paginated pydantic models shown are like:
class PaginatedEmployeeList(pydantic.BaseModel):
next: typing.Optional[str]
previous: typing.Optional[str]
results: typing.Optional[typing.List[Employee]]
where the only difference is the "Employee" model which could easily be made a template variable. This way, we can make the return type the base pagination list Pydantic model instead of a generic TypeVar and remove the type ignores.
Thanks:)
The text was updated successfully, but these errors were encountered:
Andrew-Chen-Wang
changed the title
Python type checking
Python type checking return list base model
May 19, 2024
Hi we're utilizing a helper class to parse over the paginated lists as so:
Noticing the mypy type ignores, we're having issues with the ParamSpec variable and the return type generic TypeVar covariant. It would be great if all return list return types could subclass from a base Pydantic model as all paginated pydantic models shown are like:
where the only difference is the "Employee" model which could easily be made a template variable. This way, we can make the return type the base pagination list Pydantic model instead of a generic TypeVar and remove the type ignores.
Thanks:)
The text was updated successfully, but these errors were encountered: