Skip to content
Open
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
10 changes: 10 additions & 0 deletions pyrefly/lib/solver/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,16 @@ impl<'solver, 'subset, Ans: LookupAnswer> Subset<'solver, 'subset, Ans> {
Ok(())
}
(Type::QuantifiedValue(_), Type::ClassType(cls)) if is_int_class_type(cls) => Ok(()),
(Type::ClassType(got_cls), Type::Literal(lit))
if let Lit::Enum(enum_lit) = &lit.value
&& got_cls.class_object() == enum_lit.class.class_object()
&& self
.type_order
.get_enum_member_count(got_cls.class_object())
== Some(1) =>
{
Ok(())
}
(Type::Literal(l_lit), Type::Literal(u_lit)) => {
ok_or(l_lit.value == u_lit.value, SubsetError::Other)
}
Expand Down
1 change: 1 addition & 0 deletions pyrefly/lib/test/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def f(test: bool, e1: E1, e2: E2):
x: Literal[E1.X, E1.Y] = e1
y: Literal[E1.X, E1.Y, 1] = e1
z: Literal[E2.X, E2.Y] = e2 # E: `E2` is not assignable to `Literal[E2.X, E2.Y]`
single: Literal[E2.X] = e2 # E: `E2` is not assignable to `Literal[E2.X]`
"#,
);

Expand Down
90 changes: 90 additions & 0 deletions pyrefly/lib/test/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,78 @@ def f(x: X[int]):
"#,
);

testcase!(
test_generic_alias_union_with_enum_literal,
r#"
from enum import Enum
from typing import Literal, TypeAlias, TypeVar, assert_type

T = TypeVar("T")

class Unset(Enum):
_UNSET = "UNSET"

UNSET: Literal[Unset._UNSET] = Unset._UNSET
UnsetType: TypeAlias = Literal[Unset._UNSET]
Missing: TypeAlias = UnsetType | T | None

def takes(x: Missing[Literal["a", "b"]]) -> None: ...

def or_unset[U](v: U | None) -> U | Literal[Unset._UNSET]:
return UNSET if v is None else v

def f(s: Literal["a"] | None) -> None:
assert_type(or_unset(s), Literal["a"] | Literal[Unset._UNSET])
assert_type(s, Literal["a"] | None)
takes(or_unset(s))
"#,
);

testcase!(
test_generic_alias_union_with_enum_literal_direct_argument,
r#"
from enum import Enum
from typing import Literal, TypeAlias, TypeVar

T = TypeVar("T")

class Unset(Enum):
_UNSET = "UNSET"

UnsetType: TypeAlias = Literal[Unset._UNSET]
Missing: TypeAlias = UnsetType | T | None

def takes(x: Missing[Literal["a", "b"]]) -> None: ...

def f(x: Literal["a"] | Literal[Unset._UNSET] | None) -> None:
takes(x)
"#,
);

testcase!(
test_generic_alias_union_with_inline_enum_literal,
r#"
from enum import Enum
from typing import Literal, TypeAlias, TypeVar

T = TypeVar("T")

class Unset(Enum):
_UNSET = "UNSET"

UNSET: Literal[Unset._UNSET] = Unset._UNSET
Missing: TypeAlias = Literal[Unset._UNSET] | T | None

def takes(x: Missing[Literal["a", "b"]]) -> None: ...

def or_unset[U](v: U | None) -> U | Literal[Unset._UNSET]:
return UNSET if v is None else v

def f(s: Literal["a"] | None) -> None:
takes(or_unset(s))
"#,
);

testcase!(
test_ordinary_alias_union_retains_precise_members,
r#"
Expand Down Expand Up @@ -1396,3 +1468,21 @@ X: TypeAlias = int | str
X() # E: Expected a callable
"#,
);

testcase!(
test_enum_literal_union_member_compatibility,
r#"
from enum import Enum
from typing import Literal

class Unset(Enum):
_UNSET = "UNSET"

def takes_member(x: Literal["a", "b", Unset._UNSET] | None) -> None: ...
def takes_class(x: Literal["a", "b"] | Unset | None) -> None: ...

def f(x: Literal["a"] | Literal[Unset._UNSET] | None) -> None:
takes_member(x)
takes_class(x)
"#,
);
Loading