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
8 changes: 4 additions & 4 deletions crates/ty/docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ 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 this context in a type expression"
# error: [invalid-type-form] "Boolean literals are not allowed in this context in a type expression"
# error: [invalid-type-form] "Int literals are not allowed in this context in a parameter annotation"
# error: [invalid-type-form] "Boolean literals are not allowed in this context in a parameter annotation"
def _(c: Callable[[int, 42, str, False], None]):
# revealed: (int, Unknown, str, Unknown, /) -> None
reveal_type(c)
Expand All @@ -69,7 +69,7 @@ def _(c: Callable[[...], int]):
```

```py
# error: [invalid-type-form] "`...` is not allowed in this context in a type expression"
# error: [invalid-type-form] "`...` is not allowed in this context in a parameter annotation"
def _(c: Callable[[int, ...], int]):
reveal_type(c) # revealed: (int, Unknown, /) -> int
```
Expand Down Expand Up @@ -114,7 +114,7 @@ from typing import Callable
# fmt: off

def _(c: Callable[
# error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
# error: [invalid-type-form] "Int literals are not allowed in this context in a parameter annotation"
{1, 2}, 2 # error: [invalid-type-form] "The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`"
]
):
Expand Down Expand Up @@ -143,7 +143,7 @@ from typing import Callable

def _(c: Callable[
int, # error: [invalid-type-form] "The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`"
[str] # error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
[str] # error: [invalid-type-form] "List literals are not allowed in this context in a parameter annotation"
]
):
reveal_type(c) # revealed: (...) -> Unknown
Expand All @@ -158,7 +158,7 @@ from typing import Callable

def _(c: Callable[
int, # error: [invalid-type-form] "The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`"
(str, ) # error: [invalid-type-form] "Tuple literals are not allowed in this context in a type expression"
(str, ) # error: [invalid-type-form] "Tuple literals are not allowed in this context in a parameter annotation"
]
):
reveal_type(c) # revealed: (...) -> Unknown
Expand All @@ -169,7 +169,7 @@ def _(c: Callable[
```py
from typing import Callable

# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
# error: [invalid-type-form] "List literals are not allowed in this context in a parameter annotation"
def _(c: Callable[[int], [str]]):
reveal_type(c) # revealed: (int, /) -> Unknown
```
Expand All @@ -184,8 +184,8 @@ from typing import Callable

def _(c: Callable[ # error: [invalid-type-form] "Special form `typing.Callable` expected exactly two arguments (parameter types and return type)"
[int],
[str], # error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
[bytes] # error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
[str], # error: [invalid-type-form] "List literals are not allowed in this context in a parameter annotation"
[bytes] # error: [invalid-type-form] "List literals are not allowed in this context in a parameter annotation"
]
):
reveal_type(c) # revealed: (...) -> Unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ reveal_type(Strings) # revealed: GenericAlias
However, using such a `GenericAlias` instance in a type expression is currently not supported:

```py
# error: [invalid-type-form] "Variable of type `GenericAlias` is not allowed in a type expression"
# error: [invalid-type-form] "Variable of type `GenericAlias` is not allowed in a parameter annotation"
def _(strings: Strings) -> None:
reveal_type(strings) # revealed: Unknown
```
98 changes: 49 additions & 49 deletions crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _(
):
def foo(): ...
def invalid(
a_: a, # error: [invalid-type-form] "Variable of type `type[int]` is not allowed in a type expression"
a_: a, # error: [invalid-type-form] "Variable of type `type[int]` is not allowed in a parameter annotation"
b_: b, # error: [invalid-type-form]
c_: c, # error: [invalid-type-form]
d_: d, # error: [invalid-type-form]
Expand All @@ -35,8 +35,8 @@ def _(
h_: h, # error: [invalid-type-form]
i_: typing, # error: [invalid-type-form]
j_: foo, # error: [invalid-type-form]
k_: i, # error: [invalid-type-form] "Variable of type `int` is not allowed in a type expression"
l_: j, # error: [invalid-type-form] "Variable of type `A` is not allowed in a type expression"
k_: i, # error: [invalid-type-form] "Variable of type `int` is not allowed in a parameter annotation"
l_: j, # error: [invalid-type-form] "Variable of type `A` is not allowed in a parameter annotation"
):
reveal_type(a_) # revealed: Unknown
reveal_type(b_) # revealed: Unknown
Expand Down Expand Up @@ -80,37 +80,37 @@ def bar() -> None:

def outer_sync(): # `yield` from is only valid syntax inside a synchronous function
def _(
a: (yield from [1]), # error: [invalid-type-form] "`yield from` expressions are not allowed in type expressions"
a: (yield from [1]), # error: [invalid-type-form] "`yield from` expressions are not allowed in parameter annotations"
): ...

async def baz(): ...
async def outer_async(): # avoid unrelated syntax errors on `yield` and `await`
def _(
a: 1, # error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
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 this context in a type expression"
a: 1, # error: [invalid-type-form] "Int literals are not allowed in this context in a parameter annotation"
b: 2.3, # error: [invalid-type-form] "Float literals are not allowed in parameter annotations"
c: 4j, # error: [invalid-type-form] "Complex literals are not allowed in parameter annotations"
d: True, # error: [invalid-type-form] "Boolean literals are not allowed in this context in a parameter annotation"
# error: [unsupported-operator]
# error: [invalid-type-form] "Bytes literals are not allowed in this context in a type expression"
# error: [invalid-type-form] "Bytes literals are not allowed in this context in a parameter annotation"
e: int | b"foo",
f: 1 and 2, # error: [invalid-type-form] "Boolean operations are not allowed in type expressions"
g: 1 or 2, # error: [invalid-type-form] "Boolean operations are not allowed in type expressions"
h: (foo := 1), # error: [invalid-type-form] "Named expressions are not allowed in type expressions"
i: not 1, # error: [invalid-type-form] "Unary operations are not allowed in type expressions"
j: lambda: 1, # error: [invalid-type-form] "`lambda` expressions are not allowed in type expressions"
k: 1 if True else 2, # error: [invalid-type-form] "`if` expressions are not allowed in type expressions"
l: await baz(), # error: [invalid-type-form] "`await` expressions are not allowed in type expressions"
m: (yield 1), # error: [invalid-type-form] "`yield` expressions are not allowed in type expressions"
n: 1 < 2, # error: [invalid-type-form] "Comparison expressions are not allowed in type expressions"
o: bar(), # error: [invalid-type-form] "Function calls are not allowed in type expressions"
f: 1 and 2, # error: [invalid-type-form] "Boolean operations are not allowed in parameter annotations"
g: 1 or 2, # error: [invalid-type-form] "Boolean operations are not allowed in parameter annotations"
h: (foo := 1), # error: [invalid-type-form] "Named expressions are not allowed in parameter annotations"
i: not 1, # error: [invalid-type-form] "Unary operations are not allowed in parameter annotations"
j: lambda: 1, # error: [invalid-type-form] "`lambda` expressions are not allowed in parameter annotations"
k: 1 if True else 2, # error: [invalid-type-form] "`if` expressions are not allowed in parameter annotations"
l: await baz(), # error: [invalid-type-form] "`await` expressions are not allowed in parameter annotations"
m: (yield 1), # error: [invalid-type-form] "`yield` expressions are not allowed in parameter annotations"
n: 1 < 2, # error: [invalid-type-form] "Comparison expressions are not allowed in parameter annotations"
o: bar(), # error: [invalid-type-form] "Function calls are not allowed in parameter annotations"
# error: [unsupported-operator]
# error: [invalid-type-form] "F-strings are not allowed in type expressions"
# error: [invalid-type-form] "F-strings are not allowed in parameter annotations"
p: int | f"foo",
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in type expressions"
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in parameter annotations"
q: [1, 2, 3][1:2],
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in type expressions"
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in parameter annotations"
r: list[T][int],
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in type expressions"
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in parameter annotations"
s: list[list[T][int]],
):
reveal_type(a) # revealed: Unknown
Expand Down Expand Up @@ -270,25 +270,25 @@ def bar() -> None:
async def baz(): ...
async def outer_async(): # avoid unrelated syntax errors on `yield` and `await`
def _(
a: "1", # error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
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 this context in a type expression"
e: "1 and 2", # error: [invalid-type-form] "Boolean operations are not allowed in type expressions"
f: "1 or 2", # error: [invalid-type-form] "Boolean operations are not allowed in type expressions"
g: "(foo := 1)", # error: [invalid-type-form] "Named expressions are not allowed in type expressions"
h: "not 1", # error: [invalid-type-form] "Unary operations are not allowed in type expressions"
i: "lambda: 1", # error: [invalid-type-form] "`lambda` expressions are not allowed in type expressions"
j: "1 if True else 2", # error: [invalid-type-form] "`if` expressions are not allowed in type expressions"
k: "await baz()", # error: [invalid-type-form] "`await` expressions are not allowed in type expressions"
l: "(yield 1)", # error: [invalid-type-form] "`yield` expressions are not allowed in type expressions"
m: "1 < 2", # error: [invalid-type-form] "Comparison expressions are not allowed in type expressions"
n: "bar()", # error: [invalid-type-form] "Function calls are not allowed in type expressions"
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in type expressions"
a: "1", # error: [invalid-type-form] "Int literals are not allowed in this context in a parameter annotation"
b: "2.3", # error: [invalid-type-form] "Float literals are not allowed in parameter annotations"
c: "4j", # error: [invalid-type-form] "Complex literals are not allowed in parameter annotations"
d: "True", # error: [invalid-type-form] "Boolean literals are not allowed in this context in a parameter annotation"
e: "1 and 2", # error: [invalid-type-form] "Boolean operations are not allowed in parameter annotations"
f: "1 or 2", # error: [invalid-type-form] "Boolean operations are not allowed in parameter annotations"
g: "(foo := 1)", # error: [invalid-type-form] "Named expressions are not allowed in parameter annotations"
h: "not 1", # error: [invalid-type-form] "Unary operations are not allowed in parameter annotations"
i: "lambda: 1", # error: [invalid-type-form] "`lambda` expressions are not allowed in parameter annotations"
j: "1 if True else 2", # error: [invalid-type-form] "`if` expressions are not allowed in parameter annotations"
k: "await baz()", # error: [invalid-type-form] "`await` expressions are not allowed in parameter annotations"
l: "(yield 1)", # error: [invalid-type-form] "`yield` expressions are not allowed in parameter annotations"
m: "1 < 2", # error: [invalid-type-form] "Comparison expressions are not allowed in parameter annotations"
n: "bar()", # error: [invalid-type-form] "Function calls are not allowed in parameter annotations"
# error: [invalid-type-form] "Only simple names and dotted names can be subscripted in parameter annotations"
o: "[1, 2, 3][1:2]",
# error: [invalid-type-form] "Only simple names, dotted names and subscripts can be used in type expressions"
# error: [invalid-type-form] "Only simple names, dotted names and subscripts can be used in parameter annotations"
p: list[int].append,
# error: [invalid-type-form] "Only simple names, dotted names and subscripts can be used in type expressions"
# error: [invalid-type-form] "Only simple names, dotted names and subscripts can be used in parameter annotations"
q: list[list[int].append],
):
reveal_type(a) # revealed: Unknown
Expand Down Expand Up @@ -319,17 +319,17 @@ python-version = "3.12"

```py
def _(
a: {1: 2}, # error: [invalid-type-form] "Dict literals are not allowed in type expressions"
b: {1, 2}, # error: [invalid-type-form] "Set literals are not allowed in type expressions"
c: {k: v for k, v in [(1, 2)]}, # error: [invalid-type-form] "Dict comprehensions are not allowed in type expressions"
d: [k for k in [1, 2]], # error: [invalid-type-form] "List comprehensions are not allowed in type expressions"
e: {k for k in [1, 2]}, # error: [invalid-type-form] "Set comprehensions are not allowed in type expressions"
f: (k for k in [1, 2]), # error: [invalid-type-form] "Generator expressions are not allowed in type expressions"
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
a: {1: 2}, # error: [invalid-type-form] "Dict literals are not allowed in parameter annotations"
b: {1, 2}, # error: [invalid-type-form] "Set literals are not allowed in parameter annotations"
c: {k: v for k, v in [(1, 2)]}, # error: [invalid-type-form] "Dict comprehensions are not allowed in parameter annotations"
d: [k for k in [1, 2]], # error: [invalid-type-form] "List comprehensions are not allowed in parameter annotations"
e: {k for k in [1, 2]}, # error: [invalid-type-form] "Set comprehensions are not allowed in parameter annotations"
f: (k for k in [1, 2]), # error: [invalid-type-form] "Generator expressions are not allowed in parameter annotations"
# error: [invalid-type-form] "List literals are not allowed in this context in a parameter annotation"
g: [int, str],
# error: [invalid-type-form] "Tuple literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?"
# error: [invalid-type-form] "Tuple literals are not allowed in this context in a parameter annotation: Did you mean `tuple[int, str]`?"
h: (int, str),
i: (), # error: [invalid-type-form] "Tuple literals are not allowed in this context in a type expression: Did you mean `tuple[()]`?"
i: (), # error: [invalid-type-form] "Tuple literals are not allowed in this context in a parameter annotation: Did you mean `tuple[()]`?"
):
reveal_type(a) # revealed: Unknown
reveal_type(b) # revealed: Unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ from other import Literal
#
# ?
#
# error: [invalid-type-form] "Invalid subscript of object of type `_SpecialForm` in type expression"
# error: [invalid-type-form] "Invalid subscript of object of type `_SpecialForm` in a type expression"
a1: Literal[26]

def f():
Expand Down
Loading
Loading