Skip to content

Commit

Permalink
fix: support underscore_attrs_are_private with generic models (#2139)
Browse files Browse the repository at this point in the history
closes #2138
  • Loading branch information
PrettyWood authored and samuelcolvin committed Nov 30, 2020
1 parent bc4092b commit 0d2b4d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/2138-PrettyWood.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: support `underscore_attrs_are_private` with generic models
1 change: 1 addition & 0 deletions pydantic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,5 +636,6 @@ def is_valid_private_name(name: str) -> bool:
'__classcell__',
'__doc__',
'__module__',
'__orig_bases__',
'__qualname__',
}
22 changes: 21 additions & 1 deletion tests/test_private_attributes.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -180,3 +184,19 @@ class Config:
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}

0 comments on commit 0d2b4d6

Please # to comment.