Skip to content

[red-knot] Allow ellipsis default params in stub functions #17243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,42 @@ from typing import Any
def g(x: Any = "foo"):
reveal_type(x) # revealed: Any | Literal["foo"]
```

## Stub functions

### In Protocol

```py
from typing import Protocol

class Foo(Protocol):
def x(self, y: bool = ...): ...
def y[T](self, y: T = ...) -> T: ...

class GenericFoo[T](Protocol):
def x(self, y: bool = ...) -> T: ...
```

### In abstract method

```py
from abc import abstractmethod

class Bar:
@abstractmethod
def x(self, y: bool = ...): ...
@abstractmethod
def y[T](self, y: T = ...) -> T: ...
```

### In function overload

```py
from typing import overload

@overload
def x(y: None = ...) -> None: ...
@overload
def x(y: int) -> str: ...
def x(y: int | None = None) -> str | None: ...
```
125 changes: 74 additions & 51 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,74 @@ impl<'db> TypeInferenceBuilder<'db> {
self.infer_annotation_expression(&type_alias.value, DeferredExpressionState::Deferred);
}

/// Returns `true` if the current scope is the function body scope of a method of a protocol
/// (that is, a class which directly inherits `typing.Protocol`.)
fn in_class_that_inherits_protocol_directly(&self) -> bool {
let current_scope_id = self.scope().file_scope_id(self.db());
let current_scope = self.index.scope(current_scope_id);
let Some(parent_scope_id) = current_scope.parent() else {
return false;
};
let parent_scope = self.index.scope(parent_scope_id);

let class_scope = match parent_scope.kind() {
ScopeKind::Class => parent_scope,
ScopeKind::Annotation => {
let Some(class_scope_id) = parent_scope.parent() else {
return false;
};
let potentially_class_scope = self.index.scope(class_scope_id);

match potentially_class_scope.kind() {
ScopeKind::Class => potentially_class_scope,
_ => return false,
}
}
_ => return false,
};

let NodeWithScopeKind::Class(node_ref) = class_scope.node() else {
return false;
};

// TODO move this to `Class` once we add proper `Protocol` support
node_ref.bases().iter().any(|base| {
matches!(
self.file_expression_type(base),
Type::KnownInstance(KnownInstanceType::Protocol)
)
})
}

/// Returns `true` if the current scope is the function body scope of a function overload (that
/// is, the stub declaration decorated with `@overload`, not the implementation), or an
/// abstract method (decorated with `@abstractmethod`.)
fn in_function_overload_or_abstractmethod(&self) -> bool {
let current_scope_id = self.scope().file_scope_id(self.db());
let current_scope = self.index.scope(current_scope_id);

let function_scope = match current_scope.kind() {
ScopeKind::Function => current_scope,
_ => return false,
};

let NodeWithScopeKind::Function(node_ref) = function_scope.node() else {
return false;
};

node_ref.decorator_list.iter().any(|decorator| {
let decorator_type = self.file_expression_type(&decorator.expression);

match decorator_type {
Type::FunctionLiteral(function) => matches!(
function.known(self.db()),
Some(KnownFunction::Overload | KnownFunction::AbstractMethod)
),
_ => false,
}
})
}

fn infer_function_body(&mut self, function: &ast::StmtFunctionDef) {
// Parameters are odd: they are Definitions in the function body scope, but have no
// constituent nodes that are part of the function body. In order to get diagnostics
Expand Down Expand Up @@ -1210,56 +1278,9 @@ impl<'db> TypeInferenceBuilder<'db> {
}
}

let is_overload_or_abstract = function.decorator_list.iter().any(|decorator| {
let decorator_type = self.file_expression_type(&decorator.expression);

match decorator_type {
Type::FunctionLiteral(function) => matches!(
function.known(self.db()),
Some(KnownFunction::Overload | KnownFunction::AbstractMethod)
),
_ => false,
}
});

let class_inherits_protocol_directly = (|| -> bool {
let current_scope_id = self.scope().file_scope_id(self.db());
let current_scope = self.index.scope(current_scope_id);
let Some(parent_scope_id) = current_scope.parent() else {
return false;
};
let parent_scope = self.index.scope(parent_scope_id);

let class_scope = match parent_scope.kind() {
ScopeKind::Class => parent_scope,
ScopeKind::Annotation => {
let Some(class_scope_id) = parent_scope.parent() else {
return false;
};
let potentially_class_scope = self.index.scope(class_scope_id);

match potentially_class_scope.kind() {
ScopeKind::Class => potentially_class_scope,
_ => return false,
}
}
_ => return false,
};

let NodeWithScopeKind::Class(node_ref) = class_scope.node() else {
return false;
};

// TODO move this to `Class` once we add proper `Protocol` support
node_ref.bases().iter().any(|base| {
matches!(
self.file_expression_type(base),
Type::KnownInstance(KnownInstanceType::Protocol)
)
})
})();

if (self.in_stub() || is_overload_or_abstract || class_inherits_protocol_directly)
if (self.in_stub()
|| self.in_function_overload_or_abstractmethod()
|| self.in_class_that_inherits_protocol_directly())
&& self.return_types_and_ranges.is_empty()
&& is_stub_suite(&function.body)
{
Expand Down Expand Up @@ -1552,7 +1573,9 @@ impl<'db> TypeInferenceBuilder<'db> {
declared_ty: declared_ty.into(),
inferred_ty: UnionType::from_elements(self.db(), [declared_ty, default_ty]),
}
} else if self.in_stub()
} else if (self.in_stub()
|| self.in_function_overload_or_abstractmethod()
|| self.in_class_that_inherits_protocol_directly())
&& default
.as_ref()
.is_some_and(|d| d.is_ellipsis_literal_expr())
Expand Down
Loading