Skip to content
Closed
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
21 changes: 18 additions & 3 deletions pyrefly/lib/alt/class/class_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,20 +2466,35 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
// (e.g. generic functions assigned to attributes have their own
// type parameters that should not trigger invalid-type-var errors).
let mut forall_bound: SmallSet<&Quantified> = SmallSet::new();
fn collect_overload_tparams<'a>(
overload: &'a Overload,
acc: &mut SmallSet<&'a Quantified>,
) {
for sig in overload.signatures.iter() {
if let OverloadType::Forall(forall) = sig {
for q in forall.tparams.iter() {
acc.insert(q);
}
}
}
}
fn collect_forall_tparams<'a>(ty: &'a Type, acc: &mut SmallSet<&'a Quantified>) {
match ty {
Type::Forall(forall) => {
for q in forall.tparams.iter() {
acc.insert(q);
}
}
Type::BoundMethod(bm) => {
if let BoundMethodType::Forall(forall) = &bm.func {
Type::Overload(overload) => collect_overload_tparams(overload, acc),
Type::BoundMethod(bm) => match &bm.func {
BoundMethodType::Forall(forall) => {
for q in forall.tparams.iter() {
acc.insert(q);
}
}
}
BoundMethodType::Overload(overload) => collect_overload_tparams(overload, acc),
BoundMethodType::Function(_) => {}
},
_ => {}
}
ty.recurse(&mut |inner| collect_forall_tparams(inner, acc));
Expand Down
17 changes: 17 additions & 0 deletions pyrefly/lib/test/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,23 @@ def test(o: D):
"#,
);

// https://github.com/facebook/pyrefly/issues/2812
testcase!(
test_bound_overload_assigned_to_attribute,
r#"
from typing import assert_type

class ThemeStack:
def __init__(self) -> None:
self._entries: list[dict[str, str]] = [{}]
self.get = self._entries[-1].get

def test(stack: ThemeStack) -> None:
assert_type(stack.get("theme"), str | None)
assert_type(stack.get("theme", "fallback"), str)
"#,
);

testcase!(
test_attr_unknown,
r#"
Expand Down
Loading