Skip to content

An empty string is a valid JSON attribute name #4253

@SergeyTsaplin

Description

@SergeyTsaplin

Checks

  • [ x] I added a descriptive title to this issue
  • [ x] I have searched (google, github) for similar issues and couldn't find anything
  • [ x] 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.9.0
            pydantic compiled: False
                 install path: /Users/s_tsaplin/projects/github.com/sergeytsaplin/pydantic/pydantic
               python version: 3.9.13 (main, May 19 2022, 14:10:54)  [Clang 13.1.6 (clang-1316.0.21.2)]
                     platform: macOS-12.4-x86_64-i386-64bit
     optional deps. installed: ['devtools', 'dotenv', 'email-validator', 'typing-extensions']
from pydantic import BaseModel, Field

class Model(BaseModel):
        empty_string_key: int = Field(alias='')

data = {'': 123}
m = Model(**data)
m.dict(by_alias=True)

Current behavior:

The code causes the exception:

Traceback (most recent call last):
  File "<masked>/.pyenv/versions/3.10.1/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Model
empty_string_key
  field required (type=value_error.missing)

Expected result:

The code should print:

{'': 123}

Links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions