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
15 changes: 13 additions & 2 deletions pyrefly/lib/alt/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
kws = x.arguments.keywords.map(CallKeyword::new);
}

let result = self.distribute_over_union(&callee_ty, |ty| match ty.callee_kind() {
let result = self.distribute_over_union(&callee_ty, |ty| {
// NotImplemented is a singleton constant, not a callable class.
if matches!(ty, Type::ClassType(cls) if cls.is_builtin("_NotImplementedType") || cls.has_qname("types", "NotImplementedType"))
{
return self.error(
errors,
x.func.range(),
ErrorInfo::Kind(ErrorKind::NotCallable),
"`NotImplemented` is not callable. Did you mean `NotImplementedError`?".to_owned(),
);
}
match ty.callee_kind() {
Some(CalleeKind::Function(FunctionKind::AssertType)) => self
.call_assert_type(
&x.arguments.args,
Expand Down Expand Up @@ -1844,7 +1855,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
// Check if this call applies a decorator with known typing effects to a function.
_ if let Some(ret) = self.maybe_apply_function_decorator(ty, &args, &kws, errors) => ret,
_ => self.freeform_call_infer(ty.clone(), &args, &kws, x.func.range(), x.arguments.range(), hint, errors),
});
}});
// TypeIs and TypeGuard functions return bool at runtime
match result {
Type::TypeIs(_) | Type::TypeGuard(_) => {
Expand Down
20 changes: 16 additions & 4 deletions pyrefly/lib/test/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,30 @@ def get_flow_version(run_id: str | None) -> str | None:
"#,
);

// https://github.com/facebook/pyrefly/issues/2918
// https://github.com/facebook/pyrefly/issues/2918 (solved)
testcase!(
bug = "Should error when calling NotImplemented (a constant, not a class)",
test_call_not_implemented_constant,
r#"
# NotImplemented is a singleton constant, not a callable class.
# Using NotImplemented() is always a mistake; they mean NotImplementedError().
def broken():
raise NotImplemented()
raise NotImplemented() # E: `NotImplemented` is not callable. Did you mean `NotImplementedError`?

def also_broken():
raise NotImplemented("not yet done")
raise NotImplemented("not yet done") # E: `NotImplemented` is not callable. Did you mean `NotImplementedError`?
"#,
);

testcase!(
test_call_not_implemented_in_union,
r#"
def f(condition: bool):
def g(): ...
if condition:
x = g
else:
x = NotImplemented
x() # E: `NotImplemented` is not callable. Did you mean `NotImplementedError`?
"#,
);

Expand Down
Loading