Skip to content

Commit

Permalink
[FIX] pydantic: Makes issubclass on BaseModel wrking with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Dec 8, 2021
1 parent 25dd584 commit 08e117a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pydantic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 17 additions & 1 deletion pydantic/tests/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 08e117a

Please sign in to comment.