-
-
Notifications
You must be signed in to change notification settings - Fork 59
_TableT
is not compatible with TypedDict
#596
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
Comments
_TableT
is not compatible with TypedDict
due to using dict
instead of MutableMapping
_TableT
is not compatible with TypedDict
I've had a look at this and even with the change your propose it's still not accepting the new type defined on the returned response. What I've found to work is to not worry about the type at the import os
import asyncio
from typing import Annotated
from dotenv import load_dotenv
from postgrest.exceptions import APIError
from postgrest._async.client import AsyncPostgrestClient
from pydantic import BaseModel, Field
load_dotenv()
url = os.environ.get("SUPABASE_URL", "")
key = os.environ.get("SUPABASE_KEY", "")
class Country(BaseModel):
id: Annotated[int, Field(alias="id")]
name: Annotated[str, Field(alias="name")]
def Countries(data: list):
return [Country(**x) for x in data]
class CountryResponse(BaseModel):
count: Annotated[int | None, Field(alias="count")]
data: Annotated[list[Country], Field(alias="data")]
async def main():
headers = {"Authorization": f"Bearer {key}"}
async with AsyncPostgrestClient(base_url=f"{url}/rest/v1", headers=headers) as client:
try:
resp = (await client.table("countries").select("*").execute()).model_dump()
response = CountryResponse(**resp)
for row in response.data:
print(f"RESPONSE: {row.id} {row.name}")
except APIError as e:
print(f"ERROR: {e}")
asyncio.run(main()) This might not be ideal for you but this is the current working solution I could come up with. I will investigate more to see how we can coerce the type a bit more to follow your original setup. |
Seems to be impossible to do inline type hint unless PEP 718 Subscriptable functions is accepted. I checked by reverted to the old version of postgrest that I was using, the reason why it worked before is because there was no Reason why I believe coercing the type to my original setup is impossible is because _TableT = Dict[str, Any]
AsyncPostgrestClient.from_(self, table: str) -> AsyncRequestBuilder[_TableT]: ... has defined already defined the output type through the chain of classes, which passes class APIResponse(BaseModel, Generic[_ReturnT]):
data: List[_ReturnT]
"""The data returned by the query.""" and Here are 3 ways that I came up that was close to what
table: AsyncRequestBuilder[MessageRecord] = db.table("messages")
response = await table.select("*").execute()
class DatabaseManager(AsyncClient):
"""Wrapper class for the supabase client."""
@overload
def table(self, table_name: Literal["messages"]) -> AsyncRequestBuilder[MessageRecord]: ...
@overload
def table(self, table_name: str) -> AsyncRequestBuilder[Mapping[str, Any]]: ...
@override
def table(self, table_name: str) -> AsyncRequestBuilder[Mapping[str, Any]]:
"""Override the table method to add the correct type hints via overloads."""
return self.postgrest.from_(table_name)
response = await db.table[MessageRecord]("messages").select("*").execute() |
Bug report
Describe the bug
postgrest-py/postgrest/_async/client.py
Lines 15 to 21 in 576a5b8
_TableT
should be(Edit:MutableMapping[str, Any]
Mapping[str, Any]
) instead ofdict[str, Any]
to allow for users to specify a TypedDict as the output type. This seems to be a new-ish regression because my type checker wasn't complaining 4 months ago (edit: this is because there was no_TableT
in postgrest 0.19.3 )Issues from the two main type checkers explaining why
dict
is not compatible withTypedDict
by designmypy: python/mypy#4976
pyright: microsoft/pyright#6658
To Reproduce
Expected behavior
Using a TypedDict should be accepted
System information
The text was updated successfully, but these errors were encountered: