Replies: 1 comment
-
|
The issue is that The cleaner approach is to use a Option 1 — TypedDict (recommended for query results) from typing import TypedDict
import asyncpg
class UserRecord(TypedDict):
id: int
username: str
password: str
# Cast the result
row = await conn.fetchrow("SELECT id, username, password FROM users WHERE id = $1", user_id)
user: UserRecord = dict(row) # type: ignore[assignment]
print(user["username"]) # fully typedOption 2 — Keep subclassing but suppress the Pyright error Pyright does not support from typing import Any
import asyncpg
class User(asyncpg.Record):
id: int # declare directly
username: str
password: str
def __getattr__(self, name: str) -> Any:
return self[name]Declaring them outside Option 3 — Use Asyncpg supports passing a custom from typing import Protocol
class UserLike(Protocol):
@property
def id(self) -> int: ...
@property
def username(self) -> str: ...For most cases, Option 1 (TypedDict) is the least friction and works well with type checkers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
so i just type hinted a user record like
but i get the following error from pyright

so do i need to do some additional stuff for this?
Beta Was this translation helpful? Give feedback.
All reactions