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

False positive with RUF009 using NewType, Final and dataclass #6447

Closed
inkhey opened this issue Aug 9, 2023 · 1 comment · Fixed by #15588
Closed

False positive with RUF009 using NewType, Final and dataclass #6447

inkhey opened this issue Aug 9, 2023 · 1 comment · Fixed by #15588
Labels
bug Something isn't working type-inference Requires more advanced type inference.

Comments

@inkhey
Copy link

inkhey commented Aug 9, 2023

Hello 👋 , I think I found a false positive on RUF009 rule.

test_ruff.py:

from dataclasses import dataclass
from typing import Final, NewType

State = NewType("State", str)


@dataclass
class System:
    state: Final[State] = State("Enabled")


@dataclass
class System2:
    state: Final[str] = "Enabled"

version:

❯ ruff --version
ruff 0.0.283

bug.toml:

select = ["RUF"]

output:

❯ ruff check --config=bug.toml test_ruff.py 
test_ruff.py:9:27: RUF009 Do not perform function call `State` in dataclass defaults
Found 1 error.
@zanieb
Copy link
Member

zanieb commented Aug 9, 2023

Thanks for the report! We could special case this by looking up the binding for State and checking it it's a NewType declaration. However, this will not work across files, with aliased assignments, etc. because we do not have type inference.

@zanieb zanieb added bug Something isn't working type-inference Requires more advanced type inference. labels Aug 9, 2023
dhruvmanila pushed a commit that referenced this issue Jan 20, 2025
…(`RUF009`) (#15588)

## Summary

Resolves #6447.

## Test Plan

`cargo nextest run` and `cargo insta test`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working type-inference Requires more advanced type inference.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants