Skip to content

Cannot use Literal from standard library in 3.8 #1026

@layday

Description

@layday

Bug

  • OS: macOS
  • Python version import sys; print(sys.version): 3.8.0
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.1.1

Annotating models with the built-in literal in Python 3.8 in the absence of typing_extensions does not work because is_literal_type only checks if Literal is available from typing_extensions. Please see the snippet below.

$ cat test.py
from typing import Literal
from pydantic import BaseModel


class Foo(BaseModel):
    bar: Literal['']


print('ok!')
$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    class Foo(BaseModel):
  File "/Users/layday/code/instawow/.venvs/3.8/lib/python3.8/site-packages/pydantic/main.py", line 192, in __new__
    fields[ann_name] = ModelField.infer(
  File "/Users/layday/code/instawow/.venvs/3.8/lib/python3.8/site-packages/pydantic/fields.py", line 260, in infer
    return cls(
  File "/Users/layday/code/instawow/.venvs/3.8/lib/python3.8/site-packages/pydantic/fields.py", line 237, in __init__
    self.prepare()
  File "/Users/layday/code/instawow/.venvs/3.8/lib/python3.8/site-packages/pydantic/fields.py", line 307, in prepare
    self._type_analysis()
  File "/Users/layday/code/instawow/.venvs/3.8/lib/python3.8/site-packages/pydantic/fields.py", line 359, in _type_analysis
    if issubclass(origin, Tuple):  # type: ignore
  File "/nix/store/mnl1njrv6g2jcs69kq0cy19766kcgc5n-python3-3.8.0/lib/python3.8/typing.py", line 771, in __subclasscheck__
    return issubclass(cls, self.__origin__)
TypeError: issubclass() arg 1 must be a class
$ pip3 install typing_extensions
Collecting typing_extensions
  Using cached https://files.pythonhosted.org/packages/03/92/705fe8aca27678e01bbdd7738173b8e7df0088a2202c80352f664630d638/typing_extensions-3.7.4.1-py3-none-any.whl
Installing collected packages: typing-extensions
Successfully installed typing-extensions-3.7.4.1
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ python3 test.py
ok!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.Xhelp wantedPull Request welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions