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

PD005 erroneously flags arithmetic methods from other packages #74

Open
deppen8 opened this issue Sep 16, 2019 · 7 comments
Open

PD005 erroneously flags arithmetic methods from other packages #74

deppen8 opened this issue Sep 16, 2019 · 7 comments

Comments

@deppen8
Copy link
Owner

deppen8 commented Sep 16, 2019

Describe the bug
PD005 is triggered when any module has a method named something like .add()

To Reproduce
I noticed this while using the tarfile module. The tarfile module has a method named .add()

Here is the snippet. Flake8 triggered PD005 on both of the tar.add() calls.

tar = tarfile.open(tar_filename, "w")
tar.add(other_filename)  # <-- triggers PD005
for filename in filenames:
    tar.add(filename)  # <-- triggers PD005
tar.close()

Expected behavior
I expect this to only be triggered when a method like .add() is called on a pandas object.

Additional context
I expect that this applies to the comparison operators too (PD006). Any fix for PD005 should also be made for PD006 too.

@wanaryytel
Copy link

This is also true for PD011 with .values(), e.g. Flask request object has an attribute values, too.

@deppen8
Copy link
Owner Author

deppen8 commented Oct 24, 2019

Indeed. I should create a new Issue detailing this, but the problem is that many of our checks rely on the type of the object being a pandas object. This is a fundamental issue with static linting in Python because the AST doesn't know what type a thing is.

I am open to suggestions on how to get around this, but it will likely be a big job. For now, the undesirable workaround is to turn off checks that are particularly bothersome.

@wanaryytel
Copy link

A quick hack (that could be optional for example) would be to check if pandas is imported in the file. Sure, it won't work for a lot of cases, but for a lot of cases it also will work.

@deppen8
Copy link
Owner Author

deppen8 commented Oct 27, 2019

A quick hack (that could be optional for example) would be to check if pandas is imported in the file. Sure, it won't work for a lot of cases, but for a lot of cases it also will work.

I have considered this. If you'd like to put in a PR that adds this, I'd be happy to take a look at it.

@gahjelle
Copy link

gahjelle commented Feb 5, 2020

Regarding the case for PD011 - .values, one check could be for the parentheses. I get a lot of false positives because I'm using both pandas and dictionaries. Looping through the values of the dictionary with .values() triggers PD011.

I'm not really familiar with the AST, so I don't know if this is an easy check or not.

@daviddavo
Copy link

daviddavo commented Mar 21, 2022

What about type hinting?

Mypy uses the typed-ast package, but this has been included on the ast on Python 3.8+

So, in python < 3.8 you can use typed-ast, and on python 3.8+ you can just use the ast

Using type hinting, this shouldn't be a false positive:

import pandas as pd

bl: UnixDateBuilder = UnixDateBuilder()
bl.sub(month=3).change(day=1, hour=0, minute=0, second=0)

You can get the type of something using typing.get_type_hints()

Example from towardsdatascience: https://towardsdatascience.com/python-type-hints-docstrings-7ec7f6d3416b

@simchuck
Copy link
Collaborator

simchuck commented Oct 11, 2022 via email

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

5 participants