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
17 changes: 16 additions & 1 deletion pyrefly/lib/alt/class/class_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4089,7 +4089,22 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
} else {
Instance::of_class(cls)
};
Arc::unwrap_or_clone(new_member.value).as_raw_special_method_type(self.heap, &instance)
let assume_self_return = new_member.value.is_function_without_return_annotation();
let mut new_ty = Arc::unwrap_or_clone(new_member.value)
.as_raw_special_method_type(self.heap, &instance)?;
if assume_self_return {
// Per the constructor typing spec, unannotated `__new__` may be assumed to
// return `Self` for constructor analysis.
let ret = if preserve_self {
self.heap.mk_self_type(cls.clone())
} else {
self.heap.mk_class_type(cls.clone())
};
new_ty.transform_toplevel_callable(&mut |callable: &mut Callable| {
callable.ret = ret.clone();
});
}
Some(new_ty)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pyrefly/lib/test/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ class C:
if orig_func is None:
return super().__new__(cls)
def f():
with C(): # E: `NoneType` has no attribute `__enter__` # E: `NoneType` has no attribute `__exit__`
with C():
pass
"#,
);
Expand Down
25 changes: 25 additions & 0 deletions pyrefly/lib/test/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,31 @@ C("5") # E: Argument `Literal['5']` is not assignable to parameter `x` with typ
"#,
);

// Regression test for a problem in networkx: https://github.com/facebook/pyrefly/issues/3121
testcase!(
test_return_type_inference_for_constructors,
r#"
from typing import assert_type

class A:
def __new__(cls, x: int | None = None):
if x is None:
return cls.__new__(cls, 5)
else:
return object.__new__(cls)

def __init__(cls):
return "x"

class B(A): ...

a = A()
assert_type(a, A)
b = B()
assert_type(b, B)
"#,
);

testcase!(
test_redundant_dict_constructor_call_ok,
r#"
Expand Down
Loading