Skip to content

False positive: B018 #3831

Closed
Closed

Description

@AA-Turner

Ruff 0.0.260

Reproducer:

(sphinx) PS I:\Development\sphinx> pytest -vvv bug_b018.py
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.11.0, pytest-7.2.2, pluggy-1.0.0 -- I:\Mambaforge\envs\sphinx\python.exe
cachedir: .pytest_cache
rootdir: I:\Development\sphinx, configfile: pyproject.toml
plugins: xdist-3.2.0
collected 1 item                                                                                                                                                                                                                        

bug_b018.py::test_attribute_error PASSED                                                                                                                                                                                         [100%] 

========================================================================================================== 1 passed in 0.01s ========================================================================================================== 
(sphinx) PS I:\Development\sphinx> type bug_b018.py                                        
import pytest


def test_attribute_error():
    obj = object()
    with pytest.raises(AttributeError):
        obj.nonexistent
(sphinx) PS I:\Development\sphinx> ruff --isolated --show-source --select B018 bug_b018.py  
bug_b018.py:7:9: B018 Found useless expression. Either assign it to a variable or remove it.
  |
7 |         obj.nonexistent
  |         ^^^^^^^^^^^^^^^ B018
  |

Found 1 error.
(sphinx) PS I:\Development\sphinx> 

I think that this may be related to #3453 and #3455, as this is a regression from 0.0.259 to 0.0.260.

In tests it can be useful to check that an attribute does not exist, or some behaviour relating to side effects on accessing an attribute. Perhaps if unittest or pytest context managers are detected, B018 should not be raised?

Thanks,
Adam

Activity

  1. charliermarsh commented on Mar 31, 2023

    @charliermarsh
    Member

    Yeah I considered not flagging any expressions that use attributes or subscript accesses as "useless". It would've limited the rule significantly, but it also would help us avoid a class of false-positives...

  2. added
    bugAn issue describing something that isn't working, or a PR that fixes a bug
    on Mar 31, 2023
  3. leiserfg commented on Apr 1, 2023

    @leiserfg
    Contributor

    I will prefer if it stays as is, it is better having to add a noqa than having a dangling attribute reference, that either means it's a non-required expression or is a descriptor with secondary effects. The first one should be deleted, the second one should be avoided and both require human intervention.

  4. AA-Turner commented on Apr 1, 2023

    @AA-Turner
    ContributorAuthor

    ... it is better having to add a noqa than having a dangling attribute reference

    I would tend to agree in the general case, my specific request was that:

    if unittest or pytest context managers are detected, B018 should not be raised

    Would you agree with this, specifically in a testing context?

    Thanks,
    Adam

  5. charliermarsh commented on Apr 2, 2023

    @charliermarsh
    Member

    I think the request is reasonable but I'm hesitant to act on it. I see it as similarly to how we handle unused imports within ModuleNotFoundError-like try-except blocks. We don't ignore them, since it can be surprising to users and hard to understand the intent explicitly. But we do have a custom message that points users in the right direction for a more preferable pattern.

    In this case, my preference would be to encourage a pattern like:

    with blah blah:
        _ = obj.nonexistent

    To me, that signals the intent that the variable is intentionally unused (and IIRC we should avoid flagging that as an unused local).

  6. spaceone commented on Jun 10, 2023

    @spaceone
    Contributor

    _ is often used as gettext translation variable.

  7. leiserfg commented on Jun 10, 2023

    @leiserfg
    Contributor

    It can be any var matching "dummy-variable-rgx" like _noqa = obj.nonexistent.

  8. lafrech commented on Mar 27, 2024

    @lafrech

    I came here with the same issue and I believe the underscore suggestion is a nice solution.

  9. charliermarsh commented on Mar 27, 2024

    @charliermarsh
    Member

    Closing this for now as we don't intend to change it, and I recently added the underscore suggestion to the docs (but may not be released yet, can't remember off-hand).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue describing something that isn't working, or a PR that fixes a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions