-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 of TCH003 on InitVar import #9666
Comments
I now realise that the setting: |
Is the right solution here not to add |
Oh, but I guess this only applies to |
We can fix this. |
## Summary Given: ```python from dataclasses import InitVar, dataclass @DataClass class C: i: int j: int = None database: InitVar[DatabaseType] = None def __post_init__(self, database): if self.j is None and database is not None: self.j = database.lookup('j') c = C(10, database=my_database) ``` We should avoid marking `InitVar` as typing-only, since it _is_ required by the dataclass at runtime. Note that by default, we _already_ don't flag this, since the `@dataclass` member is needed at runtime too -- so it's only a problem with `strict` mode. Closes #9666.
Thanks for the quick fix! |
TCH003 should not be raised on InitVar or ClassVar import even if they are only used for typing within a dataclass.
This is because they are needed at runtime.
See the corresponding issue in flake8-type-checking for more information: snok/flake8-type-checking#83
Not sure if it should be fixed in the same way in ruff.
Currently using ruff==0.1.14
Settings:
[tool.ruff]
extend-select = ['D213']
ignore = ['ANN101', 'ANN401', 'D107', 'D203', 'D212', 'E203', 'ISC003', 'N803', 'N806', 'PLC1901', 'PLC2701']
line-length = 120
preview = true
select = ['ALL']
target-version = 'py312'
[tool.ruff.format]
line-ending = 'lf'
quote-style = 'single'
[tool.ruff.lint.flake8-implicit-str-concat]
allow-multiline = false
[tool.ruff.lint.flake8-quotes]
inline-quotes = 'single'
[tool.ruff.lint.flake8-type-checking]
strict = true
[tool.ruff.lint.isort]
known-first-party = ['namespace.project']
[tool.ruff.lint.pylint]
max-args = 8
The text was updated successfully, but these errors were encountered: