Skip to content

Array of item /w enum creates invalid dataclass in python #6350

Closed
@ShawnSack

Description

@ShawnSack

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_

Activity

moved this to Needs Triage 🔍 in Kiotaon Mar 25, 2025
baywet

baywet commented on Mar 31, 2025

@baywet
Member

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?

added and removed on Mar 31, 2025
added theissue type on Mar 31, 2025
added
help wantedIssue caused by core project dependency modules or library
on Mar 31, 2025
moved this from Needs Triage 🔍 to Waits for author 🔁 in Kiotaon Mar 31, 2025
JanHusarcik

JanHusarcik commented on Apr 3, 2025

@JanHusarcik

Hi team,

I'm having a similar experience with https://cloud.memsource.com/web/internal/v1/docs/latest/yaml:

      - name: order
        in: query
        required: false
        type: array
        items:
          type: string
          enum:
          - ASC
          - DESC

results in order: list[GetOrderQueryParameterType] = [] I have to manually edit to order: list[GetOrderQueryParameterType] = field(default_factory=list) for the generated client to work as expected.

Thank you for looking into it.

baywet

baywet commented on Apr 4, 2025

@baywet
Member

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

writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)}{codeElement.NamePrefix}{codeElement.Name}: {(codeElement.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(codeElement.Type.IsNullable ? "]" : string.Empty)} {(isNonNullableCollection ? "= []" : "= None")}");

And a unit test should be implemented here to prevent further regressions.

Is this something you'd like to submit a pull request for provided some guidance?

JanHusarcik

JanHusarcik commented on Apr 4, 2025

@JanHusarcik

Thanks for raising this point, @baywet. I'm on Python 3.11.2.

Quickly checking the 3.9 docs, dataclass' field with default_factory is already available there, but the ValueError 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

baywet commented on Apr 7, 2025

@baywet
Member

no worries, @ShawnSack is this something you'd like to take on?

14 remaining items

moved this from In Progress 🚧 to Done ✔️ in Kiotaon Apr 29, 2025
ntauth

ntauth commented on Apr 29, 2025

@ntauth

Why was this marked as completed? The PR has not even been merged yet

baywet

baywet commented on Jun 9, 2025

@baywet
Member

sorry, this is the bot acting up again

reopened this on Jun 9, 2025
rcleveng

rcleveng commented on Jun 12, 2025

@rcleveng
Contributor

I'm also seeing this one, excited to see PR #6489 is underway.

added a commit that references this issue on Jun 12, 2025
a1c9417
rcleveng

rcleveng commented on Jun 12, 2025

@rcleveng
Contributor

If folks are too busy on PR #6489, here's a slightly different approach that might be a better fix.
#6655

rcleveng

rcleveng commented on Jun 12, 2025

@rcleveng
Contributor

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

added this to the Kiota v1.28.0 milestone on Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Pythonhelp wantedIssue caused by core project dependency modules or librarystatus:waiting-for-author-feedbackIssue that we've responded but needs author feedback to closetype:bugA broken experience

Type

Projects

Status

Done ✔️

Relationships

None yet

    Participants

    @rcleveng@baywet@ntauth@ShawnSack@JanHusarcik

    Issue actions

      Array of item /w enum creates invalid dataclass in python · Issue #6350 · microsoft/kiota