Skip to content

Commit

Permalink
Make xfailing test for root model extra stop xfailing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Aug 16, 2023
1 parent 325a5e2 commit f87019f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
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

0 comments on commit f87019f

Please sign in to comment.