Skip to content

[red-knot] Emit error if int/float/complex/bytes/boolean literals appear in type expressions outside typing.Literal[] #16765

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
Show file tree
Hide file tree
Changes from 12 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 @@ -47,8 +47,10 @@ def _(c: Callable[42, str]):
Or, when one of the parameter type is invalid in the list:

```py
# error: [invalid-type-form] "Int literals are not allowed in type expressions"
# error: [invalid-type-form] "Boolean literals are not allowed in type expressions"
def _(c: Callable[[int, 42, str, False], None]):
# revealed: (int, @Todo(number literal in type expression), str, @Todo(boolean literal in type expression), /) -> None
# revealed: (int, Unknown, str, Unknown, /) -> None
reveal_type(c)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ def _(
reveal_type(q) # revealed: Unknown
reveal_type(r) # revealed: Unknown
```

## Invalid AST nodes

```py
def _(
a: 1, # error: [invalid-type-form] "Int literals are not allowed in type expressions"
b: 2.3, # error: [invalid-type-form] "Float literals are not allowed in type expressions"
c: 4j, # error: [invalid-type-form] "Complex literals are not allowed in type expressions"
d: True, # error: [invalid-type-form] "Boolean literals are not allowed in type expressions"
e: int | b"foo", # error: [invalid-type-form] "Bytes literals are not allowed in type expressions"
):
reveal_type(a) # revealed: Unknown
reveal_type(b) # revealed: Unknown
reveal_type(c) # revealed: Unknown
reveal_type(d) # revealed: Unknown
reveal_type(e) # revealed: int | Unknown
```
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Literal: _SpecialForm
```py
from other import Literal

# error: [invalid-type-form] "Int literals are not allowed in type expressions"
a1: Literal[26]

def f():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ reveal_type(cast(int | str, 1)) # revealed: int | str
# error: [invalid-type-form]
reveal_type(cast(Literal, True)) # revealed: Unknown

# error: [invalid-type-form]
reveal_type(cast(1, True)) # revealed: Unknown

# TODO: These should be errors
cast(1)
cast(str)
cast(str, b"ar", "foo")

Expand Down
37 changes: 31 additions & 6 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6088,6 +6088,13 @@ impl<'db> TypeInferenceBuilder<'db> {
/// Infer the type of a type expression without storing the result.
fn infer_type_expression_no_store(&mut self, expression: &ast::Expr) -> Type<'db> {
// https://typing.readthedocs.io/en/latest/spec/annotations.html#grammar-token-expression-grammar-type_expression

let report_invalid_type_expression = |message: std::fmt::Arguments| {
self.context
.report_lint(&INVALID_TYPE_FORM, expression, message);
Type::unknown()
};

match expression {
ast::Expr::Name(name) => match name.ctx {
ast::ExprContext::Load => self
Expand Down Expand Up @@ -6118,12 +6125,30 @@ impl<'db> TypeInferenceBuilder<'db> {
todo_type!("ellipsis literal in type expression")
}

// Other literals do not have meaningful values in the annotation expression context.
// However, we will we want to handle these differently when working with special forms,
// since (e.g.) `123` is not valid in an annotation expression but `Literal[123]` is.
ast::Expr::BytesLiteral(_literal) => todo_type!("bytes literal in type expression"),
ast::Expr::NumberLiteral(_literal) => todo_type!("number literal in type expression"),
ast::Expr::BooleanLiteral(_literal) => todo_type!("boolean literal in type expression"),
ast::Expr::BytesLiteral(_literal) => report_invalid_type_expression(format_args!(
"Bytes literals are not allowed in type expressions"
)),
ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
value: ast::Number::Int(_),
..
}) => report_invalid_type_expression(format_args!(
"Int literals are not allowed in type expressions"
)),
ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
value: ast::Number::Float(_),
..
}) => report_invalid_type_expression(format_args!(
"Float literals are not allowed in type expressions"
)),
ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
value: ast::Number::Complex { .. },
..
}) => report_invalid_type_expression(format_args!(
"Complex literals are not allowed in type expressions"
)),
ast::Expr::BooleanLiteral(_literal) => report_invalid_type_expression(format_args!(
"Boolean literals are not allowed in type expressions"
)),

ast::Expr::Subscript(subscript) => {
let ast::ExprSubscript {
Expand Down