Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Literal from standard library in 3.8 #1026

Closed
layday opened this issue Nov 24, 2019 · 1 comment · Fixed by #1027
Closed

Cannot use Literal from standard library in 3.8 #1026

layday opened this issue Nov 24, 2019 · 1 comment · Fixed by #1027
Labels

Comments

@layday
Copy link
Contributor

@layday layday commented Nov 24, 2019

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!
@layday layday added the bug label Nov 24, 2019
@samuelcolvin

This comment has been minimized.

Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 24, 2019

Thanks for reporting. PR welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.