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 false negative when isinstance is provided too many or too few arguments #9868

Conversation

rogersheu
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix
βœ“ πŸ“œ Docs

Description

In #9847, @nickdrozd pointed out that when the wrong number of arguments (specifically too many) are passed to isinstance, we see a false negative, where no pylint warning comes out despite the issue.

Though it has been previously pointed out that built-in functions do not provide relevant argument information, i.e., expected number of arguments, we can for now rely on isinstance taking exactly two arguments to provide the user with a too-many-arguments warning.

# Built-in functions have no argument information.

A functional test has also been included to account for this case. The isinstance call makes a TypeError, so that must be caught in order for pylint to find the too-many-arguments issue successfully.

Closes #9847

@rogersheu rogersheu force-pushed the rsheu/fix/isinstance-too-many-arguments branch from a78b197 to 9da13f0 Compare August 12, 2024 01:52

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the False Negative πŸ¦‹ No message is emitted but something is wrong with the code label Aug 12, 2024

This comment has been minimized.

Copy link
Member

@jacobtylerwalls jacobtylerwalls 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 the contribution!

pylint/checkers/typecheck.py Show resolved Hide resolved
tests/functional/t/too/too_many_arguments.py Outdated Show resolved Hide resolved
doc/whatsnew/fragments/9847.false_negative Outdated Show resolved Hide resolved
@jacobtylerwalls jacobtylerwalls added this to the 3.3.0 milestone Aug 12, 2024

This comment has been minimized.

@nickdrozd
Copy link
Collaborator

Nice! Could you add a check for < 2 args too, as in isinstance(x)? That's the false negative that burned me in the real world.

@rogersheu rogersheu force-pushed the rsheu/fix/isinstance-too-many-arguments branch 3 times, most recently from 2a53891 to 4bffdbd Compare August 13, 2024 00:58
@rogersheu
Copy link
Contributor Author

rogersheu commented Aug 13, 2024

Nice! Could you add a check for < 2 args too, as in isinstance(x)? That's the false negative that burned me in the real world.

@nickdrozd I've noticed that there isn't a pylint warning for too-few-arguments. Does it make sense to make a new message for this? (Is that within the scope of this PR?)

@rogersheu rogersheu force-pushed the rsheu/fix/isinstance-too-many-arguments branch from 4bffdbd to a7f47ad Compare August 13, 2024 01:34

This comment has been minimized.

- add too-many-function-args call
- include handling for too-few-function-args
@rogersheu rogersheu force-pushed the rsheu/fix/isinstance-too-many-arguments branch from a7f47ad to 9682d47 Compare August 13, 2024 01:59
@rogersheu rogersheu force-pushed the rsheu/fix/isinstance-too-many-arguments branch from a3f1c8d to 3cc7e7f Compare August 13, 2024 02:16

This comment has been minimized.

@nickdrozd
Copy link
Collaborator

I've noticed that there isn't a pylint warning for too-few-arguments. Does it make sense to make a new message for this? (Is that within the scope of this PR?)

For me the only thing that matters is that bad isinstance calls are flagged. I don't have any opinions about how exactly this should be arranged.

@jacobtylerwalls, any thoughts?

@rogersheu
Copy link
Contributor Author

The test looks pretty fixable; I'll get to that tonight.

I've gotten the changelog error a number of times here. It looks like the test is not happy with seeing _template.rst for whatever reason?

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

Don't worry about the _template.rst issue, not related to this MR :)

Copy link

codecov bot commented Aug 14, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.80%. Comparing base (cb6db06) to head (2d6db8a).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #9868   +/-   ##
=======================================
  Coverage   95.80%   95.80%           
=======================================
  Files         174      174           
  Lines       18939    18942    +3     
=======================================
+ Hits        18144    18147    +3     
  Misses        795      795           
Files Coverage Ξ”
pylint/checkers/typecheck.py 96.52% <100.00%> (+0.01%) ⬆️

This comment has been minimized.

@rogersheu rogersheu changed the title fix false negative when isinstance is provided too many arguments fix false negative when isinstance is provided too many or too few arguments Aug 14, 2024
@rogersheu
Copy link
Contributor Author

rogersheu commented Aug 14, 2024

I see that I needed to run the make html as mentioned on the Contributing page.

@rogersheu
Copy link
Contributor Author

rogersheu commented Aug 14, 2024

Also, it would appear that the Primer / Comment workflow bumped in Python version from 3.12.4 to 3.12.5 and so the Primer / Comment Action is not finding a cached venv (and does not build one itself).

https://github.com/pylint-dev/pylint/actions/runs/10382711912/job/28746257136

However, Primer / Run has found the Python 3.12.5 cache fine at around the same time, so it's mysterious.

https://github.com/pylint-dev/pylint/actions/runs/10382595367/job/28769681193

It would appear that they are looking at different cache keys, at least.

@jacobtylerwalls
Copy link
Member

Thanks, I'll look into it. Speaking of the primer, I noticed there are warnings newly emitted (expand the "Echo Warnings" step).

@jacobtylerwalls
Copy link
Member

But the warning just looks like #9138, which was supposed to have been solved, maybe there really is a bad cache key somewhere?

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Looks really good, just some documentation nits and this should be g2g

pylint/checkers/typecheck.py Outdated Show resolved Hide resolved
doc/data/messages/t/too-few-function-args/bad.py Outdated Show resolved Hide resolved
doc/data/messages/t/too-few-function-args/good.py Outdated Show resolved Hide resolved
doc/whatsnew/fragments/9847.false_negative Show resolved Hide resolved
doc/whatsnew/fragments/9847.false_negative Outdated Show resolved Hide resolved

This comment has been minimized.

@jacobtylerwalls
Copy link
Member

Speaking of the primer, I noticed there are warnings newly emitted (expand the "Echo Warnings" step).

Oh, and you can forget this -- the warnings are happening on main now, too.

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Wonderful, thanks!

@rogersheu
Copy link
Contributor Author

@jacobtylerwalls Thanks a lot for providing some great feedback! πŸ˜„

Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 2d6db8a

@nickdrozd
Copy link
Collaborator

Thanks for working on this @rogersheu! Looking forward to seeing this warning the next time I mess up some isinstance call πŸ˜†

@rogersheu
Copy link
Contributor Author

No probs, @nickdrozd , thanks for making this suggestion in the first place!

Might be nice if this could someday be extended for other built-in functions πŸ€” , but then again, maybe it'd be easier to expand on the too-few-function-args message.

@@ -0,0 +1 @@
isinstance("foo") # [too-few-function-args]
Copy link
Member

Choose a reason for hiding this comment

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

Something minor for next time: in the pylint docs we tend to avoid "foo" and "bar" because junior programmers are often the target audience, and if they don't know the convention/backstory it has the potential to be distracting.

Also, "foo" is a literal just like 1 -- I was suggesting to change it for the reason that if we introduce a check later like literal-isinstance (in the spirit of using-constant-test), then we'll have to adjust this example. But let's not change it now, I'm not sure it's worth fleshing out this example more.

@jacobtylerwalls jacobtylerwalls merged commit a32250c into pylint-dev:main Aug 18, 2024
43 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Negative πŸ¦‹ No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False negative: Wrong number of arguments for isinstance
4 participants