pydantic: TypeError: issubclass() arg 1 must be a class
Bug
Hi. I’m getting a type error when trying to generate a schema, using fastapi:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 133, in __call__
await self.error_middleware(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 122, in __call__
raise exc from None
File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 100, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 76, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
raise exc from None
File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 585, in __call__
await route(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 207, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 40, in app
response = await func(request)
File "/usr/local/lib/python3.7/site-packages/fastapi/applications.py", line 90, in openapi
return JSONResponse(self.openapi())
File "/usr/local/lib/python3.7/site-packages/fastapi/applications.py", line 82, in openapi
openapi_prefix=self.openapi_prefix,
File "/usr/local/lib/python3.7/site-packages/fastapi/openapi/utils.py", line 248, in get_openapi
flat_models=flat_models, model_name_map=model_name_map
File "/usr/local/lib/python3.7/site-packages/fastapi/utils.py", line 42, in get_model_definitions
model, model_name_map=model_name_map, ref_prefix=REF_PREFIX
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 511, in model_process_schema
model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 537, in model_type_schema
f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 274, in field_schema
ref_prefix=ref_prefix,
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 486, in field_type_schema
ref_prefix=ref_prefix,
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 687, in field_singleton_schema
if issubclass(field.type_, Enum):
TypeError: issubclass() arg 1 must be a class
For bugs/questions:
- OS:
uname -a Linux char-lang-dev 4.18.0-20-generic #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux - Python version
import sys; print(sys.version): ‘3.7.1 (default, Oct 22 2018, 11:21:55) \n[GCC 8.2.0]’ - Pydantic version
import pydantic; print(pydantic.VERSION): ‘3.7.1 (default, Oct 22 2018, 11:21:55) \n[GCC 8.2.0]’
Where possible please include a self contained code snippet describing your bug, question, or where applicable feature request:
from __future__ import annotations
from typing import Optional, List
from pydantic import BaseModel, UUID4
class CategoryCreationValidator(BaseModel):
name: str = ...
labels: List[str] = None
description: str = ...
parent_id: int = None
class CategorySelectionValidator(BaseModel):
name: str = ...
organization_id: str = ...
labels: Optional[str] = None
description: str = ...
parent_id: Optional[int] = None
class CategoryUpdateValidator(BaseModel):
name: Optional[str] = None
organization_id: Optional[str] = None
labels: Optional[List[str]] = None
description: Optional[str] = None
parent_id: Optional[int] = None
class ClusterValidator(BaseModel):
name: str = ...
category_id: Optional[str] = None
class CategoryModel(BaseModel):
id: int = ...
name: str = ...
description: str = ...
parent_id: int = None
children: List[CategoryModel]
#CategoryModel.update_forward_refs()
class ClusterModel(BaseModel):
id: UUID4 = ...
name: str = ...
parent_category_id: str = ...
queries: List[str] = ...
urls: List[str] = ...
class SuggestionValidator(BaseModel):
queries: List[str] = ...
urls: List[str] = ...
class QueryCreationValidator(BaseModel):
query_text: str = ...
cluster_id: UUID4 = ...
class QuerySelectionValidator(BaseModel):
cluster_id: UUID4 = ...
class UrlCreationValidator(BaseModel):
url: str = ...
cluster_id: UUID4 = ...
class UrlSelectionValidator(BaseModel):
cluster_id: UUID4 = ...
class CreateClusterInput(BaseModel):
name: str = ...
category_id: int = ...
class ClusterSelectionValidator(BaseModel):
category_name: str = ...
...
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 7
- Comments: 37 (15 by maintainers)
Commits related to this issue
- Update validators.py Found this discussion: https://github.com/samuelcolvin/pydantic/issues/545; `lenient_issubclass` does seem to fix it. — committed to dmontagu/pydantic by dmontagu 5 years ago
- Update validators.py Found this discussion: https://github.com/samuelcolvin/pydantic/issues/545; `lenient_issubclass` does seem to fix it. — committed to dmontagu/pydantic by dmontagu 5 years ago
- Fix issue with unspecified generic type (#554) * Fix issue with unspecified generic type, fix #550 Seems to solve #550 With this change, models with bare `List` or `Dict` as a typehint still va... — committed to pydantic/pydantic by dmontagu 5 years ago
- fix: self-related pydantic model fails https://github.com/samuelcolvin/pydantic/issues/545 — committed to dubovikai/zar_aggregator by dubovikai 2 years ago
- temporarily guard from https://github.com/pydantic/pydantic/issues/545 — committed to ewdurbin/dbml-to-fides by ewdurbin a year ago
- temporarily guard from https://github.com/pydantic/pydantic/issues/545 — committed to ewdurbin/dbml-to-fides by ewdurbin a year ago
- Import Literal from typing_extensions to work around https://github.com/pydantic/pydantic/issues/545 — committed to PrefectHQ/prefect-gcp by desertaxle a year ago
- Fixes typos in project steps doc strings (#180) * Fixes typos in project steps doc strings * Adds `project` argument * Clarifies GCP project * Satisfy flake8 * Fixes formatting * Impor... — committed to PrefectHQ/prefect-gcp by desertaxle a year ago
- try this to fix the bug https://github.com/pydantic/pydantic/issues/545#issuecomment-1559115237 — committed to vagechirkov/reward-networks-iii-experiment by vagechirkov a year ago
- matrix_benchmarking/models.py: disable payload_schema Causes https://github.com/pydantic/pydantic/issues/545 exception — committed to kpouget/matrix-benchmarking by kpouget a year ago
- pin typing extension to fix backend bug (#137) Without this fix, I see the following error everywhere (locally on master, or from a fresh workspace): Note: this is thrown since this change https... — committed to ray-project/ray-llm by maxpumperla a year ago
- pin typing extension to fix backend bug (#137) Without this fix, I see the following error everywhere (locally on master, or from a fresh workspace): Note: this is thrown since this change https... — committed to datastark/aviary by maxpumperla a year ago
- Pin version of typing_extensions * Due to the following bug that occurs since version 4.6.0: https://github.com/pydantic/pydantic/issues/545 * Pin can be removed once either the bug is resolved or we... — committed to emdgroup/baybe by AdrianSosic a year ago
- try this to fix the bug https://github.com/pydantic/pydantic/issues/545#issuecomment-1559115237 — committed to center-for-humans-and-machines/reward-network-iii by vagechirkov a year ago
- try this to fix the bug https://github.com/pydantic/pydantic/issues/545#issuecomment-1559115237 — committed to center-for-humans-and-machines/reward-network-iii by vagechirkov a year ago
I had the same issue, Python
3.9, Pydantic1.9.2and tested with1.10.7also.Strangely I had to reinstall Pydantic to trigger the same problem locally. I noticed the reinstall also installed the newly released typing-extensions 4.6.0. Forcing typing-extensions==4.5.0 fixed the issue for me.
To reproduce, tested with Pydantic
1.9.2and1.10.7:I hope this isn’t as widespread as it seems at first glance.
To fix: Force typing-extensions==4.5.0.
EDIT: This was already pointed out in https://github.com/pydantic/pydantic/issues/5821 but I’ll leave this up here as it looks like people are finding this thread first when googling the error message. Notably this seems to not be an issue with Python
>=3.10.1.i have suddenly started facing the same problem.
pydantic==1.8.2 python 3.9.10
class RuleStackProfiles(BaseModel, extra=Extra.forbid): 10:58:20 pydantic/main.py:299: in pydantic.main.ModelMetaclass.new 10:58:20 ??? 10:58:20 pydantic/fields.py:411: in pydantic.fields.ModelField.infer 10:58:20 ??? 10:58:20 pydantic/fields.py:342: in pydantic.fields.ModelField.init 10:58:20 ??? 10:58:20 pydantic/fields.py:451: in pydantic.fields.ModelField.prepare 10:58:20 ??? 10:58:20 pydantic/fields.py:545: in pydantic.fields.ModelField._type_analysis 10:58:20 ??? 10:58:20 pydantic/fields.py:550: in pydantic.fields.ModelField._type_analysis 10:58:20 ??? 10:58:20 /usr/local/lib/python3.9/typing.py:851: in subclasscheck 10:58:20 return issubclass(cls, self.origin) 10:58:20 E TypeError: issubclass() arg 1 must be a class
If you’re stuck on
typing_extensions==4.6.0, you can also avoid the bug by importing Literal fromtyping_extensionsinstead of fromtyping.Downgrading the typing-extension version to 4.5.0 working perfectly ! Thank’s
Ok, I can confirm the issue is the self-reference(
children: List[CategoryModel]). Using justlistorList[Any]avoids the error.issue persist
Removing
from __future__ import annotationsfixed this issue for me. However, it would be cool if there would be support for this.Hit this one, I think, with very simple reproduction:
Similar issue when generating schema in FastAPI.
So far using
class Age(int): passas a workaround.Thanks @Karimai.
Yeah, as @freywaid mentioned,
update_forward_refsshould solve the problem.@Karimai update Pydantic to
1.10.10@Kilo59 please try
pydantic 1.10.8that contains the fixFWIW, also started happening here, with exactly same symptoms, and without any modifications to the code. We have Pydantic version pinned. Happens also on classes which don’t self-reference themselves.
Versions: Python
3.8, pydantic1.10.5(also tried1.10.7with same result).@haizaar This is fixed in the current version of pydantic (v0.28), with or without the
__future__import. Or at least, I am able to run the code snippet you provided above (in python 3.7.3) without any errors.@Karimai @freywaid could you please provide a minimal example that reproduce the error?
@hramezani I’m at pydantic 1.10.10 and I see this error… Adding: I added an MyModel.update_forward_refs() and this resolved the error.
Happens similarly here, started randomly happening this morning, sadly i cannot force typing-extensions version for our project layout at the moment
Version: Python 3.8, pydantic 1.10.7
Can also confirm we randomly started seeing the same issue in the past hour for what seems like no apparent reason. Code has been working for months and hasn’t been touched, but our builds randomly started failing, pointing to this error as the reason
pydantic==1.9.0 python 3.8.16
Great! I’ll close this issue now then.
@DrPyser I’m not sure what was the problem, maybe it was fixed recently in Pydantic or in FastAPI.
But here’s a slightly modified (without
from __future__ import annotations) self-contained working app:Thanks for tagging me @samuelcolvin .
I think this is then a duplicate of https://github.com/samuelcolvin/pydantic/issues/531
I still have to check/fix the JSON Schema generation when using forward refs.
For @zbarry problem, you can use
dictorDict[Any, Any]. But that’s has a separate issue now.sounds like a different problem, could you create a new issue and perhaps even a fix?
In the meantime you should be able to use just
dictinstead ofDictwhich will have the same effect but shouldn’t fail.