From 3cd7dd33754d9404c9b6ff13d3c8b9ff3d86d1ee Mon Sep 17 00:00:00 2001 From: Chen Chongchen Date: Fri, 25 Jul 2025 03:30:15 +0800 Subject: [PATCH] fix: to_pydantic typing (#2517) currently, to_pydantic will always return LanceModel. If type checking is enabled in my project. I have to use `cast(data, List[RealModelType])` to solve type error. This PR uses generic to solve this problem. --- python/python/lancedb/query.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/python/python/lancedb/query.py b/python/python/lancedb/query.py index 32af99c3..ab8aee18 100644 --- a/python/python/lancedb/query.py +++ b/python/python/lancedb/query.py @@ -14,7 +14,7 @@ from typing import ( Literal, Optional, Tuple, - Type, + TypeVar, Union, Any, ) @@ -58,6 +58,8 @@ if TYPE_CHECKING: else: from typing_extensions import Self +T = TypeVar("T", bound="LanceModel") + # Pydantic validation function for vector queries def ensure_vector_query( @@ -746,8 +748,8 @@ class LanceQueryBuilder(ABC): return self.to_arrow(timeout=timeout).to_pylist() def to_pydantic( - self, model: Type[LanceModel], *, timeout: Optional[timedelta] = None - ) -> List[LanceModel]: + self, model: type[T], *, timeout: Optional[timedelta] = None + ) -> list[T]: """Return the table as a list of pydantic models. Parameters