Skip to content

Commit 96efd70

Browse files
[pre-commit.ci] pre-commit autoupdate (#668)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.9.9 → v0.11.4](astral-sh/ruff-pre-commit@v0.9.9...v0.11.4) - [github.com/python-jsonschema/check-jsonschema: 0.31.2 → 0.32.1](python-jsonschema/check-jsonschema@0.31.2...0.32.1) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address ruff issues --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Loria <[email protected]>
1 parent 82d2c13 commit 96efd70

File tree

7 files changed

+19
-17
lines changed

7 files changed

+19
-17
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ ci:
22
autoupdate_schedule: monthly
33
repos:
44
- repo: https://github.com/astral-sh/ruff-pre-commit
5-
rev: v0.9.9
5+
rev: v0.11.4
66
hooks:
77
- id: ruff
88
- id: ruff-format
99
- repo: https://github.com/python-jsonschema/check-jsonschema
10-
rev: 0.31.2
10+
rev: 0.32.1
1111
hooks:
1212
- id: check-github-workflows
1313
- id: check-readthedocs

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ docs = [
3434
"sphinx-copybutton==0.5.2",
3535
"sphinx-design==0.6.1",
3636
"sphinx-issues==5.0.0",
37-
"sphinx==8.2.3",
37+
"sphinx==8.2.3; python_version >= '3.11'",
3838
"sphinxext-opengraph==0.10.0",
3939
]
4040
tests = ["pytest<9", "pytest-lazy-fixtures"]

src/marshmallow_sqlalchemy/convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def _get_field_class_for_data_type(
375375
if _is_field(field_or_factory):
376376
field_cls = field_or_factory
377377
else:
378-
field_cls = cast(_FieldClassFactory, field_or_factory)(
378+
field_cls = cast("_FieldClassFactory", field_or_factory)(
379379
self, data_type
380380
)
381381
break
@@ -394,7 +394,7 @@ def _get_field_class_for_data_type(
394394
raise ModelConversionError(
395395
f"Could not find field column of type {types[0]}."
396396
)
397-
return cast(type[fields.Field], field_cls)
397+
return cast("type[fields.Field]", field_cls)
398398

399399
def _get_field_class_for_property(self, prop) -> type[fields.Field]:
400400
field_cls: type[fields.Field]

src/marshmallow_sqlalchemy/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def related_model(self) -> type[DeclarativeMeta]:
6666
raise RuntimeError(
6767
"Cannot access related_model if schema does not have a model."
6868
)
69-
model_attr = getattr(self.model, cast(str, self.attribute or self.name))
69+
model_attr = getattr(self.model, cast("str", self.attribute or self.name))
7070
if hasattr(model_attr, "remote_attr"): # handle association proxies
7171
model_attr = model_attr.remote_attr
7272
return model_attr.property.mapper.class_

src/marshmallow_sqlalchemy/load_instance_mixin.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010

1111
import importlib.metadata
1212
from collections.abc import Iterable, Mapping, Sequence
13-
from typing import Any, Generic, TypeVar, Union, cast
13+
from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, cast
1414

1515
import marshmallow as ma
1616
from sqlalchemy.ext.declarative import DeclarativeMeta
17-
from sqlalchemy.orm import Session
1817
from sqlalchemy.orm.exc import ObjectDeletedError
1918

2019
from .fields import get_primary_keys
2120

21+
if TYPE_CHECKING:
22+
from sqlalchemy.orm import Session
23+
2224
_LoadDataV3 = Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
2325
_LoadDataV4 = Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]
2426
_LoadDataT = TypeVar("_LoadDataT", _LoadDataV3, _LoadDataV4)
@@ -27,8 +29,8 @@
2729

2830
def _cast_data(data):
2931
if int(importlib.metadata.version("marshmallow")[0]) >= 4:
30-
return cast(_LoadDataV4, data)
31-
return cast(_LoadDataV3, data)
32+
return cast("_LoadDataV4", data)
33+
return cast("_LoadDataV3", data)
3234

3335

3436
class LoadInstanceMixin:
@@ -87,12 +89,12 @@ def get_instance(self, data) -> _ModelType | None:
8789
"""
8890
if self.transient:
8991
return None
90-
model = cast(type[_ModelType], self.opts.model)
92+
model = cast("type[_ModelType]", self.opts.model)
9193
props = get_primary_keys(model)
9294
filters = {prop.key: data.get(prop.key) for prop in props}
9395
if None not in filters.values():
9496
try:
95-
return cast(Session, self.session).get(model, filters)
97+
return cast("Session", self.session).get(model, filters)
9698
except ObjectDeletedError:
9799
return None
98100
return None
@@ -114,7 +116,7 @@ def make_instance(self, data, **kwargs) -> _ModelType:
114116
setattr(instance, key, value)
115117
return instance
116118
kwargs, association_attrs = self._split_model_kwargs_association(data)
117-
ModelClass = cast(DeclarativeMeta, self.opts.model)
119+
ModelClass = cast("DeclarativeMeta", self.opts.model)
118120
instance = ModelClass(**kwargs)
119121
for attr, value in association_attrs.items():
120122
setattr(instance, attr, value)
@@ -144,7 +146,7 @@ def load(
144146
raise ValueError("Deserialization requires a session")
145147
self.instance = instance or self.instance
146148
try:
147-
return cast(ma.Schema, super()).load(_cast_data(data), **kwargs)
149+
return cast("ma.Schema", super()).load(_cast_data(data), **kwargs)
148150
finally:
149151
self.instance = None
150152

@@ -159,7 +161,7 @@ def validate(
159161
self._session = session or self._session
160162
if not (self.transient or self.session):
161163
raise ValueError("Validation requires a session")
162-
return cast(ma.Schema, super()).validate(_cast_data(data), **kwargs)
164+
return cast("ma.Schema", super()).validate(_cast_data(data), **kwargs)
163165

164166
def _split_model_kwargs_association(self, data):
165167
"""Split serialized attrs to ensure association proxies are passed separately.

src/marshmallow_sqlalchemy/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def create_field(
4646
if model:
4747
return converter.field_for(model, column_name, **self.field_kwargs)
4848
table = self.table if self.table is not None else schema_opts.table
49-
column = getattr(cast(sa.Table, table).columns, column_name)
49+
column = getattr(cast("sa.Table", table).columns, column_name)
5050
return converter.column2field(column, **self.field_kwargs)
5151

5252
# This field should never be bound to a schema.

tests/test_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def test_field_for_can_override_validators(self, models, session):
388388
models.Student, "full_name", validate=[validate.Length(max=20)]
389389
)
390390
assert len(field.validators) == 1
391-
validator = cast(validate.Length, field.validators[0])
391+
validator = cast("validate.Length", field.validators[0])
392392
assert validator.max == 20
393393

394394
field = field_for(models.Student, "full_name", validate=[])

0 commit comments

Comments
 (0)