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

F823 false positive #6180

Closed
bastimeyer opened this issue Jul 30, 2023 · 2 comments · Fixed by #6182
Closed

F823 false positive #6180

bastimeyer opened this issue Jul 30, 2023 · 2 comments · Fixed by #6182
Assignees
Labels
bug Something isn't working

Comments

@bastimeyer
Copy link

$ ruff -V
ruff 0.0.280

Consider the following code snippet which overrides the default requests_mock pytest fixture with InvalidRequest being raised on unknown requests, so it needs to import the requests_mock package with the same name first in order to access certain exports like Mocker, ANY, or the exceptions submodule:

import pytest
import requests_mock as rm

@pytest.fixture()
def requests_mock(requests_mock: rm.Mocker) -> rm.Mocker:
    requests_mock.register_uri(rm.ANY, rm.ANY, exc=rm.exceptions.InvalidRequest)
    return requests_mock
$ ruff check --select F823 ./F823.py
F823.py:
  6:32 F823 Local variable `requests_mock` referenced before assignment
    |
  4 | @pytest.fixture()
  5 | def requests_mock(requests_mock: rm.Mocker) -> rm.Mocker:
  6 |     requests_mock.register_uri(rm.ANY, rm.ANY, exc=rm.exceptions.InvalidRequest)
    |                                ^^ F823
  7 |     return requests_mock
    |
  
Found 1 error.

Apparently, ruff doesn't handle the renamed import correctly. When renaming the pytest fixture function or the fixture argument, which both can't be done because it's a global fixture override in our test code, then ruff doesn't raise an error.

F823 wasn't an issue in 0.0.272 but it now is after upgrading to 0.0.280. The last modification of that rule was in #6036.

@charliermarsh charliermarsh self-assigned this Jul 30, 2023
@charliermarsh
Copy link
Member

I think this is my bad, I will fix this in time for Monday’s release.

@charliermarsh charliermarsh added the bug Something isn't working label Jul 30, 2023
charliermarsh added a commit that referenced this issue Jul 30, 2023
## Summary

We have some code to ensure that if an aliased import is used, any
submodules should be marked as used too. This comment says it best:

```rust
// If the name of a submodule import is the same as an alias of another import, and the
// alias is used, then the submodule import should be marked as used too.
//
// For example, mark `pyarrow.csv` as used in:
//
// ```python
// import pyarrow as pa
// import pyarrow.csv
// print(pa.csv.read_csv("test.csv"))
// ```
```

However, it looks like when we go to look up `pyarrow` (of `import
pyarrow as pa`), we aren't checking to ensure the resolved binding is
_actually_ an import. This was causing us to attribute `print(rm.ANY)`
to `def requests_mock` here:

```python
import requests_mock as rm

def requests_mock(requests_mock: rm.Mocker):
    print(rm.ANY)
```

Closes #6180.
@charliermarsh
Copy link
Member

This should be fixed in v0.0.282, which is out now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants