Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,18 +982,22 @@ impl<'db> ClassLiteral<'db> {
let class_def_node = scope.node(db).expect_class(&parsed);
class_def_node.type_params.as_ref().map(|type_params| {
let index = semantic_index(db, scope.file(db));
GenericContext::from_type_params(db, index, type_params)
let definition = index.expect_single_definition(class_def_node);
GenericContext::from_type_params(db, index, type_params, definition)
})
}

pub(crate) fn legacy_generic_context(self, db: &'db dyn Db) -> Option<GenericContext<'db>> {
self.explicit_bases(db).iter().find_map(|base| match base {
Type::KnownInstance(
KnownInstanceType::SubscriptedGeneric(generic_context)
| KnownInstanceType::SubscriptedProtocol(generic_context),
) => Some(*generic_context),
_ => None,
})
self.explicit_bases(db)
.iter()
.find_map(|base| match base {
Type::KnownInstance(
KnownInstanceType::SubscriptedGeneric(generic_context)
| KnownInstanceType::SubscriptedProtocol(generic_context),
) => Some(*generic_context),
_ => None,
})
.map(|generic_context| generic_context.with_definition(db, self.definition(db)))
}

pub(crate) fn inherited_legacy_generic_context(
Expand All @@ -1006,6 +1010,7 @@ impl<'db> ClassLiteral<'db> {
.iter()
.copied()
.filter(|ty| matches!(ty, Type::GenericAlias(_))),
self.definition(db),
)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl<'db> OverloadLiteral<'db> {
let definition = self.definition(db);
let generic_context = function_stmt_node.type_params.as_ref().map(|type_params| {
let index = semantic_index(db, scope.file(db));
GenericContext::from_type_params(db, index, type_params)
GenericContext::from_type_params(db, index, type_params, definition)
});
Signature::from_function(
db,
Expand Down
23 changes: 19 additions & 4 deletions crates/ty_python_semantic/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ruff_python_ast as ast;
use rustc_hash::FxHashMap;

use crate::semantic_index::SemanticIndex;
use crate::semantic_index::definition::Definition;
use crate::types::class::ClassType;
use crate::types::class_base::ClassBase;
use crate::types::instance::{NominalInstanceType, Protocol, ProtocolInstanceType};
Expand All @@ -28,6 +29,13 @@ use crate::{Db, FxOrderSet};
pub struct GenericContext<'db> {
#[returns(ref)]
pub(crate) variables: FxOrderSet<TypeVarInstance<'db>>,

/// The class, function, or alias definition that this generic context belongs to. This is
/// optional so that we can infer a type for `typing.Generic[...]` independent of whether it
/// appears in a base class list or not. When processing the base class list for a legacy
/// generic class, we will use [`with_definition`](Self::with_definition) to attach the class's
/// definition to the generic context.
pub(crate) definition: Option<Definition<'db>>,
}

pub(super) fn walk_generic_context<'db, V: super::visitor::TypeVisitor<'db> + ?Sized>(
Expand All @@ -49,12 +57,13 @@ impl<'db> GenericContext<'db> {
db: &'db dyn Db,
index: &'db SemanticIndex<'db>,
type_params_node: &ast::TypeParams,
definition: Definition<'db>,
) -> Self {
let variables: FxOrderSet<_> = type_params_node
.iter()
.filter_map(|type_param| Self::variable_from_type_param(db, index, type_param))
.collect();
Self::new(db, variables)
Self::new(db, variables, Some(definition))
}

fn variable_from_type_param(
Expand Down Expand Up @@ -84,6 +93,7 @@ impl<'db> GenericContext<'db> {
db: &'db dyn Db,
parameters: &Parameters<'db>,
return_type: Option<Type<'db>>,
definition: Definition<'db>,
) -> Option<Self> {
let mut variables = FxOrderSet::default();
for param in parameters {
Expand All @@ -100,14 +110,15 @@ impl<'db> GenericContext<'db> {
if variables.is_empty() {
return None;
}
Some(Self::new(db, variables))
Some(Self::new(db, variables, Some(definition)))
}

/// Creates a generic context from the legacy `TypeVar`s that appear in class's base class
/// list.
pub(crate) fn from_base_classes(
db: &'db dyn Db,
bases: impl Iterator<Item = Type<'db>>,
definition: Definition<'db>,
) -> Option<Self> {
let mut variables = FxOrderSet::default();
for base in bases {
Expand All @@ -116,7 +127,11 @@ impl<'db> GenericContext<'db> {
if variables.is_empty() {
return None;
}
Some(Self::new(db, variables))
Some(Self::new(db, variables, Some(definition)))
}

pub(crate) fn with_definition(self, db: &'db dyn Db, definition: Definition<'db>) -> Self {
Self::new(db, self.variables(db), Some(definition))
}

pub(crate) fn len(self, db: &'db dyn Db) -> usize {
Expand Down Expand Up @@ -253,7 +268,7 @@ impl<'db> GenericContext<'db> {
.iter()
.map(|ty| ty.normalized_impl(db, visitor))
.collect();
Self::new(db, variables)
Self::new(db, variables, self.definition(db))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8800,7 +8800,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
}
})
.collect();
typevars.map(|typevars| GenericContext::new(self.db(), typevars))
typevars.map(|typevars| GenericContext::new(self.db(), typevars, None))
}

fn infer_slice_expression(&mut self, slice: &ast::ExprSlice) -> Type<'db> {
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<'db> Signature<'db> {
}
});
let legacy_generic_context =
GenericContext::from_function_params(db, &parameters, return_ty);
GenericContext::from_function_params(db, &parameters, return_ty, definition);

if generic_context.is_some() && legacy_generic_context.is_some() {
// TODO: Raise a diagnostic!
Expand Down
Loading