Skip to content

Commit

Permalink
Fix unparameterized generic type schema generation (#625)
Browse files Browse the repository at this point in the history
* Fix #623

* Update history

* Add List test

* Augment test
  • Loading branch information
dmontagu authored and samuelcolvin committed Jul 2, 2019
1 parent 3cdbbae commit 8aac71a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -10,6 +10,7 @@ v0.30 (unreleased)
* fix default values for ``GenericModel``, #610 by @dmontagu
* clarify, that self-referencing models require python 3.7+, #616 by @vlcinsky
* fix truncate for types, #611 by @dmontagu
* fix unparameterized generic type schema generation, #625 by @dmontagu
* fix schema generation with multiple/circular references to the same model, #621 by @tiangolo and @wongpat

v0.29 (2019-06-19)
Expand Down
4 changes: 2 additions & 2 deletions pydantic/schema.py
Expand Up @@ -4,7 +4,7 @@
from decimal import Decimal
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Type, Union, cast
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Type, TypeVar, Union, cast
from uuid import UUID

import pydantic
Expand Down Expand Up @@ -731,7 +731,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
ref_prefix=ref_prefix,
known_models=known_models,
)
if field.type_ is Any:
if field.type_ is Any or type(field.type_) == TypeVar:
return {}, definitions, nested_models # no restrictions
if is_callable_type(field.type_):
raise SkipField(f'Callable {field.name} was excluded from schema since JSON schema has no equivalent type.')
Expand Down
47 changes: 46 additions & 1 deletion tests/test_schema.py
Expand Up @@ -11,7 +11,13 @@
import pytest

from pydantic import BaseModel, Schema, ValidationError, validator
from pydantic.schema import get_flat_models_from_model, get_flat_models_from_models, get_model_name_map, schema
from pydantic.schema import (
get_flat_models_from_model,
get_flat_models_from_models,
get_model_name_map,
model_schema,
schema,
)
from pydantic.types import (
DSN,
UUID1,
Expand Down Expand Up @@ -1303,6 +1309,45 @@ def check_field(cls, v, *, values, config, field):
}


def test_unparameterized_schema_generation():
class FooList(BaseModel):
d: List

class BarList(BaseModel):
d: list

assert model_schema(FooList) == {
'title': 'FooList',
'type': 'object',
'properties': {'d': {'items': {}, 'title': 'D', 'type': 'array'}},
'required': ['d'],
}

foo_list_schema = model_schema(FooList)
bar_list_schema = model_schema(BarList)
bar_list_schema['title'] = 'FooList' # to check for equality
assert foo_list_schema == bar_list_schema

class FooDict(BaseModel):
d: Dict

class BarDict(BaseModel):
d: dict

model_schema(Foo)
assert model_schema(FooDict) == {
'title': 'FooDict',
'type': 'object',
'properties': {'d': {'title': 'D', 'type': 'object'}},
'required': ['d'],
}

foo_dict_schema = model_schema(FooDict)
bar_dict_schema = model_schema(BarDict)
bar_dict_schema['title'] = 'FooDict' # to check for equality
assert foo_dict_schema == bar_dict_schema


def test_known_model_optimization():
class Dep(BaseModel):
number: int
Expand Down

0 comments on commit 8aac71a

Please sign in to comment.