Skip to content

Commit

Permalink
Merge pull request marshmallow-code#1843 from marshmallow-code/revert…
Browse files Browse the repository at this point in the history
…_expose_fields_as_attr

Fix: Don't expose Schema fields by name as class/instance attributes
  • Loading branch information
lafrech authored Jul 6, 2021
2 parents 38b1ddd + 99c0431 commit af9c3d5
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _get_fields(attrs, ordered=False):
"""Get fields from a class. If ordered=True, fields will sorted by creation index.
:param attrs: Mapping of class attributes
:param bool ordered: Sort fields by creation index
"""
fields = [
(field_name, field_value)
Expand Down Expand Up @@ -98,6 +99,10 @@ def __new__(mcs, name, bases, attrs):
else:
ordered = False
cls_fields = _get_fields(attrs, ordered=ordered)
# Remove fields from list of class attributes to avoid shadowing
# Schema attributes/methods in case of name conflict
for field_name, _ in cls_fields:
del attrs[field_name]
klass = super().__new__(mcs, name, bases, attrs)
inherited_fields = _get_fields_by_mro(klass, ordered=ordered)

Expand Down

0 comments on commit af9c3d5

Please sign in to comment.