Skip to content

Commit 01c12b1

Browse files
committed
type checking improvements
1 parent 6215242 commit 01c12b1

File tree

2 files changed

+22
-8
lines changed
  • key-value
    • key-value-aio/src/key_value/aio/adapters/pydantic
    • key-value-sync/src/key_value/sync/code_gen/adapters/pydantic

2 files changed

+22
-8
lines changed

key-value/key-value-aio/src/key_value/aio/adapters/pydantic/adapter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
class PydanticAdapter(BasePydanticAdapter[T]):
1515
"""Adapter around a KVStore-compliant Store that allows type-safe persistence of Pydantic models."""
1616

17-
# Beartype doesn't like our `type[T] includes a bound on Sequence[...] as the subscript is not checkable at runtime
18-
# For just the next 20 or so lines we are no longer bear bros but have no fear, we will be back soon!
17+
# Beartype cannot handle the parameterized type annotation (type[T]) used here for this generic adapter.
18+
# Using @bear_spray to bypass beartype's runtime checks for this specific method.
1919
@bear_spray
2020
def __init__(
2121
self,
@@ -32,12 +32,19 @@ def __init__(
3232
default_collection: The default collection to use.
3333
raise_on_validation_error: Whether to raise a DeserializationError if validation fails during reads. Otherwise,
3434
calls will return None if validation fails.
35-
"""
3635
36+
Raises:
37+
TypeError: If pydantic_model is a sequence type other than list (e.g., tuple is not supported).
38+
"""
3739
self._key_value = key_value
3840

3941
origin = get_origin(pydantic_model)
40-
self._is_list_model = origin is not None and isinstance(origin, type) and issubclass(origin, Sequence)
42+
self._is_list_model = origin is list
43+
44+
# Validate that if it's a generic type, it must be a list (not tuple, etc.)
45+
if origin is not None and origin is not list:
46+
msg = f"Only list[BaseModel] is supported for sequence types, got {pydantic_model}"
47+
raise TypeError(msg)
4148

4249
self._type_adapter = TypeAdapter[T](pydantic_model)
4350
self._default_collection = default_collection

key-value/key-value-sync/src/key_value/sync/code_gen/adapters/pydantic/adapter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
class PydanticAdapter(BasePydanticAdapter[T]):
1818
"""Adapter around a KVStore-compliant Store that allows type-safe persistence of Pydantic models."""
1919

20-
# Beartype doesn't like our `type[T] includes a bound on Sequence[...] as the subscript is not checkable at runtime
21-
# For just the next 20 or so lines we are no longer bear bros but have no fear, we will be back soon!
20+
# Beartype cannot handle the parameterized type annotation (type[T]) used here for this generic adapter.
21+
# Using @bear_spray to bypass beartype's runtime checks for this specific method.
2222

2323
@bear_spray
2424
def __init__(
@@ -32,12 +32,19 @@ def __init__(
3232
default_collection: The default collection to use.
3333
raise_on_validation_error: Whether to raise a DeserializationError if validation fails during reads. Otherwise,
3434
calls will return None if validation fails.
35-
"""
3635
36+
Raises:
37+
TypeError: If pydantic_model is a sequence type other than list (e.g., tuple is not supported).
38+
"""
3739
self._key_value = key_value
3840

3941
origin = get_origin(pydantic_model)
40-
self._is_list_model = origin is not None and isinstance(origin, type) and issubclass(origin, Sequence)
42+
self._is_list_model = origin is list
43+
44+
# Validate that if it's a generic type, it must be a list (not tuple, etc.)
45+
if origin is not None and origin is not list:
46+
msg = f"Only list[BaseModel] is supported for sequence types, got {pydantic_model}"
47+
raise TypeError(msg)
4148

4249
self._type_adapter = TypeAdapter[T](pydantic_model)
4350
self._default_collection = default_collection

0 commit comments

Comments
 (0)