Skip to content

Instantly share code, notes, and snippets.

@mariusvniekerk
Last active December 8, 2020 17:30
Show Gist options
  • Save mariusvniekerk/bf508dfe1437c1598b7a5680850732e8 to your computer and use it in GitHub Desktop.
Save mariusvniekerk/bf508dfe1437c1598b7a5680850732e8 to your computer and use it in GitHub Desktop.
pydantic error case for GenericModel
from typing import TypeVar, Generic
import pytest
from pydantic import BaseModel, PrivateAttr
from pydantic.generics import GenericModel
class Regular(BaseModel):
value: int
T = TypeVar('T')
T_Bound = TypeVar('T_Bound', bound=Regular)
class RegGen(BaseModel, Generic[T_Bound]):
value: T_Bound
_privateattr: T_Bound = PrivateAttr(None)
class GenBound(GenericModel, Generic[T_Bound]):
value: T_Bound
_privateattr: T_Bound = PrivateAttr(None)
class GenT(GenericModel, Generic[T]):
value: T
_privateattr: T = PrivateAttr(None)
class Outer(BaseModel):
a: GenBound[Regular]
b: GenT[int]
c: RegGen[Regular]
@pytest.fixture
def regular():
regular = Regular(value=8)
return regular
@pytest.fixture
def a(regular):
a = GenBound(value=regular)
a._privateattr = regular
return a
@pytest.fixture
def b():
b = GenT(value=42)
b._privateattr = 42
return b
@pytest.fixture
def c(regular):
c = RegGen(value=regular)
c._privateattr = regular
return c
@pytest.fixture()
def o(a, b, c):
o = Outer(a=a, b=b, c=c)
return o
def test_equality(o: Outer, a: GenBound[Regular], b: GenT[int], c: RegGen[Regular], regular: Regular):
assert o.a == a
assert o.b == b
assert o.c == c
assert o.a.value == regular
assert o.b.value == 42
assert o.c.value == regular
def test_private_attr_generic_bound(o: Outer, regular: Regular):
assert o.a._privateattr == regular
def test_private_attr_generic_unbound(o: Outer):
assert o.b._privateattr == 42
def test_private_attr_basemodel_generic(o: Outer, regular: Regular):
assert o.c._privateattr == regular
if __name__ == '__main__':
import pytest
pytest.main([__file__])
============================= test session starts ==============================
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /Users/mvanniekerk/Library/Application Support/JetBrains/PyCharm2020.3/scratches
plugins: fh-pytest-0.3.1
collected 4 items
test_pydantic_model.py .FF. [100%]
=================================== FAILURES ===================================
_______________________ test_private_attr_generic_bound ________________________
o = Outer(a=GenBound[Regular](value=Regular(value=8)), b=GenT[int](value=42), c=RegGen(value=Regular(value=8)))
regular = Regular(value=8)
def test_private_attr_generic_bound(o: Outer, regular: Regular):
> assert o.a._privateattr == regular
E assert None == Regular(value=8)
E + where None = GenBound[Regular](value=Regular(value=8))._privateattr
E + where GenBound[Regular](value=Regular(value=8)) = Outer(a=GenBound[Regular](value=Regular(value=8)), b=GenT[int](value=42), c=RegGen(value=Regular(value=8))).a
test_pydantic_model.py:77: AssertionError
______________________ test_private_attr_generic_unbound _______________________
o = Outer(a=GenBound[Regular](value=Regular(value=8)), b=GenT[int](value=42), c=RegGen(value=Regular(value=8)))
a = GenBound(value=Regular(value=8)), b = GenT(value=42)
c = RegGen(value=Regular(value=8)), regular = Regular(value=8)
def test_private_attr_generic_unbound(o: Outer, a: GenBound[Regular], b: GenT[int], c: RegGen[Regular],
regular: Regular):
> assert o.b._privateattr == 42
E assert None == 42
E + where None = GenT[int](value=42)._privateattr
E + where GenT[int](value=42) = Outer(a=GenBound[Regular](value=Regular(value=8)), b=GenT[int](value=42), c=RegGen(value=Regular(value=8))).b
test_pydantic_model.py:81: AssertionError
=========================== short test summary info ============================
FAILED test_pydantic_model.py::test_private_attr_generic_bound - assert None ...
FAILED test_pydantic_model.py::test_private_attr_generic_unbound - assert Non...
========================= 2 failed, 2 passed in 0.34s ==========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment