You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched (google, github) for similar issues and couldn't find anything
I have read and followed the docs and still think this is a bug
Bug
It seems that None is allowed when a field's type is Any (as is expected, stated in the docs), but None is not allowed when a field's type is Union[SomeType, Any].
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
allow_none attribute for field a remains False. Possible fix may be in pydantic/fields.py:566:
if is_union_origin(origin):
types_ = []
for type_ in get_args(self.type_):
if type_ is NoneType or type_ is Any or type_ is object:
if self.required is Undefined:
self.required = False
self.allow_none = True
continue
types_.append(type_)
@mykhailoleskiv That almost works, except it doesn't add Any or object to types_ (which eventually becomes ModelField.sub_fields). So M(a="abcd") or something like that no longer works. I'll open a PR
* Add unit test for Union[int, Any]
* Allow None when Any or object is in Union
Resolves#3444
* Add changelog entry for #3444
* Prefer `is_none_type()` over `type_ is NoneType`
* fix(lint): remove useless import
Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Checks
Bug
It seems that
None
is allowed when a field's type isAny
(as is expected, stated in the docs), butNone
is not allowed when a field's type isUnion[SomeType, Any]
.Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Code which reproduces the issue:
This may be somewhat related to #1624.
The text was updated successfully, but these errors were encountered: