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

Serializing a set with default values creates a TypeError #4136

Closed
LordFckHelmchen opened this issue Jun 3, 2022 Discussed in #4053 · 0 comments · Fixed by #4155
Closed

Serializing a set with default values creates a TypeError #4136

LordFckHelmchen opened this issue Jun 3, 2022 Discussed in #4053 · 0 comments · Fixed by #4155

Comments

@LordFckHelmchen
Copy link

Discussed in #4053

Originally posted by LordFckHelmchen May 9, 2022
Dear Pydantic devs.

When I have pydantic class that contains an Set field (non-serializable), with default value, I get an TypeError: Object of type set is not JSON serializable when using json.dump. However, when removing the default value, everything works and the generated schema correctly uses the array type for the Set.
The error can also be solved by using the pydantic encoder in the dump call. Then also the default value is correctly typed.

I'm not sure if this is an issue or intended - therefore tried in the discussion section first (but it might also be related to issue #2470).

Thanks for any ideas!

Greetings

Setup

OS Linux (Kubuntu), x64
Python 3.9.0
pydantic 1.9.0

Minimal working examples

TypeError with default

Code

import json
from pydantic import BaseModel

class Test(BaseModel):
    x = {1}

print(json.dumps(Test.schema(), indent=2))

Result

Traceback (most recent call last):
  File "/usr/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 8, in <module>
  File "/usr/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.9/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/usr/lib/python3.9/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.9/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/usr/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable

No error without default

Code

import json
from pydantic import BaseModel
from typing import Set
class Test(BaseModel):
    x: Set[int]

print(json.dumps(Test.schema(), indent=2))

Result

{
  "title": "Test",
  "type": "object",
  "properties": {
    "x": {
      "title": "X",
      "type": "array",
      "items": {
        "type": "integer"
      },
      "uniqueItems": true
    }
  },
  "required": [
    "x"
  ]
}

No error with pydantic encoder

Code

import json
from pydantic import BaseModel
from pydantic.json import pydantic_encoder
from typing import Set
class Test(BaseModel):
    x = {2}

print(json.dumps(Test.schema(), indent=2, default=pydantic_encoder))

Result

{
  "title": "Test",
  "type": "object",
  "properties": {
    "x": {
      "title": "X",
      "default": [
        2
      ],
      "type": "array",
      "items": {},
      "uniqueItems": true
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant