Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"none is not an allowed value" when using Union with Any #3444

Closed
3 tasks done
tharradine opened this issue Nov 25, 2021 · 2 comments · Fixed by #3452
Closed
3 tasks done

"none is not an allowed value" when using Union with Any #3444

tharradine opened this issue Nov 25, 2021 · 2 comments · Fixed by #3452
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@tharradine
Copy link
Contributor

tharradine commented Nov 25, 2021

Checks

  • I added a descriptive title to this issue
  • 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())":

             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: C:\Users\TobyHarradine\PycharmProjects\orchestration\.venv\Lib\site-packages\pydantic
               python version: 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
                     platform: Windows-10-10.0.19041-SP0
     optional deps. installed: ['typing-extensions']

Code which reproduces the issue:

>>> import pydantic
>>> class M(pydantic.BaseModel):
...     a: Any
...
>>> M(a=1)
M(a=1)
>>> M(a=None)
M(a=None)
>>> class M(pydantic.BaseModel):
...     a: Union[int, Any]
...
>>> M(a=1)
M(a=1)
>>> M(a="abcd")
M(a='abcd')
>>> M(a=None)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "pydantic\main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for M
a
  none is not an allowed value (type=type_error.none.not_allowed)

This may be somewhat related to #1624.

@tharradine tharradine added the bug V1 Bug related to Pydantic V1.X label Nov 25, 2021
@mykhailoleskiv
Copy link

mykhailoleskiv commented Nov 25, 2021

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_)

@tharradine
Copy link
Contributor Author

@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

tharradine added a commit to tharradine/pydantic that referenced this issue Nov 25, 2021
tharradine added a commit to tharradine/pydantic that referenced this issue Nov 25, 2021
PrettyWood added a commit to tharradine/pydantic that referenced this issue Dec 8, 2021
samuelcolvin pushed a commit that referenced this issue Dec 10, 2021
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants