Skip to content

incorrect relative imports with --use-exact-imports and --collapse-root-models - bis #3001

@rhfogh

Description

@rhfogh

Thanks for the remarkably quick fix #2996 to issue #2979 - very much appreciated.
Regrettably it looks like the fix did not resolve all the issues and imports are still incorrect

Added later - 20260218:
It looks like the problem may be intermittent, maybe to do with things not always being done in the same order?
Anyway, of the two cases of this problem I had in my real code, one disappeared spontaneously in a later version with no changes to the source json file that could have made a difference. The other one is still there, though.

To Reproduce

mxlims/schematst/data/ReflectionSetData.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "ReflectionSetData",
    "type": "object",
    "properties": {
        "fooStatsOverall": {
            "allOf": [
                {
                    "$ref": "../datatypes/FooStats.json"
                }
            ]
        },
        "fooStatsShells": {
            "items": {
                "$ref": "../datatypes/FooStats.json"
            },
            "title": "Foo Stats Shells",
            "type": "array"
        }
    }
}

mxlims/schematst/datatypes/FooStats,.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "FooStats",
    "type": "object"
}

Used commandline:

datamodel-codegen --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --base-class mxlims.impl.MxlimsBase.BaseModel --use-schema-description --use-double-quotes --disable-timestamp --use-default --target-python-version 3.10 --snake-case-field --output-datetime-class datetime --use-exact-imports --capitalise-enum-members --use-title-as-name --use-one-literal-as-default --use-non-positive-negative-number-constrained-types --collapse-root-models --input mxlims/schematst --output mxlims/pydantictst

Produces the following pydantic files:

mxlims/pydantictst/data/ReflectionSetData.py

# generated by datamodel-codegen:
#   filename:  data/ReflectionSetData.json

from __future__ import annotations

from mxlims.impl.MxlimsBase import BaseModel
from pydantic import Field

from ..datatypes.FooStats import FooStats as FooStats_1


class ReflectionSetData(BaseModel):
    foo_stats_overall: FooStats | None = Field(None, alias="fooStatsOverall")
    foo_stats_shells: list[FooStats_1] | None = Field(
        None, alias="fooStatsShells", title="Foo Stats Shells"
    )

mxlims/schematst/datatypes/FooStats.py

# generated by datamodel-codegen:
#   filename:  datatypes/FooStats.json

from __future__ import annotations

from mxlims.impl.MxlimsBase import BaseModel


class FooStats(BaseModel):
    pass

Expected behavior
Whichever local name is used for the imported class, FooStats or FooStats_1, it should be used in both the relevant attributes.

Version:

  • OS: Linux OpenSUSE 15.6
  • Python version: 3.10
  • datamodel-code-generator version: 0.54.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions