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

Organize Imports Action Does Not Add __future__ Import #341

Closed
MicahLyle opened this issue Nov 15, 2023 · 3 comments · Fixed by astral-sh/ruff-lsp#321
Closed

Organize Imports Action Does Not Add __future__ Import #341

MicahLyle opened this issue Nov 15, 2023 · 3 comments · Fixed by astral-sh/ruff-lsp#321
Assignees
Labels
bug Something isn't working

Comments

@MicahLyle
Copy link

My pyproject.toml has required-imports = ["from __future__ import annotations"] but then the VS Code Organize Imports action does not automatically add future imports, and I can't seem to find a way or figure out how to make the formatting command output anything different from

Running Ruff with: ....\Scripts\ruff.exe ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--output-format', 'json', '-', '--fix', '--select', 'I001', '--stdin-filename', '....\my_file.py']

I'd expect the organize imports action to add the __future__ import because, from how I can recently remember/recall, that's what the isort VS Code extension does when it's used for the organize imports action.

--- Context ---

pyproject.toml:

# ==== ruff ====
# https://github.com/astral-sh/ruff
[tool.ruff]
ignore = [
    "E501",    # Line too long - `black` will handle formatting anyway
    "COM812",  # Trailing comma missing
    "PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable
    "RUF005",  # Consider {expr} instead of concatenation
    "RUF012",  # Mutable class attributes should be annotated with `typing.ClassVar`
    "RUF015",  # Prefer next({iterable}) over single element slice
]
per-file-ignores = { "__init__.py" = [
    "F401", # {name} imported but unused; consider using importlib.util.find_spec to test for availability
], "**/migrations/*.py" = [
    "I001", # Import block is un-sorted or un-formatted
    "I002", # Missing required import
], "**/test_*.py" = [
    "PLR0124", # Name compared with itself, consider replacing {left} {} {right}
    "PLR0915", # Too many statements ({statements} > {max_statements})
] }
select = [
    "F",    # Pyflakes               (F)    - https://docs.astral.sh/ruff/rules/#pyflakes-f
    "E",    # pycodestyle error      (E)    - https://docs.astral.sh/ruff/rules/#error-e
    "I",    # isort                  (I)    - https://docs.astral.sh/ruff/rules/#isort-i
    "UP",   # pyupgrade              (UP)   - https://docs.astral.sh/ruff/rules/#pyupgrade-up
    "YTT",  # flake8-2020            (YTT)  - https://docs.astral.sh/ruff/rules/#flake8-2020-ytt
    "B",    # flake8-bugbear         (B)    - https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
    "C4",   # flake8-comprehensions  (C4)   - https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4
    "COM",  # flake8-commas          (COM)  - https://docs.astral.sh/ruff/rules/#flake8-commas-com
    "T20",  # flake8-print           (T20)  - https://docs.astral.sh/ruff/rules/#flake8-print-t20
    "SLOT", # flake8-slots           (SLOT) - https://docs.astral.sh/ruff/rules/#flake8-slots-slot
    "RUF",  # Ruff-specific          (RUF)  - https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
]
line-length = 88
target-version = "py311"
unfixable = []

[tool.ruff.isort]
required-imports = ["from __future__ import annotations"]

VS Code Settings JSON

  "ruff.lint.args": [
    "--ignore",
    "I001,I002,F401,RUF100"
  ],
  "ruff.organizeImports": true,
  "ruff.lint.run": "onType",

I also tried adding

    "--extend-select",
    "I001,I002"

to "ruff.lint.args" but still had the same issue.

@MicahLyle
Copy link
Author

I will also note that removing "I002" from "ruff.lint.args" does not solve the issue. It just makes Ruff-VSCode tell you that at the top of the file you need the __future__ import.

Obviously I could bind a separate hot key to auto fix but I'd ideally like organize imports to behave in parity with isort here.

@charliermarsh
Copy link
Member

That's a good point. We should probably include I002 when we run the "Organize Imports" action. Right now, I believe it's confined to I001.

@charliermarsh
Copy link
Member

Fix is in the LSP: astral-sh/ruff-lsp#321

charliermarsh added a commit to astral-sh/ruff-lsp that referenced this issue Nov 16, 2023
## Summary

When running the "Organize Imports" action, we should _also_ add any
required imports, just as isort would.

Closes astral-sh/ruff-vscode#341.
azurelotus0926 added a commit to azurelotus0926/ruff-lsp that referenced this issue Jun 27, 2024
## Summary

When running the "Organize Imports" action, we should _also_ add any
required imports, just as isort would.

Closes astral-sh/ruff-vscode#341.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants