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

dataclasses requiring InitVar/ClassVar at runtime #83

Closed
jonyscathe opened this issue Apr 19, 2022 · 6 comments
Closed

dataclasses requiring InitVar/ClassVar at runtime #83

jonyscathe opened this issue Apr 19, 2022 · 6 comments

Comments

@jonyscathe
Copy link

jonyscathe commented Apr 19, 2022

As per https://docs.python.org/3/library/dataclasses.html#class-variables and https://docs.python.org/3/library/dataclasses.html#init-only-variables dataclass() inspects the type of a field in order to treat those types in some special way.

Code like their example (with imports added):

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)

Will throw TC002 on the import of InitVar.
But if InitVar is moved into a type checking block then initializing C will fail because database won't exist in the post_init function.

I imagine the only way to handle this (other than my current solution of a noqa anywhere I use InitVar) will to add exceptions for InitVar and ClassVar if dataclass is also imported or something along those lines ?

@sondrelg
Copy link
Member

I agree, we should add handling for this 👍

Do you think it makes sense to just ignore InitVar, or are there cases where dataclasses will evaluate other annotations?

Would you be interested in creating a fix for this?

@jonyscathe
Copy link
Author

From the dataclasses documentation it appears that it will evaluate InitVar or ClassVar annotations.
I can try making a fix for this.

@sondrelg
Copy link
Member

I started looking at this, but I'm no longer able to replicate the issue after v2.

v2 changes the plugin behaviour so that we won't flag imports like from dataclasses import InitVar if there's already one or more other imports from the module made that cannot be guarded. This means, since from dataclasses import dataclass will always exist in the same file, the InitVar import should never be flagged. The only way to mess up is if you did something like this:

from my_app.some_file import dataclass  # bad import
from dataclasses import InitVar

But this doesn't seem worth handling.

The typing module was also completely whitelisted in v2 since we assume from typing import TYPE_CHECKING will be needed in most scopes.

Are you able to still replicate the issue? If not, I think we might consider this resolved, accidentally 🙂

@sondrelg
Copy link
Member

I'll close this for now. Please let me know if you still think there's an issue 👍

@jonyscathe
Copy link
Author

Sorry I was sick yesterday and wasn't at my computer.
I checked this morning, and yes, it looks like this has been fixed 👍

@sondrelg
Copy link
Member

No worries! Thanks for checking 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants