mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Allow passing json_encoders in class kwargs (#2523)
* Test that `json_encoders` are respected from class kwargs * Allow passing `json_encoders` in class kwargs Fixes #2521.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Allow passing `json_encoders` in class kwargs
|
||||
@@ -184,6 +184,7 @@ def inherit_config(self_config: 'ConfigType', parent_config: 'ConfigType', **nam
|
||||
namespace['json_encoders'] = {
|
||||
**getattr(parent_config, 'json_encoders', {}),
|
||||
**getattr(self_config, 'json_encoders', {}),
|
||||
**namespace.get('json_encoders', {}),
|
||||
}
|
||||
|
||||
return type('Config', base_classes, namespace)
|
||||
|
||||
@@ -1997,6 +1997,13 @@ def test_class_kwargs_config():
|
||||
assert Model.__fields__['b'].alias == 'B' # alias_generator still works
|
||||
|
||||
|
||||
def test_class_kwargs_config_json_encoders():
|
||||
class Model(BaseModel, json_encoders={int: str}):
|
||||
pass
|
||||
|
||||
assert Model.__config__.json_encoders == {int: str}
|
||||
|
||||
|
||||
def test_class_kwargs_config_and_attr_conflict():
|
||||
|
||||
with pytest.raises(
|
||||
|
||||
Reference in New Issue
Block a user