Skip to content

Commit

Permalink
Merge branch '3.x-line' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Feb 3, 2025
2 parents 9cddff6 + 7a71dfc commit ffedbfe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ Previously-deprecated APIs have been removed, including:
- ``Field.fail``, which was replaced by ``Field.make_error`` in 3.0.0.
- `json_module` class Meta option (deprecated in 3.0.0b3). Use `render_module` instead.

(unreleased)
************
3.26.1 (2025-02-03)
*******************

Bug fixes:

- Typing: Fix type annotations for `class Meta <marshmallow.Schema.Meta>` options (:issue:`2804`).
Thanks :user:`lawrence-law` for reporting.

Other changes:

Expand Down
15 changes: 9 additions & 6 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ class Meta(Schema.Opts):
.. versionremoved:: 4.0.0 Remove ``ordered``.
"""

fields: typing.ClassVar[tuple[Field] | list[Field]]
fields: typing.ClassVar[tuple[str, ...] | list[str]]
"""Fields to include in the (de)serialized result"""
additional: typing.ClassVar[tuple[Field] | list[Field]]
additional: typing.ClassVar[tuple[str, ...] | list[str]]
"""Fields to include in addition to the explicitly declared fields.
`additional <marshmallow.Schema.Meta.additional>` and `fields <marshmallow.Schema.Meta.fields>`
are mutually-exclusive options.
Expand All @@ -364,7 +364,7 @@ class Meta(Schema.Opts):
usually better to define fields as class variables, but you may need to
use this option, e.g., if your fields are Python keywords.
"""
exclude: typing.ClassVar[tuple[Field] | list[Field]]
exclude: typing.ClassVar[tuple[str, ...] | list[str]]
"""Fields to exclude in the serialized result.
Nested fields can be represented with dot delimiters.
"""
Expand All @@ -376,15 +376,18 @@ class Meta(Schema.Opts):
"""Default format for `DateTime <marshmallow.fields.DateTime>` fields."""
timeformat: typing.ClassVar[str]
"""Default format for `Time <marshmallow.fields.Time>` fields."""
render_module: typing.ClassVar[types.RenderModule]

# FIXME: Use a more constrained type here.
# ClassVar[RenderModule] doesn't work.
render_module: typing.Any
""" Module to use for `loads <marshmallow.Schema.loads>` and `dumps <marshmallow.Schema.dumps>`.
Defaults to `json` from the standard library.
"""
index_errors: typing.ClassVar[bool]
"""If `True`, errors dictionaries will include the index of invalid items in a collection."""
load_only: typing.ClassVar[tuple[Field] | list[Field]]
load_only: typing.ClassVar[tuple[str, ...] | list[str]]
"""Fields to exclude from serialized results"""
dump_only: typing.ClassVar[tuple[Field] | list[Field]]
dump_only: typing.ClassVar[tuple[str, ...] | list[str]]
"""Fields to exclude from serialized results"""
unknown: typing.ClassVar[types.UnknownOption]
"""Whether to exclude, include, or raise an error for unknown fields in the data.
Expand Down
29 changes: 29 additions & 0 deletions tests/mypy_test_cases/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json

from marshmallow import EXCLUDE, Schema
from marshmallow.fields import Integer, String


# Test that valid `Meta` class attributes pass type checking
class MySchema(Schema):
foo = String()
bar = Integer()

class Meta(Schema.Meta):
fields = ("foo", "bar")
additional = ("baz", "qux")
include = {
"foo2": String(),
}
exclude = ("bar", "baz")
many = True
dateformat = "%Y-%m-%d"
datetimeformat = "%Y-%m-%dT%H:%M:%S"
timeformat = "%H:%M:%S"
render_module = json
ordered = False
index_errors = True
load_only = ("foo", "bar")
dump_only = ("baz", "qux")
unknown = EXCLUDE
register = False

0 comments on commit ffedbfe

Please sign in to comment.