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

Duplicated imports with --meta none #939

Closed
satwell opened this issue Jan 13, 2024 · 1 comment
Closed

Duplicated imports with --meta none #939

satwell opened this issue Jan 13, 2024 · 1 comment

Comments

@satwell
Copy link

satwell commented Jan 13, 2024

Describe the bug
Running with --meta none prints a bunch of F811 errors from ruff complaining about redefinition of imports. And the resulting output files do have duplicated imports. For example:

$ openapi-python-client generate --meta none --url https://raw.githubusercontent.com/openapi-generators/openapi-python-client/main/end_to_end_tests/baseline_openapi_3.0.json
[...]
models/test_inline_objects_body.py:8:21: F811 Redefinition of unused `UNSET` from line 6
models/test_inline_objects_body.py:8:28: F811 Redefinition of unused `Unset` from line 6
models/test_inline_objects_response_200.py:8:21: F811 Redefinition of unused `UNSET` from line 6
models/test_inline_objects_response_200.py:8:28: F811 Redefinition of unused `Unset` from line 6
Found 640 errors (482 fixed, 158 remaining).

I think this is happening because with --meta none there's no pyproject.toml that turns on isort functionality in ruff. With the other --meta options, ruff gets configured with select = ["F", "I"], and this fixes the duplicate imports.

One solution would be to explicitly include ruff's isort rules when running ruff in the post hooks. I've found that the following config fixes the issue:

post_hooks:
  - "ruff check . --fix --extend-select=I"
  - "ruff format ."

OpenAPI Spec File
This is reproducible with the openapi-python-client test specs, such as https://raw.githubusercontent.com/openapi-generators/openapi-python-client/main/end_to_end_tests/baseline_openapi_3.0.json.

Desktop (please complete the following information):

  • OS: Debian Bookworm
  • Python Version: 3.11.2
  • openapi-python-client version 0.17.1
dbanty added a commit that referenced this issue Jan 15, 2024
This PR was created by Knope. Merging it will create a new release

### Features

#### Add `--meta=pdm` option for generating PEP621 + PDM metadata

The default metadata is still `--meta=poetry`, which generates a
`pyproject.toml` file with Poetry-specific metadata.
This change adds the `--meta=pdm` option which includes
[PDM](https://pdm-project.org/latest/)-specific metadata, but also
standard
[PEP621](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#writing-pyproject-toml)
metadata. This may be useful as a starting point for other dependency
managers & build tools (like Hatch).

#### Add original OpenAPI `data` attribute to `Response` object

PR #767

In custom templates, you can now access a `response.data` attribute that
contains the original OpenAPI definition of the
response (Response Object or Reference Object).

#### Include the `UP` rule for generated Ruff config

This enables [pyupgrade-like
improvements](https://docs.astral.sh/ruff/rules/#pyupgrade-up) which
should replace some
`.format()` calls with f-strings.

### Fixes

#### Fix Ruff formatting for `--meta=none`

PR #940 fixes issue #939. Thanks @satwell!

Due to the lack of `pyproject.toml`, Ruff was not getting configured
properly when `--meta=none`.
As a result, it didn't clean up common generation issues like duplicate
imports, which would then cause errors from
linters.

This is now fixed by changing the default `post_hook` to `ruff check .
--fix --extend-select=I` when `--meta=none`.
Using `generate --meta=none` should now be almost identical to the code
generated by `update`.

Co-authored-by: GitHub <github-actions@github.com>
@dbanty
Copy link
Collaborator

dbanty commented Jan 22, 2024

This should be fixed in 0.17.2.

@dbanty dbanty closed this as completed Jan 22, 2024
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

2 participants