Skip to content

Commit

Permalink
Avoid falsely marking non-submodules as submodule aliases (#6182)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
charliermarsh authored Jul 30, 2023
1 parent 76741ca commit de898c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F823.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ def main():

for sys in range(5):
pass


import requests_mock as rm


def requests_mock(requests_mock: rm.Mocker):
print(rm.ANY)
7 changes: 6 additions & 1 deletion crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,12 @@ impl<'a> SemanticModel<'a> {
return None;
}

self.scopes[scope_id].get(qualified_name)
let binding_id = self.scopes[scope_id].get(qualified_name)?;
if !self.bindings[binding_id].kind.is_submodule_import() {
return None;
}

Some(binding_id)
}

/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
Expand Down

0 comments on commit de898c5

Please sign in to comment.