Skip to content

Since d28bc12 we generate broken code if there is a field called field #781

Closed
@david-dotorigin

Description

@david-dotorigin

Describe the bug

Prior to d28bc12 the generated code says import attr afterwards the generated code says from attrs import define, field instead. This new import of field can be shadowed by a field called field. Resulting in generated code that makes Python complain:

some_dict: Dict[str, Any] = field(init=False, factory=dict)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'Unset' object is not callable

To Reproduce

Put the OpenAPI Spec File below into demo.json.

Put this Python into demo.py:

import sys
sys.path.append('demo-api-client')
from demo_api_client.models import *

Then give these commands:

openapi-python-client generate --path demo.json
demo.py

The response from demo.py is

Traceback (most recent call last):
  File "C:\projects\dot\openapi-python-client7\demo.py", line 4, in <module>
	from demo_api_client.models.get_user_response_200 import GetUserResponse200
  File "C:\projects\dot\openapi-python-client7\demo-api-client\demo_api_client\models\__init__.py", line 3, in <module>
	from .field_defs import FieldDefs
  File "C:\projects\dot\openapi-python-client7\demo-api-client\demo_api_client\models\field_defs.py", line 11, in <module>
	class FieldDefs:
  File "C:\projects\dot\openapi-python-client7\demo-api-client\demo_api_client\models\field_defs.py", line 18, in FieldDefs
	additional_properties: Dict[str, Any] = field(init=False, factory=dict)
											^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'Unset' object is not callable

Expected behavior
A field called field should continue to work.

OpenAPI Spec File

{
	"openapi": "3.0.3",
	"servers": [
		{
			"url": "https://localhost:8080",
			"description": "___"
		}
	],
	"info": {
		"title": "Demo API",
		"version": "1.1.1"
	},
	"paths": {
		"/user": {
			"get": {
				"description": "___",
				"responses": {
					"200": {
						"description": "___"
					}
				},
				"summary": "___",
				"tags": [
					"utility"
				]
			}
		}
	},
	"components": {
		"schemas": {
			"FieldDefs": {
				"type": "object",
				"properties": {
					"field": {
						"type": "string",
						"description": "___"
					}
				}
			}
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions