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

RUF011 possible false positive for optional unwrapping in dict comprehension #9548

Closed
farmio opened this issue Jan 16, 2024 · 3 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@farmio
Copy link

farmio commented Jan 16, 2024

Hi 👋!

I have a piece of code raising

RUF011 Dictionary comprehension uses static key: `attr`

from ruff check . since the update from ruff 0.1.11 to 0.1.13.

This is the piece of code in question:
https://github.com/XKNX/xknxproject/blob/73088e13bfcd11f4dc03963bf2c8ef4776972e68/xknxproject/loader/knx_master_loader.py#L70-L75
A short version of it:

from xml.etree.ElementTree import Element
translation_element: Element

translations = {
    attr: text
    for item in translation_element.findall("{*}Translation")  # `Element.findall() -> list[Element]`
    if (attr := item.get("AttributeName")) is not None  # unwrap since `Element.get() -> str | None`
    and (text := item.get("Text")) is not None
}

I would expect attr not to be treated as static key here since it should (depending on the xml source) be different for every iteration of the comprehension.

Here is my ruff section from pyproject.toml:

[tool.ruff]
target-version = "py39"
select = [
  "C4",  # comprehensions
  "D",   # pydocstyle
  "E",   # pycodestyle
  "F",   # pyflakes
  "I",   # isort
  "RUF", # ruff specific
  "T20", # print
  "UP",  # pyupgrade
  "W",   # pydocstyle warning
]
ignore = [
  "D202",
  "D203",
  "D212",
  "E501", # line too long
]
extend-exclude = [
  "script",
]

[tool.ruff.isort]
force-sort-within-sections = true
combine-as-imports = true
@charliermarsh charliermarsh self-assigned this Jan 16, 2024
@charliermarsh charliermarsh added the bug Something isn't working label Jan 16, 2024
@charliermarsh
Copy link
Member

Thanks! Looks like a bug.

@charliermarsh
Copy link
Member

I'll fix this today.

@charliermarsh
Copy link
Member

Oh sorry, I can confirm that this was already fixed on main but hasn't been released yet: #9494

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

No branches or pull requests

2 participants