Skip to content

Relative imports from parallel directories incorrect #2979

@rhfogh

Description

@rhfogh

JSON schemas that with a previous version of datamodel-codegenerator (maybe 6 months+ ago - precise version not known) produced the following correct import:
from ..datatypes.FooStats import FooStats

now (version 0.53.0) produces duplicate incorrect imports:

from ..datatypes import FooStats as FooStats_1
from ..datatypes.FooStats.FooStats import FooStats

Testing using the files given below shows that version 0.42.0 gives the correct import statement, whereas versions 0.43.0, 0.44.0 and 0.45.0 gives the following statement instead
from ..datatypes.FooStats.FooStats import FooStats

The following directory structure:
...mxlims/schematst/data/ ReflectionSetData.json:

{
    "$schema": "https://json-schema.org/draft-07/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-07/schema",
    "title": "FooStats",
    "type": "object"
}

Run with the command
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

Produced the following 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 import FooStats as FooStats_1
from ..datatypes.FooStats.FooStats import FooStats


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"
    )

and

...mxlims/pydantictst/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 behaviour is the restoration of the correct import syntax:

from ..datatypes.FooStats import FooStats

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions