8000 Config.json_loads is not used with BaseModel.parse_file · Issue #1067 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Config.json_loads is not used with BaseModel.parse_file #1067

@kierandarcy

Description

@kierandarcy

Bug

BaseModel.parse_file does not use the custom JSON decoder as specified in Config.json_loads.

Please complete:

  • OS: Ubuntu 18.04.3 LTS
  • Python version import sys; print(sys.version): 3.8.0 (default, Oct 28 2019, 16:14:01)
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.2

Please read the docs and search through issues to
confirm your bug hasn't already been reported.

Where possible please include a self contained code snippet describing your bug:

import json
import tempfile
from pydantic import BaseModel

def name_overwriting_decoder(*args, **kwargs):
    """A silly JSON decoder that hard codes a `name` value"""
    data = json.loads(*args, **kwargs)
    data['name'] = "Overwritten Name"
    return data

class Example(BaseModel):
    name = "Default Name"

    class Config:
        json_loads = name_overwriting_decoder

raw_example = Example.parse_raw(b'{"name": "Name To Overwrite"}')
assert raw_example.dict() == {"name": "Overwritten Name"}

# Create a temporary JSON file and write some JSON
tmp_file = tempfile.NamedTemporaryFile(delete=False)
tmp_file.write(b'{"name": "Name From Temp File"}')
tmp_file.close()

file_example = Example.parse_file(tmp_file.name)
assert file_example.dict() == {"name": "Overwritten Name"}

I would have expected Example.parse_file to use the same decoder as Example.parse_raw

FYI: I have a use-case where I parse a Hjson file with hjson.loads.

I am happy to produce a PR for this, if you think it is actually a bug :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.Xhelp wantedPull Request welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0