Skip to content

Commit

Permalink
Fix types for class Meta options (#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Feb 3, 2025
1 parent 7d15bae commit c836795
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
(unreleased)
************

Bug fixes:

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

Other changes:

- Remove default value for the ``data`` param of `Nested._deserialize <marshmallow.fields.Nested._deserialize>` (:issue:`2802`).
Expand Down
15 changes: 9 additions & 6 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ class Meta(Schema.Opts):
.. versionchanged:: 3.26.0 Deprecate `ordered`. Field order is preserved by default.
"""

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 @@ -396,7 +396,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 @@ -408,17 +408,20 @@ 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.
"""
ordered: typing.ClassVar[bool]
"""If `True`, `Schema.dump <marshmallow.Schema.dump>` is a `collections.OrderedDict`."""
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[str]
"""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 c836795

Please sign in to comment.