From 08e117a47f3367132f976ae88123f753d42276ec Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Wed, 8 Dec 2021 13:35:18 +0100 Subject: [PATCH] [FIX] pydantic: Makes issubclass on BaseModel wrking with generics --- pydantic/models.py | 2 +- pydantic/tests/test_pydantic.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pydantic/models.py b/pydantic/models.py index bf5f00b6e..12f36b0c2 100644 --- a/pydantic/models.py +++ b/pydantic/models.py @@ -151,7 +151,7 @@ def __subclasscheck__(cls, subclass): # noqa: B902 """Implement issubclass(sub, cls).""" if hasattr(subclass, "_original_cls"): return cls.__subclasscheck__(subclass._original_cls) - return super().__subclasscheck__(subclass) + return isinstance(subclass, type) and super().__subclasscheck__(subclass) class BaseModel(pydantic.BaseModel, metaclass=ExtendablePydanticModelMeta): diff --git a/pydantic/tests/test_pydantic.py b/pydantic/tests/test_pydantic.py index 76d2133ef..bd31ac3bf 100644 --- a/pydantic/tests/test_pydantic.py +++ b/pydantic/tests/test_pydantic.py @@ -1,7 +1,7 @@ # Copyright 2021 ACSONE SA/NV # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) -from typing import List +from typing import List, Literal import pydantic @@ -154,3 +154,19 @@ class ExtendedLocation(Location, extends=Location): inst2 = ExtendedLocation.construct() self.assertEqual(inst1.__class__, inst2.__class__) self.assertEqual(inst1.schema(), inst2.schema()) + + def test_issubclass(self): + """In this test we check that issublass is lenient when used with + GenericAlias + """ + self.assertTrue() + self.assertFalse(issubclass(Literal["test"], models.BaseModel)) + self.assertFalse(issubclass(Literal, models.BaseModel)) + + class Location(models.BaseModel): + kind: Literal["view", "bin"] + my_list: List[str] + + self._build_pydantic_classes(Location) + schema = Location.schema() + self.assertTrue(schema)