Skip to content

Commit 0d2b4d6

Browse files
PrettyWoodsamuelcolvin
authored andcommitted
fix: support underscore_attrs_are_private with generic models (#2139)
closes #2138
1 parent bc4092b commit 0d2b4d6

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

changes/2138-PrettyWood.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: support `underscore_attrs_are_private` with generic models

pydantic/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,5 +636,6 @@ def is_valid_private_name(name: str) -> bool:
636636
'__classcell__',
637637
'__doc__',
638638
'__module__',
639+
'__orig_bases__',
639640
'__qualname__',
640641
}

tests/test_private_attributes.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from typing import ClassVar
1+
import sys
2+
from typing import ClassVar, Generic, TypeVar
23

34
import pytest
45

56
from pydantic import BaseModel, Extra, PrivateAttr
67
from pydantic.fields import Undefined
8+
from pydantic.generics import GenericModel
9+
10+
skip_36 = pytest.mark.skipif(sys.version_info < (3, 7), reason='generics only supported for python 3.7 and above')
711

812

913
def test_private_attribute():
@@ -180,3 +184,19 @@ class Config:
180184
m = MyModel(x='hello')
181185
assert m.dict() == {'x': 'hello'}
182186
assert m._private_attr == 123
187+
188+
189+
@skip_36
190+
def test_generic_private_attribute():
191+
T = TypeVar('T')
192+
193+
class Model(GenericModel, Generic[T]):
194+
value: T
195+
_private_value: T
196+
197+
class Config:
198+
underscore_attrs_are_private = True
199+
200+
m = Model[int](value=1, _private_attr=3)
201+
m._private_value = 3
202+
assert m.dict() == {'value': 1}

0 commit comments

Comments
 (0)