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

Fix abstract-class-instantiated false positive for __new__ method. #8006

Closed
wants to merge 6 commits into from

Conversation

clavedeluna
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Closes #3060

@codecov
Copy link

codecov bot commented Dec 31, 2022

Codecov Report

Merging #8006 (e600117) into main (ae3f36d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8006   +/-   ##
=======================================
  Coverage   95.51%   95.52%           
=======================================
  Files         176      176           
  Lines       18541    18557   +16     
=======================================
+ Hits        17710    17726   +16     
  Misses        831      831           
Impacted Files Coverage Ξ”
pylint/checkers/base/basic_error_checker.py 95.85% <100.00%> (+0.29%) ⬆️

@github-actions

This comment has been minimized.

@clavedeluna clavedeluna added the Blocked 🚧 Blocked by a particular issue label Jan 5, 2023
@clavedeluna
Copy link
Contributor Author

Blocked by codecov issue

@DanielNoord DanielNoord added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Blocked 🚧 Blocked by a particular issue labels Jan 9, 2023
@github-actions

This comment has been minimized.

@clavedeluna
Copy link
Contributor Author

primer output is exactly what we'd expect here

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the coverage and test!

if "__new__" not in node.locals:
return False

new = next(node.igetattr("__new__"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be done in a try...except. Or with safe_infer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    try:
        new = next(node.igetattr("__new__"))
    except astroid.InferenceError:
        return False

is the closest I see to other repo code using igetattr. However, I can't think of a single example where we'd actually raise InferenceError since new is a dunder for pretty much any class. Do you have a suggestion? I can remove the try/except or ignore coverage for this line just for precaution.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. @Pierre-Sassoulas what do you want? Non-covered code? Or code that is known to raise a certain exception that we just don't know the edge case for yet?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with uncovered code catching the inference errors, it's safer imo.

pylint/checkers/base/basic_error_checker.py Show resolved Hide resolved
@@ -467,8 +496,16 @@ def _check_inferred_class_is_abstract(
return

if metaclass.qname() in ABC_METACLASSES:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make L498 a if not and then return. There is no need for this indentation here.

import weakref
from lala import Bala
import pandas as pd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this test as we don't install pandas.

"""book."""


bad = GoodithComplexNew()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

def __new__(self):
    cls = super().__new__()
    return cls

def __new__(self):
    cls = super().__new__()
    return "1"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as the class has abstract methods, yes msg will be emitted. will add them as test cases

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you take a look at the coverage? For the first one the pragma needs to be on the line two below.

@github-actions

This comment has been minimized.

self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
if metaclass.qname() not in ABC_METACLASSES:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out I can't come up with a single class example where we would land in this code, so going to remove this check. The previous if-checks above this handle the edge cases.

@github-actions

This comment has been minimized.

@clavedeluna
Copy link
Contributor Author

hm need to see what's up with that new primer output

@github-actions
Copy link
Contributor

πŸ€– Effect of this PR on checked open source code: πŸ€–

Effect on pandas:
The following messages are no longer emitted:

  1. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/io/formats/excel.py#L937
  2. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L110
  3. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L145
  4. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L363
  5. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L519
  6. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L657
  7. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L660
  8. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1103
  9. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1218
  10. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1223
  11. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1248
  12. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1257
  13. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1293
  14. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_writers.py#L1317
  15. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_xlsxwriter.py#L23
  16. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_xlsxwriter.py#L60
  17. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_xlsxwriter.py#L68
  18. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_xlsxwriter.py#L75
  19. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L94
  20. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L111
  21. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L125
  22. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L147
  23. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L173
  24. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L203
  25. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L245
  26. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L319
  27. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_openpyxl.py#L374
  28. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_style.py#L43
  29. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_style.py#L138
  30. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_style.py#L178
  31. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_style.py#L240
  32. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_style.py#L270
  33. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_odswriter.py#L19
  34. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_odswriter.py#L36
  35. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_odswriter.py#L38
  36. abstract-class-instantiated:
    Abstract class 'ExcelWriter' with abstract methods instantiated
    https://github.com/pandas-dev/pandas/blob/33f4f7b57b46bb295c1b71f3890377a5e541122e/pandas/tests/io/excel/test_odswriter.py#L45

Effect on pytest:
The following messages are now emitted:

  1. abstract-class-instantiated:
    Abstract class 'DontReadFromInput' with abstract methods instantiated
    https://github.com/pytest-dev/pytest/blob/ca40380e99c2cdaab1d0c041f9f28cff37ef8ff9/src/_pytest/capture.py#L344

This comment was generated for commit e600117

@clavedeluna
Copy link
Contributor Author

I'm still stumped by

abstract-class-instantiated:
Abstract class 'DontReadFromInput' with abstract methods instantiated
https://github.com/pytest-dev/pytest/blob/ca40380e99c2cdaab1d0c041f9f28cff37ef8ff9/src/_pytest/capture.py#L344

I tried to reproduce it locally bit that msg doesn't get raised. Maybe a maintainer has a better idea?

@DanielNoord
Copy link
Collaborator

Have you checked wether TextIO has any "abstract" methods?

@clavedeluna
Copy link
Contributor Author

Have you checked wether TextIO has any "abstract" methods?

It does ... while I can see DontReadFromInput getting some abstract-method msg raised, I'm not sure why we'd raise abstract-class-instantiated? Maybe I'm missing something subtle and the primer is right.

@DanielNoord
Copy link
Collaborator

Have you checked wether TextIO has any "abstract" methods?

It does ... while I can see DontReadFromInput getting some abstract-method msg raised, I'm not sure why we'd raise abstract-class-instantiated? Maybe I'm missing something subtle and the primer is right.

I don't understand this comment. If DontReadFromInput has abstract methods that don't get implemented than it is correct that we raise this here as well right?

@clavedeluna
Copy link
Contributor Author

Maybe I'm misunderstanding the purpose of the msg abstract-class-instantiated versus msg abstract-method? What I understand from the docs is that the former is telling a user "don't directly instantiate a class with abc.ABCMeta and with abstract-methods", but if the class instantiated is inheriting from one of these, but the instantiated class DOES implement the abstract methods, as in the case of DontReadFromInput then it's fine.

Anyway, if I'm wrong, then this PR should be able to be merged, I think everything asked is complete?

@DanielNoord
Copy link
Collaborator

Maybe I'm misunderstanding the purpose of the msg abstract-class-instantiated versus msg abstract-method? What I understand from the docs is that the former is telling a user "don't directly instantiate a class with abc.ABCMeta and with abstract-methods", but if the class instantiated is inheriting from one of these, but the instantiated class DOES implement the abstract methods, as in the case of DontReadFromInput then it's fine.

Anyway, if I'm wrong, then this PR should be able to be merged, I think everything asked is complete?

But isn't one of the abstract methods of DontReadFromInput missing from the final implementation?

@jacobtylerwalls jacobtylerwalls added the Waiting on author Indicate that maintainers are waiting for a message of the author label Sep 3, 2023
@github-actions
Copy link
Contributor

This PR needs take over because because it has been open 8 weeks with no activity.

@github-actions github-actions bot added the Needs take over πŸ›ŽοΈ Orignal implementer went away but the code could be salvaged. label Oct 29, 2023
@DanielNoord
Copy link
Collaborator

Closing this as it seems to have stalled.

@DanielNoord DanielNoord closed this Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs take over πŸ›ŽοΈ Orignal implementer went away but the code could be salvaged. Waiting on author Indicate that maintainers are waiting for a message of the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive abstract-class-instantiated with pandas.ExcelWriter
4 participants