Skip to content

Conversation

@charliermarsh
Copy link
Member

Summary

Given:

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.

@charliermarsh charliermarsh force-pushed the charlie/init-var branch 2 times, most recently from a06adbc to a52afe7 Compare January 29, 2024 20:38
@charliermarsh charliermarsh added the bug Something isn't working label Jan 29, 2024
@charliermarsh charliermarsh changed the title Avoid marking InitVar as typing-only Avoid marking InitVar as a typing-only annotation Jan 29, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Jan 29, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@charliermarsh
Copy link
Member Author

Ah wow that's a great ecosystem result -- this implementation actually isn't quite correct.

@charliermarsh charliermarsh merged commit 11449ac into main Jan 29, 2024
@charliermarsh charliermarsh deleted the charlie/init-var branch January 29, 2024 21:27
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 this pull request may close these issues.

False positive of TCH003 on InitVar import

2 participants