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

Error when creating model with keys starting with a number #169

Closed
fzirker opened this issue Jul 7, 2020 · 2 comments
Closed

Error when creating model with keys starting with a number #169

fzirker opened this issue Jul 7, 2020 · 2 comments
Labels

Comments

@fzirker
Copy link

fzirker commented Jul 7, 2020

Its me again and I have a new crazy JSON here.
Sometimes I have JSON which contains a key starting with a number.

To Reproduce
Example JSON:

{
    "Values": {
        "1 Step": "42",
        "2 Step": "23"
    }
}

Used commandline:

$ datamodel-codegen --input jsons/numbers.json --input-file-type json --output models/numbers.py --target-python-version 3.6

Expected behavior
Model is created. For the keys starting with a number there should be a underscore "_" added at the beginning of the string.

Current behavior
An error is thrown:

Traceback (most recent call last):
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/datamodel_code_generator/__main__.py", line 122, in main
    validation=namespace.validation,
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/datamodel_code_generator/__init__.py", line 179, in generate
    result = parser.parse()
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/datamodel_code_generator/parser/base.py", line 317, in parse
    body = format_code(body, self.target_python_version)
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/datamodel_code_generator/format.py", line 26, in format_code
    code = apply_black(code, python_version)
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/datamodel_code_generator/format.py", line 46, in apply_black
    string_normalization=not config.get("skip-string-normalization", True),
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/black.py", line 725, in format_str
    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
  File "/home/flz/.local/share/virtualenvs/modelCreate-nvqMaMoH/lib/python3.6/site-packages/black.py", line 836, in lib2to3_parse
    raise exc from None
black.InvalidInput: Cannot parse: 5:5:     1_Step: str = Field(..., alias='1 Step')

Version:

  • OS: Ubuntu 20.04
  • Python Verison: 3.6.11
  • datamodel-code-generator Version 0.5.13

Additional context
I think in Python variable names must not start with a number.

@koxudaxi
Copy link
Owner

koxudaxi commented Jul 9, 2020

@fzirker
Thank you for your report.
Sorry, I forgot about this pattern.
I will fix it.

@koxudaxi
Copy link
Owner

@fzirker
I have fixed it and released a new version as 0.5.16

Model is created. For the keys starting with a number there should be a underscore "_" added at the beginning of the string.

pydantic model doesn't accept a filed which has an underscore "_" in first.
the code-generator adds field_ to the head of the field name.

class Values(BaseModel):
field_1_Step: str = Field(..., alias='1 Step')
field_2_Step: str = Field(..., alias='2 Step')

If you have more good idea then, please tell me it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants