Skip to content

Self referencing classes leading to undefined-name (F821) #2973

@aschamberger

Description

@aschamberger

Describe the bug
Undefined name ISA95PropertyDataType for subproperties. The ISA95PropertyDataType should be replaced by Self

To Reproduce

Example schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "ISA95PropertyDataType": {
      "type": "object",
      "properties": {
        "ID": {
          "type": "string",
          "description": "Unique identifier for a property within the scope of the associated resource"
        },
        "Value": {
          "description": "Value for the property"
        },
        "Description": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/LocalizedText"
          },
          "description": "An optional description of the parameter."
        },
        "EngineeringUnits": {
          "$ref": "#/$defs/EUInformation",
          "description": "The Unit Of Measure of the value"
        },
        "Subproperties": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ISA95PropertyDataType"
          }
        }
      },
      "description": "A subtype of OPC UA Structure that defines two linked data items: an ID, which is a unique identifier for a property within the scope of the associated resource, and the value, which is the data for the property.\nhttps://reference.opcfoundation.org/ISA95JOBCONTROL/v200/docs/7.3.9"
    }
}

Result:

@dataclass(kw_only=True)
class ISA95PropertyDataType:
    """
    A subtype of OPC UA Structure that defines two linked data items: an ID, which is a unique identifier for a property within the scope of the associated resource, and the value, which is the data for the property.
    https://reference.opcfoundation.org/ISA95JOBCONTROL/v200/docs/7.3.9
    """

    id: str | None = None
    """
    Unique identifier for a property within the scope of the associated resource
    """
    value: Any | None = None
    """
    Value for the property
    """
    description: list[LocalizedText] | None = None
    """
    An optional description of the parameter.
    """
    engineering_units: EUInformation | None = None
    """
    The Unit Of Measure of the value
    """
    subproperties: list[ISA95PropertyDataType] | None = None

Used commandline:

    data_model_types = get_data_model_types(
        DataModelType.DataclassesDataclass, target_python_version=PythonVersion.PY_314
    )
    config = JSONSchemaParserConfig(
        target_python_version=PythonVersion.PY_314,
        use_union_operator=True,
        use_standard_collections=True,
        formatters=[Formatter.RUFF_FORMAT, Formatter.RUFF_CHECK],
        custom_template_dir=pathlib.Path(__file__).parent / "template",
        data_model_type=data_model_types.data_model,
        data_model_root_type=data_model_types.root_model,
        data_model_field_type=data_model_types.field_model,
        data_type_manager_type=data_model_types.data_type_manager,
        dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
        keyword_only=True,
        use_subclass_enum=True,
        capitalise_enum_members=True,
        snake_case_field=True,
        use_field_description=True,
        use_schema_description=True,
        use_title_as_name=True,
        additional_imports=[
            "app.dataclass.DataClassConfig",
        ],
    )
    parser = JsonSchemaParser(schema_file_path.read_text(), config=config)
    result = parser.parse()

Expected behavior

    # ...
    subproperties: list[Self] | None = None

Version:

  • OS: [e.g. iOS] Linux
  • Python version: 3.14
  • datamodel-code-generator version: 53

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions