ruff/crates/ruff_benchmark
Alex Waygood e87fee4b3b
[red-knot] Add initial support for * imports (#16923)
## Summary

This PR adds initial support for `*` imports to red-knot. The approach
is to implement a standalone query, called from semantic indexing, that
visits the module referenced by the `*` import and collects all
global-scope public names that will be imported by the `*` import. The
`SemanticIndexBuilder` then adds separate definitions for each of these
names, all keyed to the same `ast::Alias` node that represents the `*`
import.

There are many pieces of `*`-import semantics that are still yet to be
done, even with this PR:
- This PR does not attempt to implement any of the semantics to do with
`__all__`. (If a module defines `__all__`, then only the symbols
included in `__all__` are imported, _not_ all public global-scope
symbols.
- With the logic implemented in this PR as it currently stands, we
sometimes incorrectly consider a symbol bound even though it is defined
in a branch that is statically known to be dead code, e.g. (assuming the
target Python version is set to 3.11):

  ```py
  # a.py

  import sys

  if sys.version_info < (3, 10):
      class Foo: ...

  ```

  ```py
  # b.py

  from a import *

  print(Foo)  # this is unbound at runtime on 3.11,
# but we currently consider it bound with the logic in this PR
  ```

Implementing these features is important, but is for now deferred to
followup PRs.

Many thanks to @ntBre, who contributed to this PR in a pairing session
on Friday!

## Test Plan

Assertions in existing mdtests are adjusted, and several new ones are
added.
2025-03-24 17:15:58 +00:00
..
benches [red-knot] Add initial support for * imports (#16923) 2025-03-24 17:15:58 +00:00
resources Fixing more spelling errors (#16926) 2025-03-23 10:55:14 -07:00
src Vendor benchmark test files (#15878) 2025-02-02 18:16:07 +00:00
Cargo.toml Move red_knot_python_semantic::PythonVersion to the ruff_python_ast crate (#16147) 2025-02-14 12:48:08 -05:00
README.md Update contributing docs to use cargo bench -p ruff_benchmark (#9535) 2024-01-15 14:57:30 -05:00

Ruff Benchmarks

The ruff_benchmark crate benchmarks the linter and the formatter on individual files:

# Run once on the "baseline".
cargo bench -p ruff_benchmark -- --save-baseline=main

# Compare against the "baseline".
cargo bench -p ruff_benchmark -- --baseline=main

# Run the lexer benchmarks.
cargo bench -p ruff_benchmark lexer -- --baseline=main

See CONTRIBUTING.md on how to use these benchmarks.