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

Support for TypedDict with row_factory in psqlpy #119

Open
akanksha314e opened this issue Jan 9, 2025 · 2 comments
Open

Support for TypedDict with row_factory in psqlpy #119

akanksha314e opened this issue Jan 9, 2025 · 2 comments

Comments

@akanksha314e
Copy link

Currently, psqlpy supports using the row_factory method for mapping results to Python objects like Pydantic models. However, it does not support mapping to TypedDict, which is a common construct used for type-safe dictionary-like objects in Python.
Steps to reproduce:

  1. Create a TypedDict class:
    from typing import TypedDict

class MyTypedDict(TypedDict):
id: int
name: str
2. Use the row_factory to map results to this TypedDict:
def to_class(class_):
def to_class_inner(row):
return class_(**row)
return to_class_inner

conn_result.row_factory(row_factory=to_class(MyTypedDict))
the code raises a TypeError.

@chandr-andr
Copy link
Member

@akanksha314e Hello!

Thank you for the issue. Could you please specify the database table schema, SQL query, and PSQLPy code?

@chandr-andr
Copy link
Member

@akanksha314e In general, you don't need to use row_factory functionality, just use as_class method on result:

class MyTypedDict(TypedDict):
	id: int
	name: str


typed_result = conn_result.as_class(MyTypedDict)

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

No branches or pull requests

2 participants