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

Bug(?): FBT001 for a dataclass #10011

Closed
Spectre5 opened this issue Feb 16, 2024 · 1 comment · Fixed by #10027
Closed

Bug(?): FBT001 for a dataclass #10011

Spectre5 opened this issue Feb 16, 2024 · 1 comment · Fixed by #10027
Assignees
Labels
bug Something isn't working

Comments

@Spectre5
Copy link

Spectre5 commented Feb 16, 2024

In the __post_init__ method of a dataclass, the InitVar's cannot be passed by keyword argument, they must be positional. But in this case, FBT001 is trigged. For example:

from dataclasses import dataclass, InitVar

@dataclass
class Fit:
    force: InitVar[bool] = False

    def __post_init__(self, force: bool) -> None:
        print(force)

Fit(force=True)

And then with Ruff:

$ ruff --version
ruff 0.2.1
$ ruff check --isolated --select=FBT test.py 
test.py:7:29: FBT001 Boolean-typed positional argument in function definition
Found 1 error.

If we instead change the definition to def __post_init__(self, *, force: bool) -> None:, then FBT001 is no longer triggered, but the code will not work:

$ python test.py 
Traceback (most recent call last):
  File "<snip>/test.py", line 11, in <module>
    Fit(True)
  File "<string>", line 3, in __init__
TypeError: Fit.__post_init__() takes 1 positional argument but 2 were given

I suggest that dataclasses __post_init__ be added to an internal whitelist to be ignored by FBT001.

@charliermarsh charliermarsh added the bug Something isn't working label Feb 18, 2024
@charliermarsh
Copy link
Member

👍 We should allow this.

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