Skip to content

Commit c836795

Browse files
authored
Fix types for class Meta options (#2806)
1 parent 7d15bae commit c836795

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Changelog
44
(unreleased)
55
************
66

7+
Bug fixes:
8+
9+
- Typing: Fix type annotations for `class Meta <marshmallow.Schema.Meta>` options (:issue:`2804`).
10+
Thanks :user:`lawrence-law` for reporting.
11+
712
Other changes:
813

914
- Remove default value for the ``data`` param of `Nested._deserialize <marshmallow.fields.Nested._deserialize>` (:issue:`2802`).

src/marshmallow/schema.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ class Meta(Schema.Opts):
384384
.. versionchanged:: 3.26.0 Deprecate `ordered`. Field order is preserved by default.
385385
"""
386386

387-
fields: typing.ClassVar[tuple[Field] | list[Field]]
387+
fields: typing.ClassVar[tuple[str, ...] | list[str]]
388388
"""Fields to include in the (de)serialized result"""
389-
additional: typing.ClassVar[tuple[Field] | list[Field]]
389+
additional: typing.ClassVar[tuple[str, ...] | list[str]]
390390
"""Fields to include in addition to the explicitly declared fields.
391391
`additional <marshmallow.Schema.Meta.additional>` and `fields <marshmallow.Schema.Meta.fields>`
392392
are mutually-exclusive options.
@@ -396,7 +396,7 @@ class Meta(Schema.Opts):
396396
usually better to define fields as class variables, but you may need to
397397
use this option, e.g., if your fields are Python keywords.
398398
"""
399-
exclude: typing.ClassVar[tuple[Field] | list[Field]]
399+
exclude: typing.ClassVar[tuple[str, ...] | list[str]]
400400
"""Fields to exclude in the serialized result.
401401
Nested fields can be represented with dot delimiters.
402402
"""
@@ -408,17 +408,20 @@ class Meta(Schema.Opts):
408408
"""Default format for `DateTime <marshmallow.fields.DateTime>` fields."""
409409
timeformat: typing.ClassVar[str]
410410
"""Default format for `Time <marshmallow.fields.Time>` fields."""
411-
render_module: typing.ClassVar[types.RenderModule]
411+
412+
# FIXME: Use a more constrained type here.
413+
# ClassVar[RenderModule] doesn't work.
414+
render_module: typing.Any
412415
""" Module to use for `loads <marshmallow.Schema.loads>` and `dumps <marshmallow.Schema.dumps>`.
413416
Defaults to `json` from the standard library.
414417
"""
415418
ordered: typing.ClassVar[bool]
416419
"""If `True`, `Schema.dump <marshmallow.Schema.dump>` is a `collections.OrderedDict`."""
417420
index_errors: typing.ClassVar[bool]
418421
"""If `True`, errors dictionaries will include the index of invalid items in a collection."""
419-
load_only: typing.ClassVar[tuple[Field] | list[Field]]
422+
load_only: typing.ClassVar[tuple[str, ...] | list[str]]
420423
"""Fields to exclude from serialized results"""
421-
dump_only: typing.ClassVar[tuple[Field] | list[Field]]
424+
dump_only: typing.ClassVar[tuple[str, ...] | list[str]]
422425
"""Fields to exclude from serialized results"""
423426
unknown: typing.ClassVar[str]
424427
"""Whether to exclude, include, or raise an error for unknown fields in the data.

tests/mypy_test_cases/test_schema.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
from marshmallow import EXCLUDE, Schema
4+
from marshmallow.fields import Integer, String
5+
6+
7+
# Test that valid `Meta` class attributes pass type checking
8+
class MySchema(Schema):
9+
foo = String()
10+
bar = Integer()
11+
12+
class Meta(Schema.Meta):
13+
fields = ("foo", "bar")
14+
additional = ("baz", "qux")
15+
include = {
16+
"foo2": String(),
17+
}
18+
exclude = ("bar", "baz")
19+
many = True
20+
dateformat = "%Y-%m-%d"
21+
datetimeformat = "%Y-%m-%dT%H:%M:%S"
22+
timeformat = "%H:%M:%S"
23+
render_module = json
24+
ordered = False
25+
index_errors = True
26+
load_only = ("foo", "bar")
27+
dump_only = ("baz", "qux")
28+
unknown = EXCLUDE
29+
register = False

0 commit comments

Comments
 (0)