ruff/crates/red_knot_python_semantic/resources/mdtest/annotations
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
..
annotated.md Special-case value-expression inference of special form subscriptions (#16877) 2025-03-20 21:46:02 +00:00
any.md [red-knot] Understand Annotated (#14950) 2024-12-13 09:41:37 -08:00
callable.md [red-knot] Add initial support for * imports (#16923) 2025-03-24 17:15:58 +00:00
deferred.md [red-knot] detect invalid return type (#16540) 2025-03-12 01:58:59 +00:00
int_float_complex.md [red-knot] add special case for float/complex (#16166) 2025-02-14 12:24:10 -08:00
invalid.md [red-knot] Ban most Type::Instance types in type expressions (#16872) 2025-03-20 15:19:56 -07:00
literal.md [red-knot] Make' Type::in_type_expression()' exhaustive for Type::KnownInstance (#16836) 2025-03-19 07:36:28 -07:00
literal_string.md [red-knot] Attribute access and the descriptor protocol (#16416) 2025-03-07 22:03:28 +01:00
never.md [red-knot] Statically known branches (#15019) 2024-12-21 11:33:10 +01:00
new_types.md [red-knot] Ban most Type::Instance types in type expressions (#16872) 2025-03-20 15:19:56 -07:00
optional.md [red-knot] Make' Type::in_type_expression()' exhaustive for Type::KnownInstance (#16836) 2025-03-19 07:36:28 -07:00
starred.md [red-knot] function parameter types (#14802) 2024-12-06 12:55:56 -08:00
stdlib_typing_aliases.md [red-knot] Add initial support for * imports (#16923) 2025-03-24 17:15:58 +00:00
string.md [red-knot] Add diagnostic for class-object access to pure instance variables (#16036) 2025-02-24 15:17:16 +01:00
union.md [red-knot] Make' Type::in_type_expression()' exhaustive for Type::KnownInstance (#16836) 2025-03-19 07:36:28 -07:00
unsupported_special_forms.md [red-knot] Support calling a typing.Callable (#16888) 2025-03-23 02:39:33 +05:30
unsupported_type_qualifiers.md Show more precise messages in invalid type expressions (#16850) 2025-03-19 17:00:30 +00:00