json2pyi icon indicating copy to clipboard operation
json2pyi copied to clipboard

keys with spaces not handled correctly

Open graingert opened this issue 4 years ago • 2 comments

json with spaces in the keys

{
    "example with space": 1
}

becomes:

from __future__ import annotations
from dataclasses import dataclass

@dataclass
class UnnammedType53F4C8:
    example with space: int

however this works fine with a TypedDict:

from typing import TypedDict

UnnammedType160988 = TypedDict("UnnammedType160988", {"example with space": int})

graingert avatar Dec 10 '21 17:12 graingert

Thanks for reporting.

But what is the expected output here? I suppose it is impossible to support attribute names with spaces in Python like this anyway. (The Pydantic BaseMode may support renaming/alias of field names, but the built-in dataclass could not.)

Gowee avatar Dec 10 '21 17:12 Gowee

I think the most pythonic approach to this problem would be to rename the attribute with snake_case naming

from __future__ import annotations
from pydantic import BaseModel

class UnnammedType160398(BaseModel):
    example_with_space: int

s-rigaud avatar Mar 23 '23 14:03 s-rigaud