-
Notifications
You must be signed in to change notification settings - Fork 152
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
Dataclass variables are tagged as unused variables #264
Comments
Thanks for the report! Could you also post the code where the three variables are used? |
@jendrikseipp FYI this is happening to me too. In my case the variables are used only in a SOME_TEMPLATE = """...{s.foo}...{s.bar}..."""
@dataclass
class MyDataclass:
foo: str
bar: str
def __str__(self):
return SOME_TEMPLATE.format(s=self) |
* Rename switch_port to switch_iface to avoid confusions. * Rename the context manager from dhcp_push() to config() as it's more natural to use: with dhcp.config(my_config): # do something * Simplify formatting of templates, added ignores to vulture for false positives, see also: jendrikseipp/vulture#264 * Add constructor documentation to the dataclasses. Change-Id: I3a1114118150189c267476b022d540df22f26aca
I also have this same issue. Is it possible to add something similar to the list of ignored decorators, like a list of superclasses such that all subclasses extending those superclasses should be ignored by vulture? In my specific use case, I have something like as follows. from abc import ABC
from dataclasses import dataclass, fields
from typing import Sequence, Type
@dataclass(frozen=True)
class Superclass(ABC):
@classmethod
def ordered_field_names(cls) -> Sequence[str]:
return [field.name for field in fields(cls)]
@dataclass(frozen=True)
class Foo(Superclass):
x: int
@dataclass(frozen=True)
class Bar(Superclass):
y: int
class Utils:
@staticmethod
def get_values(data: Superclass, typ: Type[Superclass]) -> Sequence[int]:
return [getattr(data, field_name) for field_name in typ.ordered_field_names()]
Utils.get_values(Foo(x=1), Foo)
Utils.get_values(Bar(y=2), Bar) When I run vulture, I get the following errors.
This is a result of using Ideally, I would like some configuration such that I can tell vulture to ignore all attributes of all class definitions that extend |
I think the best solution for these types of situations is to use a whitelist module such as the following:
Then add this module to the list of paths that Vulture scans. You may also want to call |
The whitelist approach works, but I think that having a |
Why would dataclass-like classes benefit more from a |
At least in my use case, dataclasses (and pydantic In regular classes I usually only declare attributes that I myself end up setting and using, so in that case an unused attribute really is unused and vulture is great at pointing that out. Another good example for external APIs is enums. I might have an enum of strings, which might only be constructed based on a string coming from an external API, in which case I might never reference any of the actual enum values directly. I might be missing something but this feels like a good use case to ignore classes subclassing |
Thanks for your explanation! I can see the utility of being able to ignore members of all classes that inherit from a given class. However, this is difficult to implement in Vulture, which has no notion of variable scopes. If somebody sees a solution for this, I'm happy to discuss it or review a pull request, though. |
We have a dataclass like the below we are using for data representation and vulture seems to treat these as unused variables.
Example dataclass:
Example unused variables:
The text was updated successfully, but these errors were encountered: