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 of TCH003 on InitVar import #9666

Closed
jonyscathe opened this issue Jan 29, 2024 · 5 comments · Fixed by #9688
Closed

False positive of TCH003 on InitVar import #9666

jonyscathe opened this issue Jan 29, 2024 · 5 comments · Fixed by #9688
Assignees
Labels
bug Something isn't working

Comments

@jonyscathe
Copy link

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

@jonyscathe
Copy link
Author

I now realise that the setting: exempt-modules = ["dataclasses"] does fix this.
So I guess that is fine. Not the most ideal solution as it could mask genuine issues, but it'll work.

@charliermarsh
Copy link
Member

Is the right solution here not to add @dataclass to runtime-evaluated-decorators? See: https://docs.astral.sh/ruff/settings/#flake8-type-checking-runtime-evaluated-decorators.

@charliermarsh
Copy link
Member

Oh, but I guess this only applies to InitVar specifically, and not other annotations used within dataclasses?

@charliermarsh charliermarsh added question Asking for support or clarification bug Something isn't working and removed question Asking for support or clarification labels Jan 29, 2024
@charliermarsh
Copy link
Member

We can fix this.

@charliermarsh charliermarsh self-assigned this Jan 29, 2024
charliermarsh added a commit that referenced this issue Jan 29, 2024
## 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.
@jonyscathe
Copy link
Author

Thanks for the quick fix!
It is annoying that there are a handful of special cases like this in Python where the typing is actually used at runtime.

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