Skip to content

Commit

Permalink
Fix error message (#2246)
Browse files Browse the repository at this point in the history
close #2245
  • Loading branch information
sloria authored Mar 4, 2024
1 parent cd976d5 commit 8f493e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
---------

3.21.1 (unreleased)
*******************

Bug fixes:

- Fix error message when field is declared as a class and not an instance (:issue:`2245`).
Thanks :user:`travnick` for reporting.

3.21.0 (2024-02-26)
*******************

Expand Down
4 changes: 2 additions & 2 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,9 @@ def _bind_field(self, field_name: str, field_obj: ma_fields.Field) -> None:
# the type checker's perspective.
if isinstance(field_obj, type) and issubclass(field_obj, base.FieldABC):
msg = (
'Field for "{field_name}" must be declared as a '
f'Field for "{field_name}" must be declared as a '
"Field instance, not a class. "
'Did you mean "fields.{field_obj.__name__}()"?'
f'Did you mean "fields.{field_obj.__name__}()"?' # type: ignore
)
raise TypeError(msg) from error
raise error
Expand Down
4 changes: 3 additions & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,9 @@ def test_fields_must_be_declared_as_instances(user):
class BadUserSchema(Schema):
name = fields.String

with pytest.raises(TypeError, match="must be declared as a Field instance"):
with pytest.raises(
TypeError, match='Field for "name" must be declared as a Field instance'
):
BadUserSchema().dump(user)


Expand Down

0 comments on commit 8f493e6

Please sign in to comment.