datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Imported annotations break python type inspections

Open carstencodes opened this issue 1 year ago • 4 comments
trafficstars

Describe the bug

After digging a bit, I found a strange behavior, which could be related to #270:

I generate my code like this.

As the target version is not PY_36, in any case the line

from __future__ import annotations

will be added. If I remove the line, everything will then work fine. Otherwise, it will go rogue.

To Reproduce

Example schema

Used commandline:

$ datamodel-codegen --input schema/build-info.schema.json --custom-file-header-path .licenseheader --output src/buildinfo_om/_model.py --output-model-type dataclasses.dataclass --enum-field-as-literal all --field-constraints --set-default-enum-member --strict-types str bytes int float bool --use-annotated --use-generic-container-types --use-non-positive-negative-number-constrained-types --use-double-quotes --use-standard-collections --use-subclass-enum --use-union-operator --capitalise-enum-members --use-default-kwarg --use-field-description --disable-appending-item-suffix --enable-version-header --target-python-version 3.11 --use-schema-description --use-title-as-name --no-color --input-file-type jsonschema

Expected behavior

I can reflect on the datatypes with python. This actually does not work.

from dataclasses import fields
from typing import get_options, get_args
from ._model import Agent

name = fields(Agent)[0]
print (get_origin(name.type))
print (get_args(name.type))

Expected:

<class 'types.UnionType'>
(<class 'str'>, <class 'NoneType'>)

Actual:

None
()

If I drop the line from the resulting code, inspection works again properly...

Version:

  • OS: Linux x64
  • Python version: 3.12.0
  • datamodel-code-generator version: 0.25.3

Additional context Add any other context about the problem here.

carstencodes avatar Feb 05 '24 19:02 carstencodes

@carstencodes Thank you for creating the issue. Just to confirm, this is not a bug, just not in your use case, right? If so, how about adding an option to disable feature annotation import?

koxudaxi avatar Mar 16 '24 17:03 koxudaxi

Hi @koxudaxi

The issue can be easily reproduced with any dataclass in Python 3.9 or later:


from __future__ import annotations

from dataclasses import dataclass, field, fields
from typing import get_options, get_args

@dataclass
class Agent:
    name: str = field()

name = fields(Agent)[0]
print (get_origin(name.type))
print (get_args(name.type))

If I take a look parser/base.py, this import is only added, if the Python Version is not Python 3.6. The change was made 4-5 years ago, when Python 3.6 was still in maintenance: https://endoflife.date/python

I am wondering, if this is still legit. It looks like, this is from a pre-PY3.6 era.

carstencodes avatar Mar 24 '24 20:03 carstencodes

annotation import

It would be great to add this option, it doesn't seem to do anything in the latest python version

lazyhope avatar Mar 27 '24 03:03 lazyhope

Hi @koxudaxi ,

could you please remove these annotations or add an opt-out flag for the users in one of the next versions?

Regads

Carsten

carstencodes avatar Aug 07 '24 17:08 carstencodes