Swagger-codegen: [bug][python][model] Should not import self

Created on 31 Jan 2018  路  8Comments  路  Source: swagger-api/swagger-codegen

Description

image

Swagger-codegen version

2.3.1

Swagger declaration file content or url
"PlacementNode": {
    "type": "object",
    "title": "PlacementNode",
    "properties": {
        "create": {
            "type": "string",
            "format": "date-time"
        },
        "id": {
            "type": "integer",
            "format": "int64"
        },
        "name": {
            "type": "string"
        },
        "parent": {
            "$ref": "#/definitions/PlacementNode"
        },
        "type": {
            "type": "string"
        },
        "update": {
            "type": "string",
            "format": "date-time"
        }
    }
}
Command line used for generation

generate -l python

Most helpful comment

We currently have data models that use other data types as part of their definition. We are seeing circular imports in this case.

If datatype A uses data type B as a part of one variable type. The import for datatype B is present in the datatype A file (not needed as the type is used only as string or doc string). But then datatype B imports datatype A (causing circular import - again not needed)

I have to manually comment out one of these import statements before my auto-generated client works without an import error!

Is this currently being worked on?

All 8 comments

ran into this too... did not occur in v2.2.3

Seeing this as well on 2.3.0. Maybe related to #6839? (looking at the generated # noqa: F401,E501 comments introduced there).

Same issue....had to manually remove one of the circular references before building.

In my opinion we should remove these imports from models. It makes a lot of problems with circular dependencies between 2 or more models (model A has an attribute with model B and model B has a optional attribute with model A). These imported objects are not used at all and we know about it - we added F401 to disable warning from pylint :/

Didn't have time to make any contributions to the referenced PR, but I made a gist that can be used to comment out the problematic lines after the code is generated. Hopefully this can be useful for those still dealing with this problem.

https://gist.github.com/macks22/a087cce914090b4a5d3a16e8ab3c28aa

In my opinion we should remove these imports from models. It makes a lot of problems with circular dependencies between 2 or more models (model A has an attribute with model B and model B has a optional attribute with model A).

YES

Please remove the imports for parent/child referenced types as they do in fact cause runtime errors from circular imports!
PLEASE

We currently have data models that use other data types as part of their definition. We are seeing circular imports in this case.

If datatype A uses data type B as a part of one variable type. The import for datatype B is present in the datatype A file (not needed as the type is used only as string or doc string). But then datatype B imports datatype A (causing circular import - again not needed)

I have to manually comment out one of these import statements before my auto-generated client works without an import error!

Is this currently being worked on?

Was this page helpful?
0 / 5 - 0 ratings