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 produce schema containing Literal #646

Closed
layday opened this issue Jul 9, 2019 · 1 comment
Closed

Cannot produce schema containing Literal #646

layday opened this issue Jul 9, 2019 · 1 comment
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@layday
Copy link
Contributor

layday commented Jul 9, 2019

Bug

Thought I'd give the new Literal a whirl and stumbled on this:

In [1]: import sys

In [2]: sys.platform, sys.version
Out[2]:
('darwin',
 '3.7.2 (default, Mar  1 2019, 12:15:59) \n[Clang 5.0.2 (tags/RELEASE_502/final)]')

In [3]: import pydantic

In [4]: pydantic.VERSION
Out[4]: StrictVersion ('0.30')

In [5]: from typing_extensions import Literal

In [6]: class Foo(pydantic.BaseModel):
   ...:     bar: Literal['baz']
   ...:

In [7]: Foo.schema()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-0d83fa816939> in <module>
----> 1 Foo.schema()

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/main.py in schema(cls, by_alias)
    461         if cached is not None:
    462             return cached
--> 463         s = model_schema(cls, by_alias=by_alias)
    464         cls._schema_cache[by_alias] = s
    465         return s

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in model_schema(model, by_alias, ref_prefix)
    233     model_name = model_name_map[model]
    234     m_schema, m_definitions, nested_models = model_process_schema(
--> 235         model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
    236     )
    237     if model_name in nested_models:

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in model_process_schema(model, by_alias, model_name_map, ref_prefix, known_models)
    551     known_models.add(model)
    552     m_schema, m_definitions, nested_models = model_type_schema(
--> 553         model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
    554     )
    555     s.update(m_schema)

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in model_type_schema(model, by_alias, model_name_map, ref_prefix, known_models)
    579         try:
    580             f_schema, f_definitions, f_nested_models = field_schema(
--> 581                 f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
    582             )
    583         except SkipField as skip:

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in field_schema(field, by_alias, model_name_map, ref_prefix, known_models)
    292         schema_overrides=schema_overrides,
    293         ref_prefix=ref_prefix,
--> 294         known_models=known_models or set(),
    295     )
    296     # $ref will only be returned when there are no schema_overrides

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in field_type_schema(field, by_alias, model_name_map, schema_overrides, ref_prefix, known_models)
    522             schema_overrides=schema_overrides,
    523             ref_prefix=ref_prefix,
--> 524             known_models=known_models,
    525         )
    526         definitions.update(f_definitions)

~/code/xyz/.venv/lib/python3.7/site-packages/pydantic/schema.py in field_singleton_schema(field, by_alias, model_name_map, schema_overrides, ref_prefix, known_models)
    743     if field.schema is not None and field.schema.const:
    744         f_schema['const'] = field.default
--> 745     if issubclass(field.type_, Enum):
    746         f_schema.update({'enum': [item.value for item in field.type_]})
    747         # Don't return immediately, to allow adding specific types

TypeError: issubclass() arg 1 must be a class
@dmontagu
Copy link
Contributor

dmontagu commented Jul 9, 2019

This same issue comes up for NewType as well. I'll take a look at this tonight.

@samuelcolvin samuelcolvin added the bug V1 Bug related to Pydantic V1.X label Jul 11, 2019
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

3 participants