fix: support underscore_attrs_are_private with generic models (#2139)

closes #2138
This commit is contained in:
Eric Jolibois
2020-11-30 18:54:07 +01:00
committed by GitHub
parent 1a2791d422
commit a82a411d60
3 changed files with 23 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
fix: support `underscore_attrs_are_private` with generic models
+1
View File
@@ -636,5 +636,6 @@ def is_valid_private_name(name: str) -> bool:
'__classcell__',
'__doc__',
'__module__',
'__orig_bases__',
'__qualname__',
}
+21 -1
View File
@@ -1,9 +1,13 @@
from typing import ClassVar
import sys
from typing import ClassVar, Generic, TypeVar
import pytest
from pydantic import BaseModel, Extra, PrivateAttr
from pydantic.fields import Undefined
from pydantic.generics import GenericModel
skip_36 = pytest.mark.skipif(sys.version_info < (3, 7), reason='generics only supported for python 3.7 and above')
def test_private_attribute():
@@ -180,3 +184,19 @@ def test_config_override_init():
m = MyModel(x='hello')
assert m.dict() == {'x': 'hello'}
assert m._private_attr == 123
@skip_36
def test_generic_private_attribute():
T = TypeVar('T')
class Model(GenericModel, Generic[T]):
value: T
_private_value: T
class Config:
underscore_attrs_are_private = True
m = Model[int](value=1, _private_attr=3)
m._private_value = 3
assert m.dict() == {'value': 1}