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

B017: False negative when "from" imports used #418

Closed
Klim314 opened this issue Oct 27, 2023 · 2 comments · Fixed by #424
Closed

B017: False negative when "from" imports used #418

Klim314 opened this issue Oct 27, 2023 · 2 comments · Fixed by #424

Comments

@Klim314
Copy link

Klim314 commented Oct 27, 2023

Summary

B017 fails when raises is imported directly

Version

Replicated on version 23.9.16

Sample code

import pytest
from pytest import raises


def foo():
    raise


def test_pytest_raises():
    with raises(Exception):
        foo()
    with pytest.raises(Exception):
        foo()
@cooperlees
Copy link
Collaborator

Nice find. Will happily take a fix to include supporting this.

@azinneck0485
Copy link
Contributor

I'm interested in doing a PR for this fix, however I have a question about how best to go about avoiding false positives.

The most obvious solution modifies the check for B017 to also be "positive" for an ast.Name object with id == "raises", but this would also falsely flag from Dummy import raises.

After looking further into the code, the most straightforward solution, to me, seems to me

  1. Add visit_ImportFrom(self, node) that then calls visitImport(...) to perform the same checks.
    • As-is, the ast.ImportFrom node is not handled in any way
    • I have verified that the B005 check method is entered with both types of import nodes after this change
  2. Modify the logic of check_for_b005 to account for the possibility of an ast.ImportFrom node
    • It seems like the solution would be to add an "or" to the first condition, if isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom) so that the node would be added to self._b005_imports
    • Is there anything else that would need to be changed to support ast.ImportFrom node? If so, would that be expected to be don as part of this PR, or create a new issue to log the needed change?
  3. Modify the logic in check_for_b017 to check for: ast.Name node with id = "raises" and an ast.ImportFrom node in _b005_imports that indicates the from pytest import raises node
  • This check should account for the possibility that the node is from pytest import ..., raises, ... (multiple imports in one line)
  • Would only the check for raises() have an matches argument be the only check that should be added (if possible)?

I want to make sure this approach is a reasonable solution before I get too far into the development, particularly the changes to check_for_b005 and how to handle the ast.ImportFrom node. Do you think this makes sense for a solution?

cooperlees pushed a commit that referenced this issue Nov 26, 2023

Verified

This commit was signed with the committer’s verified signature.
edgarrmondragon Edgar Ramírez Mondragón
… imported directly from pytest (#424)

* handle ast.ImportFrom nodes in b005 check to add this to the list of imports

* work out logic to determine if ast.Name node "raises" correspondes to the pytest.raises package

* integrate new logic for B017 to the check itself

* add check for matches keyword to prevent B017; whitespace corrections to pass tests

* update tests to capture changes to B017 logic

* files modified by pre-commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants