Skip to content

Commit a96fc4e

Browse files
authored
[ty] Simplify TypedDict constructor call validation (#23148)
## Summary #23113 overcomplicated things a little bit :-) ## Test Plan existing tests pass
1 parent efc876f commit a96fc4e

File tree

1 file changed

+8
-26
lines changed
  • crates/ty_python_semantic/src/types/infer

1 file changed

+8
-26
lines changed

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11656,14 +11656,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
1165611656
// that protocol -- and indeed, according to the spec, type checkers must disallow abstract
1165711657
// subclasses of the protocol to be passed to parameters that accept `type[SomeProtocol]`.
1165811658
// <https://typing.python.org/en/latest/spec/protocol.html#type-and-class-objects-vs-protocols>.
11659-
if !callable_type.is_subclass_of() {
11660-
if let Some(protocol) = class.into_protocol_class(self.db()) {
11661-
report_attempted_protocol_instantiation(
11662-
&self.context,
11663-
call_expression,
11664-
protocol,
11665-
);
11666-
}
11659+
if !callable_type.is_subclass_of()
11660+
&& let Some(protocol) = class.into_protocol_class(self.db())
11661+
{
11662+
report_attempted_protocol_instantiation(&self.context, call_expression, protocol);
1166711663
}
1166811664

1166911665
// For class literals we model the entire class instantiation logic, so it is handled
@@ -11752,26 +11748,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
1175211748
);
1175311749

1175411750
// Validate `TypedDict` constructor calls after argument type inference.
11755-
// This needs to run for direct class literals (e.g. `MyTD(...)`), generic aliases
11756-
// (e.g. `MyGenTD[str](...)`), and `type[MyTD]`-style subclass targets.
11757-
let typed_dict = match callable_type {
11758-
Type::ClassLiteral(class_literal) if class_literal.is_typed_dict(self.db()) => {
11759-
Type::typed_dict(ClassType::NonGeneric(class_literal)).as_typed_dict()
11760-
}
11761-
Type::GenericAlias(generic_alias) if generic_alias.is_typed_dict(self.db()) => {
11762-
Type::typed_dict(ClassType::Generic(generic_alias)).as_typed_dict()
11763-
}
11764-
Type::SubclassOf(subclass_of) if subclass_of.is_typed_dict(self.db()) => subclass_of
11765-
.subclass_of()
11766-
.into_class(self.db())
11767-
.and_then(|class| Type::typed_dict(class).as_typed_dict()),
11768-
_ => None,
11769-
};
11770-
11771-
if let Some(typed_dict) = typed_dict {
11751+
if let Some(class) = class
11752+
&& class.class_literal(self.db()).is_typed_dict(self.db())
11753+
{
1177211754
validate_typed_dict_constructor(
1177311755
&self.context,
11774-
typed_dict,
11756+
TypedDictType::new(class),
1177511757
arguments,
1177611758
func.as_ref().into(),
1177711759
|expr| self.expression_type(expr),

0 commit comments

Comments
 (0)