Skip to content
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 @@ -255,6 +255,11 @@ class Derived(Base):
Final may only be used in assignments or variable annotations. Using it in any other position is an
error.

```toml
[environment]
python-version = "3.12"
```

```py
from typing import Final, ClassVar, Annotated

Expand All @@ -275,13 +280,21 @@ class C:
self.LEGAL_I: Final[int]
self.LEGAL_I = 1

# TODO: This should be an error
# error: [invalid-type-form] "`Final` is not allowed in function parameter annotations"
def f(ILLEGAL: Final[int]) -> None:
pass

# TODO: This should be an error
# error: [invalid-type-form] "`Final` is not allowed in function parameter annotations"
def f[T](ILLEGAL: Final[int]) -> None:
pass

# error: [invalid-type-form] "`Final` is not allowed in function return type annotations"
def f() -> Final[None]: ...

# error: [invalid-type-form] "`Final` is not allowed in function return type annotations"
def f[T](x: T) -> Final[T]:
return x

# TODO: This should be an error
class Foo(Final[tuple[int]]): ...

Expand Down
32 changes: 28 additions & 4 deletions crates/ty_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
.as_deref()
.expect("function type params scope without type params");

self.infer_optional_annotation_expression(
self.infer_return_type_annotation(
function.returns.as_deref(),
DeferredExpressionState::None,
);
Expand Down Expand Up @@ -2581,7 +2581,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
if self.defer_annotations() {
self.deferred.insert(definition);
} else {
self.infer_optional_annotation_expression(
self.infer_return_type_annotation(
returns.as_deref(),
DeferredExpressionState::None,
);
Expand Down Expand Up @@ -2638,6 +2638,24 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
);
}

fn infer_return_type_annotation(
&mut self,
returns: Option<&ast::Expr>,
deferred_expression_state: DeferredExpressionState,
) {
if let Some(returns) = returns {
let annotated = self.infer_annotation_expression(returns, deferred_expression_state);

if annotated.qualifiers.contains(TypeQualifiers::FINAL) {
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, returns) {
builder.into_diagnostic(
"`Final` is not allowed in function return type annotations",
);
}
}
}
}

fn infer_parameters(&mut self, parameters: &ast::Parameters) {
let ast::Parameters {
range: _,
Expand Down Expand Up @@ -2668,10 +2686,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
default: _,
} = parameter_with_default;

self.infer_optional_annotation_expression(
let annotated = self.infer_optional_annotation_expression(
parameter.annotation.as_deref(),
DeferredExpressionState::None,
);

if annotated.is_some_and(|annotated| annotated.qualifiers.contains(TypeQualifiers::FINAL)) {
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, parameter) {
builder.into_diagnostic("`Final` is not allowed in function parameter annotations");
}
}
Comment on lines +2694 to +2698
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the same goes for ClassVar FWIW; we could add that rule as well, while we're here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the same goes for ClassVar FWIW; we could add that rule as well, while we're here?

ClassVar should be disallowed in many more positions as well. I'll write up a ticket. Seems like a good beginner task?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

}

fn infer_parameter(&mut self, parameter: &ast::Parameter) {
Expand Down Expand Up @@ -2956,7 +2980,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
}

fn infer_function_deferred(&mut self, function: &ast::StmtFunctionDef) {
self.infer_optional_annotation_expression(
self.infer_return_type_annotation(
function.returns.as_deref(),
DeferredExpressionState::Deferred,
);
Expand Down
Loading