Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ except* BaseException as e:
try:
help()
except* OSError as e:
# TODO: more precise would be `ExceptionGroup[OSError]` --Alex
# (needs homogeneous tuples + generics)
reveal_type(e) # revealed: BaseExceptionGroup[BaseException]
reveal_type(e) # revealed: ExceptionGroup[OSError]
```

## `except*` with multiple exceptions
Expand All @@ -33,9 +31,7 @@ except* OSError as e:
try:
help()
except* (TypeError, AttributeError) as e:
# TODO: more precise would be `ExceptionGroup[TypeError | AttributeError]` --Alex
# (needs homogeneous tuples + generics)
reveal_type(e) # revealed: BaseExceptionGroup[BaseException]
reveal_type(e) # revealed: ExceptionGroup[TypeError | AttributeError]
```

## `except*` with mix of `Exception`s and `BaseException`s
Expand All @@ -44,8 +40,7 @@ except* (TypeError, AttributeError) as e:
try:
help()
except* (KeyboardInterrupt, AttributeError) as e:
# TODO: more precise would be `BaseExceptionGroup[KeyboardInterrupt | AttributeError]` --Alex
reveal_type(e) # revealed: BaseExceptionGroup[BaseException]
reveal_type(e) # revealed: BaseExceptionGroup[KeyboardInterrupt | AttributeError]
```

## Invalid `except*` handlers
Expand All @@ -54,10 +49,10 @@ except* (KeyboardInterrupt, AttributeError) as e:
try:
help()
except* 3 as e: # error: [invalid-exception-caught]
reveal_type(e) # revealed: BaseExceptionGroup[BaseException]
reveal_type(e) # revealed: BaseExceptionGroup[Unknown]

try:
help()
except* (AttributeError, 42) as e: # error: [invalid-exception-caught]
reveal_type(e) # revealed: BaseExceptionGroup[BaseException]
reveal_type(e) # revealed: BaseExceptionGroup[AttributeError | Unknown]
```
28 changes: 24 additions & 4 deletions crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,8 @@ impl<'db> ClassLiteral<'db> {
} else {
let name = Type::string_literal(db, self.name(db));
let bases = TupleType::from_elements(db, self.explicit_bases(db));
// TODO: Should be `dict[str, Any]`
let namespace = KnownClass::Dict.to_instance(db);
let namespace = KnownClass::Dict
.to_specialized_instance(db, [KnownClass::Str.to_instance(db), Type::any()]);

// TODO: Other keyword arguments?
let arguments = CallArgumentTypes::positional([name, bases, namespace]);
Expand Down Expand Up @@ -1913,7 +1913,9 @@ pub enum KnownClass {
Slice,
Property,
BaseException,
Exception,
BaseExceptionGroup,
ExceptionGroup,
Classmethod,
Super,
// enum
Expand Down Expand Up @@ -2002,6 +2004,8 @@ impl<'db> KnownClass {

Self::Any
| Self::BaseException
| Self::Exception
| Self::ExceptionGroup
| Self::Object
| Self::OrderedDict
| Self::BaseExceptionGroup
Expand Down Expand Up @@ -2076,6 +2080,8 @@ impl<'db> KnownClass {
| Self::Property
| Self::BaseException
| Self::BaseExceptionGroup
| Self::Exception
| Self::ExceptionGroup
| Self::Classmethod
| Self::GenericAlias
| Self::GeneratorType
Expand Down Expand Up @@ -2133,6 +2139,8 @@ impl<'db> KnownClass {
Self::Property => "property",
Self::BaseException => "BaseException",
Self::BaseExceptionGroup => "BaseExceptionGroup",
Self::Exception => "Exception",
Self::ExceptionGroup => "ExceptionGroup",
Self::Classmethod => "classmethod",
Self::GenericAlias => "GenericAlias",
Self::ModuleType => "ModuleType",
Expand Down Expand Up @@ -2229,7 +2237,9 @@ impl<'db> KnownClass {
db: &'db dyn Db,
specialization: impl IntoIterator<Item = Type<'db>>,
) -> Type<'db> {
let class_literal = self.to_class_literal(db).expect_class_literal();
let Type::ClassLiteral(class_literal) = self.to_class_literal(db) else {
return Type::unknown();
};
Comment on lines +2240 to +2242
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was necessary or some custom-typeshed tests started panicking because they couldn't find Exception and ExceptionGroup in the custom typeshed

let Some(generic_context) = class_literal.generic_context(db) else {
return Type::unknown();
};
Expand Down Expand Up @@ -2349,6 +2359,8 @@ impl<'db> KnownClass {
| Self::Dict
| Self::BaseException
| Self::BaseExceptionGroup
| Self::Exception
| Self::ExceptionGroup
| Self::Classmethod
| Self::Slice
| Self::Super
Expand Down Expand Up @@ -2438,6 +2450,8 @@ impl<'db> KnownClass {
| Self::Property
| Self::BaseException
| Self::BaseExceptionGroup
| Self::Exception
| Self::ExceptionGroup
| Self::Classmethod
| Self::GenericAlias
| Self::ModuleType
Expand Down Expand Up @@ -2515,6 +2529,8 @@ impl<'db> KnownClass {
| Self::SupportsIndex
| Self::BaseException
| Self::BaseExceptionGroup
| Self::Exception
| Self::ExceptionGroup
| Self::Classmethod
| Self::TypeVar
| Self::ParamSpec
Expand Down Expand Up @@ -2557,6 +2573,8 @@ impl<'db> KnownClass {
"property" => Self::Property,
"BaseException" => Self::BaseException,
"BaseExceptionGroup" => Self::BaseExceptionGroup,
"Exception" => Self::Exception,
"ExceptionGroup" => Self::ExceptionGroup,
"classmethod" => Self::Classmethod,
"GenericAlias" => Self::GenericAlias,
"NoneType" => Self::NoneType,
Expand Down Expand Up @@ -2634,6 +2652,8 @@ impl<'db> KnownClass {
| Self::ModuleType
| Self::VersionInfo
| Self::BaseException
| Self::Exception
| Self::ExceptionGroup
| Self::EllipsisType
| Self::BaseExceptionGroup
| Self::Classmethod
Expand Down Expand Up @@ -2846,7 +2866,7 @@ mod tests {
for class in KnownClass::iter() {
let version_added = match class {
KnownClass::UnionType => PythonVersion::PY310,
KnownClass::BaseExceptionGroup => PythonVersion::PY311,
KnownClass::BaseExceptionGroup | KnownClass::ExceptionGroup => PythonVersion::PY311,
KnownClass::GenericAlias => PythonVersion::PY39,
_ => PythonVersion::PY37,
};
Expand Down
17 changes: 10 additions & 7 deletions crates/ty_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,9 +1769,7 @@ impl<'db> TypeInferenceBuilder<'db> {
}
let use_def = self.index.use_def_map(scope_id);
if use_def.can_implicit_return(self.db())
&& !KnownClass::NoneType
.to_instance(self.db())
.is_assignable_to(self.db(), declared_ty)
&& !Type::none(self.db()).is_assignable_to(self.db(), declared_ty)
{
report_implicit_return_type(&self.context, returns.range(), declared_ty);
}
Expand Down Expand Up @@ -2549,9 +2547,14 @@ impl<'db> TypeInferenceBuilder<'db> {
};

let symbol_ty = if except_handler_definition.is_star() {
// TODO: we should infer `ExceptionGroup` if `node_ty` is a subtype of `tuple[type[Exception], ...]`
// TODO: should be generic with `symbol_ty` as the generic parameter
KnownClass::BaseExceptionGroup.to_instance(self.db())
let class = if symbol_ty
.is_subtype_of(self.db(), KnownClass::Exception.to_instance(self.db()))
Comment on lines +2550 to +2551
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using is_subtype_of rather than is_assignable_to here because I think we should infer BaseExceptionGroup rather than ExceptionGroup if symbol_ty is Unknown or similar

{
KnownClass::ExceptionGroup
} else {
KnownClass::BaseExceptionGroup
};
class.to_specialized_instance(self.db(), [symbol_ty])
} else {
symbol_ty
};
Expand Down Expand Up @@ -4025,7 +4028,7 @@ impl<'db> TypeInferenceBuilder<'db> {
.map_or(ret.range(), |value| value.range());
self.record_return_type(ty, range);
} else {
self.record_return_type(KnownClass::NoneType.to_instance(self.db()), ret.range());
self.record_return_type(Type::none(self.db()), ret.range());
}
}

Expand Down
Loading