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 @@ -13,7 +13,7 @@ materializations of `B`, and all materializations of `B` are also materializatio
### Fully static

```py
from typing_extensions import Literal, LiteralString, Never
from typing_extensions import Literal, LiteralString, Protocol, Never
from ty_extensions import Unknown, is_equivalent_to, static_assert, TypeOf, AlwaysTruthy, AlwaysFalsy
from enum import Enum

Expand Down Expand Up @@ -43,6 +43,16 @@ static_assert(is_equivalent_to(Literal[Single.VALUE], Single))
static_assert(is_equivalent_to(Single, Literal[Single.VALUE]))
static_assert(is_equivalent_to(Literal[Single.VALUE], Literal[Single.VALUE]))

static_assert(is_equivalent_to(tuple[Single] | int | str, str | int | tuple[Literal[Single.VALUE]]))

class Protocol1(Protocol):
a: Single

class Protocol2(Protocol):
a: Literal[Single.VALUE]

static_assert(is_equivalent_to(Protocol1, Protocol2))

static_assert(is_equivalent_to(Never, Never))
static_assert(is_equivalent_to(AlwaysTruthy, AlwaysTruthy))
static_assert(is_equivalent_to(AlwaysFalsy, AlwaysFalsy))
Expand Down
6 changes: 6 additions & 0 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,12 @@ impl<'db> Type<'db> {
type_is.with_type(db, type_is.return_type(db).normalized_impl(db, v))
}),
Type::Dynamic(dynamic) => Type::Dynamic(dynamic.normalized()),
Type::EnumLiteral(enum_literal)
if is_single_member_enum(db, enum_literal.enum_class(db)) =>
{
// Always normalize single-member enums to their class instance (`Literal[Single.VALUE]` => `Single`)
enum_literal.enum_class_instance(db)
}
Comment on lines +1065 to +1070
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose this direction, because the other seems more costly in terms of performance: If we wanted to normalize Single => Literal[Single.VALUE], we would have to check every nominal instance type, to see if it's an enum — which requires a MRO traversal.

Type::LiteralString
| Type::AlwaysFalsy
| Type::AlwaysTruthy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::db::tests::TestDb;
use crate::place::{builtins_symbol, known_module_symbol};
use crate::types::enums::is_single_member_enum;
use crate::types::tuple::TupleType;
use crate::types::{
BoundMethodType, CallableType, EnumLiteralType, IntersectionBuilder, KnownClass, Parameter,
Expand Down Expand Up @@ -27,6 +28,8 @@ pub(crate) enum Ty {
BytesLiteral(&'static str),
// An enum literal variant, using `uuid.SafeUUID` as base
EnumLiteral(&'static str),
// A single-member enum literal, using `dataclasses.MISSING`
SingleMemberEnumLiteral,
// BuiltinInstance("str") corresponds to an instance of the builtin `str` class
BuiltinInstance(&'static str),
/// Members of the `abc` stdlib module
Expand Down Expand Up @@ -145,6 +148,15 @@ impl Ty {
.expect_class_literal(),
Name::new(name),
)),
Ty::SingleMemberEnumLiteral => {
let ty = known_module_symbol(db, KnownModule::Dataclasses, "MISSING")
.place
.expect_type();
debug_assert!(
matches!(ty, Type::NominalInstance(instance) if is_single_member_enum(db, instance.class.class_literal(db).0))
);
ty
}
Ty::BuiltinInstance(s) => builtins_symbol(db, s)
.place
.expect_type()
Expand Down Expand Up @@ -265,6 +277,7 @@ fn arbitrary_core_type(g: &mut Gen, fully_static: bool) -> Ty {
Ty::EnumLiteral("safe"),
Ty::EnumLiteral("unsafe"),
Ty::EnumLiteral("unknown"),
Ty::SingleMemberEnumLiteral,
Ty::KnownClassInstance(KnownClass::Object),
Ty::KnownClassInstance(KnownClass::Str),
Ty::KnownClassInstance(KnownClass::Int),
Expand Down
Loading