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

Make xfailing test for root model extra stop xfailing #6937

Merged
merged 1 commit into from Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion docs/errors/usage_errors.md
Expand Up @@ -900,7 +900,7 @@ except PydanticUserError as exc_info:
assert exc_info.code == 'invalid_annotated_type'
```

## `config` is unused with TypeAdapter {#type-adapter-config-unused}
## `config` is unused with `TypeAdapter` {#type-adapter-config-unused}

You will get this error if you try to pass `config` to `TypeAdapter` when the type is a type that
has its own config that cannot be overridden (currently this is only `BaseModel`, `TypedDict` and `dataclass`):
Expand Down Expand Up @@ -939,4 +939,23 @@ class MyTypedDict(TypedDict):
TypeAdapter(MyTypedDict) # ok
```

## Cannot specify `model_config['extra']` with `RootModel` {#root-model-extra}

Because `RootModel` is not capable of storing or even accepting extra fields during initialization, we raise an error
if you try to specify a value for the config setting `'extra'` when creating a subclass of `RootModel`:

```py
from pydantic import PydanticUserError, RootModel

try:

class MyRootModel(RootModel):
model_config = {'extra': 'allow'}
root: int

except PydanticUserError as exc_info:
assert exc_info.code == 'root-model-extra'
```


{% endraw %}
1 change: 1 addition & 0 deletions pydantic/errors.py
Expand Up @@ -56,6 +56,7 @@
'multiple-field-serializers',
'invalid_annotated_type',
'type-adapter-config-unused',
'root-model-extra',
]


Expand Down
9 changes: 9 additions & 0 deletions pydantic/root_model.py
Expand Up @@ -7,6 +7,7 @@

from pydantic_core import PydanticUndefined

from . import PydanticUserError
from ._internal import _repr
from .main import BaseModel, _object_setattr

Expand Down Expand Up @@ -43,6 +44,14 @@ class RootModel(BaseModel, typing.Generic[RootModelRootType]):

root: RootModelRootType

def __init_subclass__(cls, **kwargs):
extra = cls.model_config.get('extra')
if extra is not None:
raise PydanticUserError(
"`RootModel` does not support setting `model_config['extra']`", code='root-model-extra'
)
super().__init_subclass__(**kwargs)

def __init__(__pydantic_self__, root: RootModelRootType = PydanticUndefined, **data) -> None: # type: ignore
__tracebackhide__ = True
if data:
Expand Down
1 change: 0 additions & 1 deletion tests/test_root_model.py
Expand Up @@ -344,7 +344,6 @@ class B(BaseModel):
assert B(root=42) != R(42)


@pytest.mark.xfail(reason='TODO: raise error for `extra` with `RootModel`')
@pytest.mark.parametrize('extra_value', ['ignore', 'allow', 'forbid'])
def test_extra_error(extra_value):
with pytest.raises(PydanticUserError, match='extra'):
Expand Down