Skip to content

Commit e84870f

Browse files
committed
fix(types): resolve mypy redundant-cast and unreachable errors
- Replace cast() with explicit type annotations to avoid redundant-cast - Remove unnecessary `not isinstance(pk_value, str)` checks that mypy correctly identifies as unreachable (tuple/dict can't be str subclasses)
1 parent ead4e3d commit e84870f

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

advanced_alchemy/repository/_async.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,21 +709,23 @@ def _build_pk_filter(self, pk_value: PrimaryKeyType) -> ColumnElement[bool]:
709709
# In this case, use id_attribute for backward compatibility
710710
if len(pk_columns) == 0:
711711
id_attr = get_instrumented_attr(self.model_type, self.id_attribute)
712-
return cast("ColumnElement[bool]", id_attr == pk_value)
712+
result: ColumnElement[bool] = id_attr == pk_value
713+
return result
713714

714715
# Single primary key - accept scalar value only
715716
if len(pk_columns) == 1:
716-
if isinstance(pk_value, (tuple, dict)) and not isinstance(pk_value, str):
717+
if isinstance(pk_value, (tuple, dict)):
717718
pk_type_name = type(pk_value).__name__
718719
msg = (
719720
f"Model {self.model_type.__name__} has a single primary key column '{pk_attr_names[0]}'. "
720721
f"Expected a scalar value, got {pk_type_name}: {pk_value!r}"
721722
)
722723
raise ValueError(msg)
723-
return cast("ColumnElement[bool]", pk_columns[0] == pk_value)
724+
single_pk_result: ColumnElement[bool] = pk_columns[0] == pk_value
725+
return single_pk_result
724726

725727
# Composite primary key - require tuple or dict
726-
if isinstance(pk_value, tuple) and not isinstance(pk_value, str):
728+
if isinstance(pk_value, tuple):
727729
# Tuple format: values must match column order
728730
pk_tuple: tuple[Any, ...] = pk_value
729731
if len(pk_tuple) != len(pk_columns):

advanced_alchemy/repository/_sync.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,21 +710,23 @@ def _build_pk_filter(self, pk_value: PrimaryKeyType) -> ColumnElement[bool]:
710710
# In this case, use id_attribute for backward compatibility
711711
if len(pk_columns) == 0:
712712
id_attr = get_instrumented_attr(self.model_type, self.id_attribute)
713-
return cast("ColumnElement[bool]", id_attr == pk_value)
713+
result: ColumnElement[bool] = id_attr == pk_value
714+
return result
714715

715716
# Single primary key - accept scalar value only
716717
if len(pk_columns) == 1:
717-
if isinstance(pk_value, (tuple, dict)) and not isinstance(pk_value, str):
718+
if isinstance(pk_value, (tuple, dict)):
718719
pk_type_name = type(pk_value).__name__
719720
msg = (
720721
f"Model {self.model_type.__name__} has a single primary key column '{pk_attr_names[0]}'. "
721722
f"Expected a scalar value, got {pk_type_name}: {pk_value!r}"
722723
)
723724
raise ValueError(msg)
724-
return cast("ColumnElement[bool]", pk_columns[0] == pk_value)
725+
single_pk_result: ColumnElement[bool] = pk_columns[0] == pk_value
726+
return single_pk_result
725727

726728
# Composite primary key - require tuple or dict
727-
if isinstance(pk_value, tuple) and not isinstance(pk_value, str):
729+
if isinstance(pk_value, tuple):
728730
# Tuple format: values must match column order
729731
pk_tuple: tuple[Any, ...] = pk_value
730732
if len(pk_tuple) != len(pk_columns):

0 commit comments

Comments
 (0)