Repository navigation
Cannot use Literal when I use typing-extension==4.6.0 #5821
Copy link
Copy link
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Description
Initial Checks
- I have searched GitHub for a duplicate issue and I'm sure this is something new
- I have searched Google & StackOverflow for a solution and couldn't find anything
- I have read and followed the docs and still think this is a bug
- I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)
Description
I update my typing extention 4.6.0, I face this error
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/entry.py:3: in <module>
from apis import router as api_router
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/apis/__init__.py:5: in <module>
from .recsys import router as recsys_router
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/apis/recsys/__init__.py:3: in <module>
from .course.endpoints import router as course_router
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/apis/recsys/course/__init__.py:1: in <module>
from . import cell
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/apis/recsys/course/cell/__init__.py:1: in <module>
from . import container
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/apis/recsys/course/cell/container.py:4: in <module>
from core.models.feature_store.cell_dtos import CourseCellDTO
.venv/lib/python3.9/site-packages/ddtrace/internal/module.py:216: in _exec_module
self.loader.exec_module(module)
src/core/models/feature_store/cell_dtos.py:14: in <module>
class VocaCellDTO(BaseModel):
pydantic/main.py:197: in pydantic.main.ModelMetaclass.__new__
???
pydantic/fields.py:506: in pydantic.fields.ModelField.infer
???
pydantic/fields.py:436: in pydantic.fields.ModelField.__init__
???
pydantic/fields.py:552: in pydantic.fields.ModelField.prepare
???
pydantic/fields.py:668: in pydantic.fields.ModelField._type_analysis
???
Example Code
from enum import Enum
from typing import Literal
from pydantic import BaseModel
class MyEum(str, Enum):
T1 = "T1"
T2 = "T2"
class MyType(BaseModel):
type: Literal[MyEum.T1] = MyEum.T1
dalc: strthen I meet
Traceback (most recent call last):
File "/Users/project/test.py", line 12, in <module>
class MyType(BaseModel):
File "pydantic/main.py", line 197, in pydantic.main.ModelMetaclass.__new__
File "pydantic/fields.py", line 506, in pydantic.fields.ModelField.infer
File "pydantic/fields.py", line 436, in pydantic.fields.ModelField.__init__
File "pydantic/fields.py", line 552, in pydantic.fields.ModelField.prepare
File "pydantic/fields.py", line 668, in pydantic.fields.ModelField._type_analysis
File "/Users/project/lib/python3.9/typing.py", line 852, in __subclasscheck__
return issubclass(cls, self.__origin__)
TypeError: issubclass() arg 1 must be a class
Python, Pydantic & OS Version
pydantic version: 1.10.7
pydantic compiled: True
python version: 3.9.13 (main, Aug 25 2022, 18:24:45) [Clang 12.0.0 ]
platform: macOS-12.6.1-arm64-arm-64bit
optional deps. installed: ['dotenv', 'typing-extensions']
Affected Components
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.dict()and.json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
construct(), pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Activity
cmditch commentedon May 23, 2023
Just started hitting this as well.
mdavis-xyz commentedon May 23, 2023
I'm getting this too. Even on pydantic v2.
As a workaround:
from typing import Literaltofrom typing_extensions import LiteralThe docs for pydantic do say:
But I find that documentation ambiguous. It sounds like the requirement is that typing_extensions merely be installed, and then pydantic will choose it if required. But that doesn't seem to be the case.
So I think we need clearer documentation.
I think the source of the bug is this line:
https://github.com/pydantic/pydantic/blob/bf6b884563e7c635b9d811e97d2fdb51805398b1/pydantic/typing.py#LL412C57-L412C64
So I think this is a bug in pydantic.
is_literal_typeis comparing only totyping_extensions.Literal, nottyping.Literal.I think a patch looks something like:
AiyionPrime commentedon May 23, 2023
Can confirm, the bug appears to occur with
typing-extension==4.6.0.eachimei commentedon May 23, 2023
Minimal steps to reproduce the issue (typing_extensions==4.6.0, pydantic==1.10.7, Python 3.8.10):
raises:
TypeError: issubclass() arg 1 must be a classsamuelcolvin commentedon May 23, 2023
Thanks for reporting, @hramezani will take a look 🙏.
Czaki commentedon May 23, 2023
I could confirm that this bug is caused by this commit python/typing_extensions@fb37b2e
hramezani commentedon May 23, 2023
@mdavis-xyz thanks for your explanation. seems your suggestion will fix the bug. Do you want to create a patch for fixing? otherwise, I will create a patch.
AlexWaygood commentedon May 23, 2023
This has also been reported over at typing_extensions FYI: python/typing_extensions#179.
If this analysis is correct (and I haven't checked, but it looks very plausible), you'd probably want to do something like this, in order to retain compatibility with Python 3.7 (where
typing.Literalisn't a thing yet):(Not sure under what conditions
Literalcan beNonebtw? Seems like a slight oddity in the code as it's currently written :)sietse commentedon May 23, 2023
List of workarounds:
typing_extensions==4.5.0typing.Literalto refer totyping_extensions.Literal. (Full patch example below.) You only need to do this in the initial module.from typing import Literalwithfrom typing_extensions import Literal.The patch of workaround 3:
Pin typing_extensions < 4.6.0
Pin typing_extensions < 4.6.0 (#14407)
7 remaining items
samuelcolvin commentedon May 23, 2023
Released.
jessekrubin commentedon May 23, 2023
@dmontagu @samuelcolvin yall the best!
jnawk commentedon May 23, 2023
How about yanking 1.10.7 from pypi? (as in the footsteps of 1.10.3)
Czaki commentedon May 23, 2023
But this is not a bug only in 1.10.7 It is present in more versions.
dmontagu commentedon May 23, 2023
@jnawk @Czaki the reason that 1.10.3 was yanked was because 1.10.4 updated the minimum-allowed-version of typing_extensions. As a result, it was incompatible to use pydantic 1.10.4 with other libraries that had pinned the version of typing_extensions below 4.2.0.
However, the release of pydantic 1.10.8 did not set any new minimum dependency versions (it just fixed the bug). So unless I'm missing something, I don't think the same reasons for yanking the 1.10.3 pydantic version apply here.
deps: floor pydantic
NOJIRA: Pin typing extensions to 4.5.0 to workaround bug in pydantic
deps: floor pydantic
Unpin typing_extensions due to pydantic issue
Unpin typing_extensions due to pydantic issue
Pin typing_extensions < 4.6.0 (#14407)
Resolves issue related to use of Literal (#303)
Resolves issue related to use of Literal (#303)
RandallPittmanOrSt commentedon Apr 10, 2024
Just to add info: From my tests, this bug is present for all installs I tried with Python 3.7 as well as any combination of the following:
It appears to be not an issue for any version of Pydantic 1.10.8+ or Python 3.10[.6]+. It also appears to not be a problem for typing-extensions 4.4 or 4.5. I did not test Pydantic < 1.8 or typing-extensions < 4.4. I only tested incremental minor versions of typing-extensions (4.6.0, 4.7.0 etc) and the specific Python 3.8 and 3.9 versions noted above.