Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting error when class has a property with the same name after snake case #1117

Open
joaonc opened this issue Sep 3, 2024 · 4 comments
Open

Comments

@joaonc
Copy link

joaonc commented Sep 3, 2024

openapi-python-client: 0.21.4
Python: 3.10
OpenAPI spec version: 3.0.3

In OpenAPI specs where a property has the same name as the snake cased class name (in the example below, the snake casing is the lower case, but if it was, ex, InstagramId, then this would happen with a property named instagram_id) the code generated has a duplicated variable name (see image below).

The suggested fix is that the return variable name should have an underscore _, in this example, email_.

      "Email": {
        "type": "object",
        "properties": {
          "email": {
            "type": "string"
          },
          "verified": {
            "type": "boolean"
          }
        },
        "required": [
          "email",
          "type"
        ]
      },
Monosnap xeal-qa-utils – email py 2024-09-03 18-31-37
@joaonc
Copy link
Author

joaonc commented Sep 3, 2024

Note: I understand that openapi-python-client uses ruff, but I also use mypy on the autogenerated code as it's part of a greater CI/CD workflow.

@dbanty
Copy link
Collaborator

dbanty commented Sep 4, 2024

Another option would be to change the name we use for the temporary value that's eventually returned, that way the property can keep its normal name.

The challenge will be picking a name that is unlikely to be used as a property. I suppose _email, since we try to avoid generating "private" names for properties?

@joaonc
Copy link
Author

joaonc commented Sep 4, 2024

This is an external API that I'm consuming so I can't really change the name of the property. I can, however, change the name of the class, which I changed to EmailObj.
But this is a temporary solution. The code should be fixed, imo.
The return variable name could be named return_obj, _return, obj, value or something neutral like that.

@eli-bl
Copy link
Collaborator

eli-bl commented Nov 5, 2024

Seems to me like the only reason there's a variable name at all in that last part is that additional_properties needs to be set separately— it's been deliberately declared as not settable via the initializer (which also makes it not settable by evolve), for reasons I'm unclear on. Otherwise it could just be:

return cls(
    email=email,
    verified=verified,
    type=type,
    id=id,
    additional_properties=d
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants