We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
import sys; print(sys.version)
import pydantic; print(pydantic.VERSION)
from typing import ( Optional, List, Dict, Tuple, Union, # Final ) from pydantic import ( BaseModel, Field, ValidationError, validator, root_validator, Extra ) from pydantic.dataclasses import dataclass class ModelConfig: extra = Extra.forbid @dataclass(config=ModelConfig) class ABC(BaseModel): a: Optional[int] b: Optional[int] c: Optional[int]
Call ABC(**{"a": 0}) returns TypeError: __init__() missing 2 required positional arguments: 'b' and 'c' Call ABC(**{"a": 0, "b": 0, "c": 0}) returns AttributeError: __fields_set__ Call ABC(**{"a": 0, "b": 0, "c": 0, "garbage": 0}) returns TypeError: __init__() got an unexpected keyword argument 'garbage'
ABC(**{"a": 0})
TypeError: __init__() missing 2 required positional arguments: 'b' and 'c'
ABC(**{"a": 0, "b": 0, "c": 0})
AttributeError: __fields_set__
ABC(**{"a": 0, "b": 0, "c": 0, "garbage": 0})
TypeError: __init__() got an unexpected keyword argument 'garbage'
class BaseModelConfig(BaseModel): class Config: extra = Extra.forbid class XYZ(BaseModelConfig): x: Optional[int] y: Optional[int] z: Optional[int]
Call XYZ(**{"x": 0}) returns right object: XYZ(x=0, y=None, z=None) Call XYZ(**{"x": 0, "y": 0, "z": 0}) returns right object: XYZ(x=0, y=0, z=0) Call XYZ(**{"x": 0, "y": 0, "z": 0, "garbage": 0}) returns
XYZ(**{"x": 0})
XYZ(x=0, y=None, z=None)
XYZ(**{"x": 0, "y": 0, "z": 0})
XYZ(x=0, y=0, z=0)
XYZ(**{"x": 0, "y": 0, "z": 0, "garbage": 0})
ValidationError: 1 validation error for XYZ garbage extra fields not permitted (type=value_error.extra)
The text was updated successfully, but these errors were encountered:
This is standard behaviour of dataclasses, not something pydantic can change, see #986 for details.
Sorry, something went wrong.
uprev: 2.14.4 (pydantic#1081)
223aa27
No branches or pull requests
import sys; print(sys.version)
: 3.7.3 (default, Oct 7 2019, 12:56:13) [GCC 8.3.0]import pydantic; print(pydantic.VERSION)
: 1.21. When Config is passed through dataclass decorator
Call
ABC(**{"a": 0})
returnsTypeError: __init__() missing 2 required positional arguments: 'b' and 'c'
Call
ABC(**{"a": 0, "b": 0, "c": 0})
returnsAttributeError: __fields_set__
Call
ABC(**{"a": 0, "b": 0, "c": 0, "garbage": 0})
returnsTypeError: __init__() got an unexpected keyword argument 'garbage'
2. When Config is inherited
Call
XYZ(**{"x": 0})
returns right object:XYZ(x=0, y=None, z=None)
Call
XYZ(**{"x": 0, "y": 0, "z": 0})
returns right object:XYZ(x=0, y=0, z=0)
Call
XYZ(**{"x": 0, "y": 0, "z": 0, "garbage": 0})
returnsThe text was updated successfully, but these errors were encountered: