Description
What are you generating using Kiota, clients or plugins?
API Client/SDK
In what context or format are you using Kiota?
Linux executable
Client library/SDK language
Python
Describe the bug
I am trying to generate a library for https://developer.sophos.com/docs/endpoint-v1/1/overview
What I'm finding is that the generated python is creating invalid dataclasses for query parameters that include enums. For example in the api yaml:
- in: query
name: healthStatus
description: Find endpoints by health status.
style: form
explode: false
required: false
schema:
type: array
items:
type: string
enum:
- bad
- good
- suspicious
- unknown
Creates a parameter in dataclass:
# Find endpoints by health status.
health_status: list[GetHealthStatusQueryParameterType] = []
Which causes the error:
Exception has occurred: ValueError
mutable default <class 'list'> for field health_status is not allowed: use default_factory
I don't really see a way to fix this without editing the generated code. I'm assuming it should be wrapped with Optional[] since it's an optional value.
Expected behavior
I should be able to run a get() without running into the "Exception has occurred: ValueError
mutable default <class 'list'> for field health_status is not allowed: use default_factory" Error.
How to reproduce
Generate for python using the attached yaml.
See endpoints/endpoints_request_builder.py
Open API description file
openapi-yaml-client-generated.zip
Kiota Version
1.24.1
Latest Kiota version known to work for scenario above?(Not required)
No response
Known Workarounds
No response
Configuration
- OS: MacOS
- Arm64
Debug output
Click to expand log
```</details>
### Other information
_No response_
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Activity
baywet commentedon Mar 31, 2025
Hi @ShawnSack
Thank you for using kiota and for reaching out.
Thank you for your patience. (the team is currently ongoing some re-organization, responses will be slow)
Do you think you could provide a snippet of what the code should be in your opinion?
JanHusarcik commentedon Apr 3, 2025
Hi team,
I'm having a similar experience with https://cloud.memsource.com/web/internal/v1/docs/latest/yaml:
results in
order: list[GetOrderQueryParameterType] = []
I have to manually edit toorder: list[GetOrderQueryParameterType] = field(default_factory=list)
for the generated client to work as expected.Thank you for looking into it.
baywet commentedon Apr 4, 2025
Thank you for the additional information.
Is this a behaviour that has changed in python? are the field and default factory symbols available all the way down to python 3.9?
This gets written here
kiota/src/Kiota.Builder/Writers/Python/CodePropertyWriter.cs
Line 43 in 6c20342
And a unit test should be implemented here to prevent further regressions.
kiota/tests/Kiota.Builder.Tests/Writers/Python/CodePropertyWriterTests.cs
Line 110 in 6c20342
Is this something you'd like to submit a pull request for provided some guidance?
JanHusarcik commentedon Apr 4, 2025
Thanks for raising this point, @baywet. I'm on Python 3.11.2.
Quickly checking the 3.9 docs, dataclass'
field
withdefault_factory
is already available there, but theValueError
is being raised only in 3.11 if I read the spec correctly.I would love to help, but C# is a no-go for me :/
baywet commentedon Apr 7, 2025
no worries, @ShawnSack is this something you'd like to take on?
14 remaining items
ntauth commentedon Apr 29, 2025
Why was this marked as completed? The PR has not even been merged yet
baywet commentedon Jun 9, 2025
sorry, this is the bot acting up again
rcleveng commentedon Jun 12, 2025
I'm also seeing this one, excited to see PR #6489 is underway.
Python: Set a default value for non-nullable array fields
rcleveng commentedon Jun 12, 2025
If folks are too busy on PR #6489, here's a slightly different approach that might be a better fix.
#6655
rcleveng commentedon Jun 12, 2025
Just for completeness, the change requiring the default_factory was added in Python 3.11 at #cpython/88840.
Using default_factory has been supported by dataclasses since it was added in Python 3.7