Skip to content

Commit

Permalink
[FIX] pydantic: Makes issubclass on BaseModel working 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 bfa2925
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 1 addition & 4 deletions pydantic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
"data": [],
"demo": [],
"external_dependencies": {
"python": [
"pydantic",
"contextvars",
]
"python": ["pydantic", "contextvars", "typing-extensions>=4.0.1"]
},
"installable": True,
}
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
20 changes: 20 additions & 0 deletions pydantic/tests/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

from typing import List

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal

import pydantic

from .. import models, utils
Expand Down Expand Up @@ -154,3 +159,18 @@ 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.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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ marshmallow-objects>=2.0.0
parse-accept-language
pydantic
pyquerystring
typing-extensions>=4.0.1

0 comments on commit bfa2925

Please sign in to comment.