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

Fix field of a type that has a default value #880

Merged
merged 4 commits into from Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/880-koxudaxi.md
@@ -0,0 +1 @@
Fix field of a type that has a default value.
8 changes: 6 additions & 2 deletions pydantic/main.py
Expand Up @@ -18,7 +18,7 @@
from .schema import model_schema
from .types import PyObject, StrBytes
from .typing import AnyCallable, AnyType, ForwardRef, is_classvar, resolve_annotations, update_field_forward_refs
from .utils import GetterDict, ValueItems, truncate, validate_field_name
from .utils import GetterDict, ValueItems, lenient_issubclass, truncate, validate_field_name

if TYPE_CHECKING:
from .class_validators import ValidatorListDict
Expand Down Expand Up @@ -175,7 +175,11 @@ def __new__(mcs, name, bases, namespace, **kwargs):
elif is_valid_field(ann_name):
validate_field_name(bases, ann_name)
value = namespace.get(ann_name, ...)
if isinstance(value, untouched_types) and ann_type != PyObject:
if (
isinstance(value, untouched_types)
and ann_type != PyObject
and not lenient_issubclass(getattr(ann_type, '__origin__', None), Type)
):
continue
fields[ann_name] = ModelField.infer(
name=ann_name,
Expand Down
18 changes: 11 additions & 7 deletions tests/test_edge_cases.py
@@ -1,7 +1,7 @@
import re
from decimal import Decimal
from enum import Enum
from typing import Any, Dict, List, Optional, Set, Tuple, Union
from typing import Any, Dict, List, Optional, Set, Tuple, Type, Union

import pytest

Expand Down Expand Up @@ -957,15 +957,19 @@ def __init__(self, **data) -> None:
Foobar(x=1)


def test_ignored_type():
def foobar():
def test_type_on_annotation():
class FooBar:
pass

class Model(BaseModel):
a: int = foobar
b: int

assert Model.__fields__.keys() == {'b'}
a: int = int
b: Type[int]
c: Type[int] = int
d: FooBar = FooBar
e: Type[FooBar]
f: Type[FooBar] = FooBar

assert Model.__fields__.keys() == {'b', 'c', 'e', 'f'}
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved


def test_optional_subfields():
Expand Down