mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Fix dict with extra keys (#490)
* Fix dict with extra keys (#489), fix #488 * Fix PR number * Fix Config of MyModel not to inherit BaseConfig
This commit is contained in:
@@ -6,6 +6,7 @@ History
|
||||
v0.25 (unreleased)
|
||||
..................
|
||||
* Improve documentation on self-referencing models and annotations, #487 by @theenglishway
|
||||
* fix ``.dict()`` with extra keys, #490 by @JaewonKim
|
||||
|
||||
v0.24 (2019-04-23)
|
||||
..................
|
||||
|
||||
+1
-1
@@ -289,7 +289,7 @@ class BaseModel(metaclass=MetaModel):
|
||||
|
||||
def _get_key_factory(self, by_alias: bool) -> Callable[..., str]:
|
||||
if by_alias:
|
||||
return lambda fields, key: fields[key].alias
|
||||
return lambda fields, key: fields[key].alias if key in fields else key
|
||||
|
||||
return lambda _, key: key
|
||||
|
||||
|
||||
+13
-1
@@ -3,7 +3,7 @@ from typing import Any, ClassVar, List
|
||||
|
||||
import pytest
|
||||
|
||||
from pydantic import BaseModel, Extra, NoneBytes, NoneStr, Required, ValidationError, constr
|
||||
from pydantic import BaseModel, Extra, NoneBytes, NoneStr, Required, Schema, ValidationError, constr
|
||||
|
||||
|
||||
def test_success():
|
||||
@@ -576,3 +576,15 @@ def test_dir_fields():
|
||||
assert "json" in dir(m)
|
||||
assert "attribute_a" in dir(m)
|
||||
assert "attribute_b" in dir(m)
|
||||
|
||||
|
||||
def test_dict_with_extra_keys():
|
||||
class MyModel(BaseModel):
|
||||
a: str = Schema(None, alias='alias_a')
|
||||
|
||||
class Config:
|
||||
extra = Extra.allow
|
||||
|
||||
m = MyModel(extra_key='extra')
|
||||
assert m.dict() == {'a': None, 'extra_key': 'extra'}
|
||||
assert m.dict(by_alias=True) == {'alias_a': None, 'extra_key': 'extra'}
|
||||
|
||||
Reference in New Issue
Block a user