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

Different behaviour of Config for pydantic model when Config is inherited and when Config is passed through dataclass decorator #1081

Closed
carmenere opened this issue Dec 5, 2019 · 1 comment

Comments

@carmenere
Copy link

  • OS: Ubuntu 19.04
  • Python version import sys; print(sys.version): 3.7.3 (default, Oct 7 2019, 12:56:13) [GCC 8.3.0]
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.2

1. When Config is passed through dataclass decorator

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'

2. When Config is inherited

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

ValidationError: 1 validation error for XYZ 
garbage 
  extra fields not permitted (type=value_error.extra)
@carmenere carmenere added the bug V1 Bug related to Pydantic V1.X label Dec 5, 2019
@samuelcolvin
Copy link
Member

This is standard behaviour of dataclasses, not something pydantic can change, see #986 for details.

@samuelcolvin samuelcolvin added duplicate and removed bug V1 Bug related to Pydantic V1.X labels Dec 5, 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
Projects
None yet
Development

No branches or pull requests

2 participants