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

Object of type 'NameEmail' is not JSON serializable #2341

Closed
3 tasks done
gjcarneiro opened this issue Feb 11, 2021 · 4 comments · Fixed by #2479
Closed
3 tasks done

Object of type 'NameEmail' is not JSON serializable #2341

gjcarneiro opened this issue Feb 11, 2021 · 4 comments · Fixed by #2479
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@gjcarneiro
Copy link

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

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.7.3
            pydantic compiled: True
                 install path: /app/env/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
                     platform: Linux-5.8.0-43-generic-x86_64-with-glibc2.29
     optional deps. installed: ['typing-extensions', 'email-validator']

This issue was mentioned in #462, but was only solved for the case of SecretStr, NameEmail was left unsolved.

import pydantic

class MyModel(pydantic.BaseModel):
    email: pydantic.NameEmail

MyModel(email="foo <zzz@bar.com>").json()

I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pydantic/main.py", line 506, in pydantic.main.BaseModel.json
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "pydantic/json.py", line 65, in pydantic.json.pydantic_encoder
TypeError: Object of type 'NameEmail' is not JSON serializable
@gjcarneiro gjcarneiro added the bug V1 Bug related to Pydantic V1.X label Feb 11, 2021
@samuelcolvin
Copy link
Member

Thanks for reporting. Amazing this has never come up before.

Should be relatively simple to fix in pydantic/json.py, the only tricky thing is dealing with conditional imports.

@alecgerona
Copy link
Contributor

@samuelcolvin hey I'm trying to fix this bug and I'm encountering the tricky conditional imports thing. Would it be easier to move NameEmail to pydantic/types.py like SecretStr?

@samuelcolvin
Copy link
Member

Humm, I would prefer not to.

What is the problem? network.py shouldn't import json.py

@alecgerona
Copy link
Contributor

I got it to work. @gjcarneiro @samuelcolvin my PR's ready. #2479

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.

3 participants