Skip to content

Commit

Permalink
chore(lint): address pyright warnings where possible (#190)
Browse files Browse the repository at this point in the history
* feat: address `pyright` wwarnings where possible

* feat: run unasyncd

* fix: adjust scope and ignores
  • Loading branch information
cofin authored May 11, 2024
1 parent 2799a80 commit 2c6b284
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: unasyncd
additional_dependencies: ["ruff"]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.4.2"
rev: "v0.4.4"
hooks:
- id: ruff
args: ["--fix"]
Expand Down
2 changes: 1 addition & 1 deletion advanced_alchemy/extensions/litestar/alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def get_database_migration_plugin(app: Litestar) -> SQLAlchemyInitPlugin:
class AlembicCommands(_AlembicCommands):
def __init__(self, app: Litestar) -> None:
self._app = app
self.sqlalchemy_config = get_database_migration_plugin(self._app)._config # noqa: SLF001
self.sqlalchemy_config = get_database_migration_plugin(self._app)._config # noqa: SLF001 # pyright: ignore[reportPrivateUsage]
self.config = self._get_alembic_command_config()
26 changes: 13 additions & 13 deletions advanced_alchemy/repository/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from advanced_alchemy.utils.text import slugify

if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import _CoreSingleExecuteParams
from sqlalchemy.engine.interfaces import _CoreSingleExecuteParams # pyright: ignore[reportPrivateUsage]
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio.scoping import async_scoped_session

Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(
def get_id_attribute_value(
cls,
item: ModelT | type[ModelT],
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> Any:
"""Get value of attribute named as :attr:`id_attribute <AbstractAsyncRepository.id_attribute>` on ``item``.
Expand All @@ -115,7 +115,7 @@ def set_id_attribute_value(
cls,
item_id: Any,
item: ModelT,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Return the ``item`` after the ID is set to the appropriate attribute.
Expand Down Expand Up @@ -206,7 +206,7 @@ async def delete(
item_id: Any,
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Delete instance identified by ``item_id``.
Expand Down Expand Up @@ -237,7 +237,7 @@ async def delete_many(
item_ids: list[Any],
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
chunk_size: int | None = None,
) -> list[ModelT]:
"""Delete instance identified by `item_id`.
Expand Down Expand Up @@ -349,7 +349,7 @@ def _get_base_stmt(
def _get_delete_many_statement(
self,
model_type: type[ModelT],
id_attribute: InstrumentedAttribute,
id_attribute: InstrumentedAttribute[Any],
id_chunk: list[Any],
supports_returning: bool,
statement_type: Literal["delete", "select"] = "delete",
Expand All @@ -361,17 +361,17 @@ def _get_delete_many_statement(
if self._prefer_any:
statement += lambda s: s.where(any_(id_chunk) == id_attribute) # type: ignore[arg-type]
else:
statement += lambda s: s.where(id_attribute.in_(id_chunk))
statement += lambda s: s.where(id_attribute.in_(id_chunk)) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
if supports_returning and statement_type != "select":
statement += lambda s: s.returning(model_type)
statement += lambda s: s.returning(model_type) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return statement

async def get(
self,
item_id: Any,
auto_expunge: bool | None = None,
statement: Select[tuple[ModelT]] | StatementLambdaElement | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Get instance identified by `item_id`.
Expand Down Expand Up @@ -674,7 +674,7 @@ async def update(
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
auto_refresh: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Update instance with the attribute values present on `data`.
Expand Down Expand Up @@ -771,7 +771,7 @@ async def update_many(
def _get_update_many_statement(model_type: type[ModelT], supports_returning: bool) -> StatementLambdaElement:
statement = lambda_stmt(lambda: update(model_type))
if supports_returning:
statement += lambda s: s.returning(model_type)
statement += lambda s: s.returning(model_type) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return statement

async def list_and_count(
Expand Down Expand Up @@ -1051,7 +1051,7 @@ async def upsert_many(
)
for field_name in match_fields:
field = get_instrumented_attr(self.model_type, field_name)
matched_values = [getattr(datum, field_name) for datum in existing_objs if datum is not None]
matched_values = [getattr(datum, field_name) for datum in existing_objs if datum]
if self._prefer_any:
match_filter.append(any_(matched_values) == field) # type: ignore[arg-type]
else:
Expand Down Expand Up @@ -1154,7 +1154,7 @@ def filter_collection_by_kwargs(
"""
with wrap_sqlalchemy_exception():
collection = lambda_stmt(lambda: collection)
collection += lambda s: s.filter_by(**kwargs)
collection += lambda s: s.filter_by(**kwargs) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return collection

@classmethod
Expand Down
26 changes: 13 additions & 13 deletions advanced_alchemy/repository/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from advanced_alchemy.utils.text import slugify

if TYPE_CHECKING:
from sqlalchemy.engine.interfaces import _CoreSingleExecuteParams
from sqlalchemy.engine.interfaces import _CoreSingleExecuteParams # pyright: ignore[reportPrivateUsage]
from sqlalchemy.orm.scoping import scoped_session

from advanced_alchemy.filters import FilterTypes
Expand Down Expand Up @@ -95,7 +95,7 @@ def __init__(
def get_id_attribute_value(
cls,
item: ModelT | type[ModelT],
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> Any:
"""Get value of attribute named as :attr:`id_attribute <AbstractAsyncRepository.id_attribute>` on ``item``.
Expand All @@ -116,7 +116,7 @@ def set_id_attribute_value(
cls,
item_id: Any,
item: ModelT,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Return the ``item`` after the ID is set to the appropriate attribute.
Expand Down Expand Up @@ -207,7 +207,7 @@ def delete(
item_id: Any,
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Delete instance identified by ``item_id``.
Expand Down Expand Up @@ -238,7 +238,7 @@ def delete_many(
item_ids: list[Any],
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
chunk_size: int | None = None,
) -> list[ModelT]:
"""Delete instance identified by `item_id`.
Expand Down Expand Up @@ -350,7 +350,7 @@ def _get_base_stmt(
def _get_delete_many_statement(
self,
model_type: type[ModelT],
id_attribute: InstrumentedAttribute,
id_attribute: InstrumentedAttribute[Any],
id_chunk: list[Any],
supports_returning: bool,
statement_type: Literal["delete", "select"] = "delete",
Expand All @@ -362,17 +362,17 @@ def _get_delete_many_statement(
if self._prefer_any:
statement += lambda s: s.where(any_(id_chunk) == id_attribute) # type: ignore[arg-type]
else:
statement += lambda s: s.where(id_attribute.in_(id_chunk))
statement += lambda s: s.where(id_attribute.in_(id_chunk)) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
if supports_returning and statement_type != "select":
statement += lambda s: s.returning(model_type)
statement += lambda s: s.returning(model_type) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return statement

def get(
self,
item_id: Any,
auto_expunge: bool | None = None,
statement: Select[tuple[ModelT]] | StatementLambdaElement | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Get instance identified by `item_id`.
Expand Down Expand Up @@ -675,7 +675,7 @@ def update(
auto_commit: bool | None = None,
auto_expunge: bool | None = None,
auto_refresh: bool | None = None,
id_attribute: str | InstrumentedAttribute | None = None,
id_attribute: str | InstrumentedAttribute[Any] | None = None,
) -> ModelT:
"""Update instance with the attribute values present on `data`.
Expand Down Expand Up @@ -772,7 +772,7 @@ def update_many(
def _get_update_many_statement(model_type: type[ModelT], supports_returning: bool) -> StatementLambdaElement:
statement = lambda_stmt(lambda: update(model_type))
if supports_returning:
statement += lambda s: s.returning(model_type)
statement += lambda s: s.returning(model_type) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return statement

def list_and_count(
Expand Down Expand Up @@ -1052,7 +1052,7 @@ def upsert_many(
)
for field_name in match_fields:
field = get_instrumented_attr(self.model_type, field_name)
matched_values = [getattr(datum, field_name) for datum in existing_objs if datum is not None]
matched_values = [getattr(datum, field_name) for datum in existing_objs if datum]
if self._prefer_any:
match_filter.append(any_(matched_values) == field) # type: ignore[arg-type]
else:
Expand Down Expand Up @@ -1155,7 +1155,7 @@ def filter_collection_by_kwargs(
"""
with wrap_sqlalchemy_exception():
collection = lambda_stmt(lambda: collection)
collection += lambda s: s.filter_by(**kwargs)
collection += lambda s: s.filter_by(**kwargs) # pyright: ignore[reportUnknownLambdaType,reportUnknownMemberType]
return collection

@classmethod
Expand Down
Loading

0 comments on commit 2c6b284

Please sign in to comment.