8000 Allow config and bases to be specified together in `create_model()` · pydantic/pydantic@7b3f513 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b3f513

Browse files
committed
Allow config and bases to be specified together in create_model()
The limitation was added back in the early days of Pydantic, when `create_model()` was implemented. It is no longer necessary. Backport of: #11714
1 parent fc52138 commit 7b3f513

File tree

4 files changed

+15
-31
lines changed

4 files changed

+15
-31
lines changed

docs/errors/usage_errors.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -631,23 +631,6 @@ except PydanticUserError as exc_info:
631631

632632
The fields definition syntax can be found in the [dynamic model creation](../concepts/models.md#dynamic-model-creation) documentation.
633633

634-
635-
## `create_model` config base {#create-model-config-base}
636-
637-
This error is raised when you use both `__config__` and `__base__` together in `create_model`.
638-
639-
```python
640-
from pydantic import BaseModel, ConfigDict, PydanticUserError, create_model
641-
642-
try:
643-
config = ConfigDict(frozen=True)
644-
model = create_model(
645-
'FooModel', foo=(int, ...), __config__=config, __base__=BaseModel
646-
)
647-
except PydanticUserError as exc_info:
648-
assert exc_info.code == 'create-model-config-base'
649-
```
650-
651634
## Validator with no fields {#validator-no-fields}
652635

653636
This error is raised when you use validator bare (with no fields).

pydantic/errors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
'schema-for-unknown-type',
5151
'import-error',
5252
'create-model-field-definitions',
53-
'create-model-config-base',
5453
'validator-no-fields',
5554
'validator-invalid-fields',
5655
'validator-instance-method',

pydantic/main.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,16 +1716,10 @@ def create_model( # noqa: C901
17161716
Raises:
17171717
PydanticUserError: If `__base__` and `__config__` are both passed.
17181718
"""
1719-
if __base__ is not None:
1720-
if __config__ is not None:
1721-
raise PydanticUserError(
1722-
'to avoid confusion `__config__` and `__base__` cannot be used together',
1723-
code='create-model-config-base',
1724-
)
1725-
if not isinstance(__base__, tuple):
1726-
__base__ = (__base__,)
1727-
else:
1719+
if __base__ is None:
17281720
__base__ = (cast('type[ModelT]', BaseModel),)
1721+
elif not isinstance(__base__, tuple):
1722+
__base__ = (__base__,)
17291723

17301724
__cls_kwargs__ = __cls_kwargs__ or {}
17311725

@@ -1757,7 +1751,7 @@ def create_model( # noqa: C901
17571751
namespace.update(__validators__)
17581752
namespace.update(fields)
17591753
if __config__:
1760-
namespace['model_config'] = _config.ConfigWrapper(__config__).config_dict
1754+
namespace['model_config'] = __config__
17611755
resolved_bases = types.resolve_bases(__base__)
17621756
meta, ns, kwds = types.prepare_class(model_name, resolved_bases, kwds=__cls_kwargs__)
17631757
if resolved_bases is not __base__:

tests/test_create_model.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
PydanticUserError,
1313
ValidationError,
1414
create_model,
15-
errors,
1615
field_validator,
1716
validator,
1817
)
@@ -138,8 +137,17 @@ def test_create_model_must_not_reset_parent_namespace():
138137

139138

140139
def test_config_and_base():
141-
with pytest.raises(errors.PydanticUserError):
142-
create_model('FooModel', __config__=BaseModel.model_config, __base__=BaseModel)
140+
class Base(BaseModel):
141+
a: str
142+
143+
model_config = {'str_to_lower': True}
144+
145+
Model = create_model('Model', __base__=Base, __config__={'str_max_length': 3})
146+
147+
assert Model(a='AAA').a == 'aaa'
148+
149+
with pytest.raises(ValidationError):
150+
Model(a='AAAA')
143151

144152

145153
def test_inheritance():

0 commit comments

Comments
 (0)
0