Skip to content

Commit c76a5b9

Browse files
PrettyWoodsamuelcolvin
authored andcommitted
fix: ignore __doc__ as valid private attribute (#2091)
closes #2090
1 parent 6853032 commit c76a5b9

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

changes/2090-PrettyWood.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ignore `__doc__` as private attribute when `Config.underscore_attrs_are_private` is set

pydantic/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,10 @@ def is_valid_field(name: str) -> bool:
631631

632632

633633
def is_valid_private_name(name: str) -> bool:
634-
return not is_valid_field(name) and name not in {'__annotations__', '__classcell__', '__module__', '__qualname__'}
634+
return not is_valid_field(name) and name not in {
635+
'__annotations__',
636+
'__classcell__',
637+
'__doc__',
638+
'__module__',
639+
'__qualname__',
640+
}

tests/test_private_attributes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class Model(BaseModel):
5555

5656
def test_private_attribute_annotation():
5757
class Model(BaseModel):
58+
"""The best model"""
59+
5860
__foo__: str
5961

6062
class Config:
@@ -63,6 +65,7 @@ class Config:
6365
assert Model.__slots__ == {'__foo__'}
6466
assert repr(Model.__foo__) == "<member '__foo__' of 'Model' objects>"
6567
assert Model.__private_attributes__ == {'__foo__': PrivateAttr(Undefined)}
68+
assert repr(Model.__doc__) == "'The best model'"
6669

6770
m = Model()
6871
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)