Skip to content

Corrupted import statements in generated Pydantic classes #2984

@rhfogh

Description

@rhfogh

Import statements in some specific contexts are replaced with wrong file names.
The error is absent in version 0.37.0 but appears in 0.38.0 and subsequent version up to 0.53.0

I could not reduce this to a reproducible example, it being part of a huge multifile structure, but the examples below at least show the problem, which might be enough given that it is known when the problem appeared?

The project directory structure is

.../mxlims/
    schemas/
        data/
        references/
        messages/
        objects/

With relevant files

mesages/MsgTop.json

{
    "$schema": "https://json-schema.org/draft-07/schema",
    "title": "MsgTop",
    "type": "object",
    "allOf": [
        {
            "$ref": "BaseMessageData.json"
        }
    ],
    "properties": {
        "Plate": {
            "description": "idString:object dictionary of Plates.",
            "title": "Plates",
            "type": "object",
            "minProperties": 1,
            "additionalProperties": {
                "$ref": "../objects/Plate.json"
            }
        },
        "PlateWell": {
            "description": "idString:object dictionary of PlateWells.",
            "title": "PlateWells",
            "type": "object",
            "minProperties": 1,
            "additionalProperties": {
                "$ref": "../objects/PlateWell.json"
            }
        }
    }
}

objects/Plate.json

{
    "$schema": "https://json-schema.org/draft-07/schema",
    "description": "A crystallization plate, with typed JSON containment lists",
    "title": "Plate",
    "type": "object",
    "allOf": [
        {
            "$ref": "../data/PlateData.json"
        },
        {
            "$ref": "../data/LogisticalSampleData.json"
        }
    ],
    "properties": {
        "mxlimsType": {
            "const": "Plate",
            "description": "The type of MXLIMS object.",
            "title": "MxlimsType",
            "type": "string"
        },
        "containerRef": {
            "allOf": [
                {
                    "reverseLinkName": "contents"
                },
                {
                    "$ref": "../references/ShipmentRef.json"
                }
            ]
        }
    }
}

objects/PlateWell.json

{
    "$schema": "https://json-schema.org/draft-07/schema",
    "description": "A well in a crystallization plate, with typed JSON containment lists",
    "title": "PlateWell",
    "type": "object",
    "allOf": [
        {
            "$ref": "../data/PlateWellData.json"
        },
        {
            "$ref": "../data/LogisticalSampleData.json"
        }
    ],
    "properties": {
        "mxlimsType": {
            "const": "PlateWell",
            "description": "The type of MXLIMS object.",
            "title": "MxlimsType",
            "type": "string"
        },
        "sampleRef": {
            "allOf": [
                {
                    "reverseLinkName": "logisticalSamples"
                },
                {
                    "$ref": "../references/MacromoleculeSampleRef.json"
                }
            ]
        },
        "containerRef": {
            "allOf": [
                {
                    "reverseLinkName": "contents"
                },
                {
                    "$ref": "../references/PlateRef.json"
                }
            ]
        }
    }
}

Incorrect result (version 0.38.0)

pydantic/messages/MsgTop.py

# generated by datamodel-codegen:
#   filename:  messages/MsgTop.json

from __future__ import annotations

from typing import Dict, Optional

from pydantic import Field

from ..objects.Plate import Plate
from ..objects.PlateWell import LogisticalSampleData
from .BaseMessageData import BaseMessageData


class MsgTop(BaseMessageData):
    plate: Optional[Dict[str, Plate]] = Field(
        None,
        alias="Plate",
        description="idString:object dictionary of Plates.",
        title="Plates",
    )
    plate_well: Optional[Dict[str, LogisticalSampleData]] = Field(
        None,
        alias="PlateWell",
        description="idString:object dictionary of PlateWells.",
        title="PlateWells",
    )

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/schemas --output mxlims/pydantic 

Under Python 3.10 in a conda environment under OpenSUSE 15.6

The correct result, as obtained with version 0.37.0 is

pydantic/messages.MsgTop.py

# generated by datamodel-codegen:
#   filename:  messages/MsgTop.json

from __future__ import annotations

from typing import Dict, Optional

from pydantic import Field

from ..objects.Plate import Plate
from ..objects.PlateWell import PlateWell
from .BaseMessageData import BaseMessageData


class MsgTop(BaseMessageData):
    plate: Optional[Dict[str, Plate]] = Field(
        None,
        alias="Plate",
        description="idString:object dictionary of Plates.",
        title="Plates",
    )
    plate_well: Optional[Dict[str, PlateWell]] = Field(
        None,
        alias="PlateWell",
        description="idString:object dictionary of PlateWells.",
        title="PlateWells",
    )

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