From a3ea8f484d6c24659151b23572f375a012955286 Mon Sep 17 00:00:00 2001 From: Daniel Stanila Date: Thu, 19 Nov 2026 11:16:30 +0100 Subject: [PATCH] Accept sentinel values as instances of the runtime sentinel class --- pyrefly/lib/solver/subset.rs | 8 ++++++++ pyrefly/lib/test/sentinel.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pyrefly/lib/solver/subset.rs b/pyrefly/lib/solver/subset.rs index 9f4940fb9f..c1e8dbac31 100644 --- a/pyrefly/lib/solver/subset.rs +++ b/pyrefly/lib/solver/subset.rs @@ -3029,6 +3029,14 @@ impl<'solver, 'subset, Ans: LookupAnswer> Subset<'solver, 'subset, Ans> { .heap .mk_class_type(self.type_order.stdlib().none_type().clone()), ), + // A sentinel value is an instance of the runtime `sentinel` class. + (Type::Sentinel(_), _) => self.is_subset_eq( + &self + .solver + .heap + .mk_class_type(self.type_order.stdlib().sentinel().clone()), + want, + ), (Type::Forall(forall), _) => { let got = self.instantiate_fresh_forall((**forall).clone(), want); self.is_subset_forall(got, want) diff --git a/pyrefly/lib/test/sentinel.rs b/pyrefly/lib/test/sentinel.rs index 8865260e69..54ef6588c2 100644 --- a/pyrefly/lib/test/sentinel.rs +++ b/pyrefly/lib/test/sentinel.rs @@ -118,6 +118,40 @@ B: A = Sentinel("B") # E: `sentinel` is not assignable to `A` "#, ); +testcase!( + test_sentinel_is_instance_of_runtime_class, + r#" +from typing_extensions import Sentinel + +MISSING = Sentinel("MISSING") +OTHER = Sentinel("OTHER") + +value: Sentinel = MISSING + +def f(value: int | Sentinel = MISSING) -> Sentinel: + return OTHER + +def g(value: Sentinel) -> None: + specific: MISSING = value # E: `sentinel` is not assignable to `MISSING` + +different: MISSING = OTHER # E: `OTHER` is not assignable to `MISSING` +not_class: type[Sentinel] = MISSING # E: `MISSING` is not assignable to `type[sentinel]` + "#, +); + +testcase!( + test_builtin_sentinel_is_instance_of_runtime_class_3_15, + TestEnv::new().with_version(PythonVersion::new(3, 15, 0)), + r#" +from typing_extensions import Sentinel + +BUILTIN = sentinel("BUILTIN") +UPPER = Sentinel("UPPER") + +def f(value: int | sentinel = BUILTIN, other: sentinel = UPPER) -> None: ... + "#, +); + testcase!( test_sentinel_typehint_any, r#"