Skip to content

Commit 0109d70

Browse files
committed
document binding_context better
1 parent d657c30 commit 0109d70

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6479,6 +6479,31 @@ pub struct TypeVarInstance<'db> {
64796479

64806480
/// The definition of the generic class, function, or type alias that binds this typevar. This
64816481
/// is `None` for a legacy typevar outside of a context that can bind it.
6482+
///
6483+
/// For a legacy typevar, the binding context might be missing:
6484+
///
6485+
/// ```py
6486+
/// T = TypeVar("T") # [1]
6487+
/// def generic_function(t: T) -> T: ... # [2]
6488+
/// ```
6489+
///
6490+
/// Here, we will create two `TypeVarInstance`s for the typevar `T`. Both will have `[1]` as
6491+
/// their [`definition`][Self::definition]. The first represents the variable when it is first
6492+
/// created, and not yet used, so it's `binding_context` will be `None`. The second represents
6493+
/// when the typevar is used in `generic_function`, and its `binding_context` will be `[2]`
6494+
/// (that is, the definition of `generic_function`).
6495+
///
6496+
/// For a PEP 695 typevar, there will always be a binding context, since you can only define
6497+
/// one as part of creating the generic context that uses it:
6498+
///
6499+
/// ```py
6500+
/// def generic_function[T](t: T) -> T: ...
6501+
/// ```
6502+
///
6503+
/// Here, we will create a single `TypeVarInstance`. Its [`definition`][Self::definition] will
6504+
/// be the `T` in `[T]` (i.e., the definition of the typevar in the syntactic construct that
6505+
/// creates the generic context that uses it). Its `binding_context` will be the definition of
6506+
/// `generic_function`.
64826507
binding_context: Option<Definition<'db>>,
64836508

64846509
/// The upper bound or constraint on the type of this TypeVar

0 commit comments

Comments
 (0)