2Tests the typing.assert_type function.
5# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#assert-type
7from typing import Annotated, Any, Literal, assert_type
10# > When a type checker encounters a call to assert_type(), it should
11# > emit an error if the value is not of the specified type.
18 d: "ForwardReference",
19 e: Annotated[Literal[4], ""],
21 assert_type(a, int | str) # OK
22 assert_type(b, list[int]) # OK
23 assert_type(c, Any) # OK
24 assert_type(d, "ForwardReference") # OK
25 assert_type(e, Literal[4]) # OK
27 assert_type(a, int) # E: Type mismatch
[type-assertion-failure] Type `int` does not match asserted type `int | str`
28 assert_type(a, Any) # E: Type mismatch
[type-assertion-failure] Type `Any` does not match asserted type `int | str`
29 assert_type(c, int) # E: Type mismatch
[type-assertion-failure] Type `int` does not match asserted type `Any`
30 assert_type(e, int) # E: Type mismatch
[type-assertion-failure] Type `int` does not match asserted type `Literal[4]`
32 assert_type() # E: not enough arguments
[missing-argument] No arguments provided for required parameters `value`, `type` of function `assert_type`
33 assert_type("", int) # E: wrong argument type
[type-assertion-failure] Type `int` does not match asserted type `Literal[""]`
34 assert_type(a, int | str, a) # E: too many arguments
[too-many-positional-arguments] Too many positional arguments to function `assert_type`: expected 2, got 3
37# > If the two types are :term:`equivalent` but syntactically different,
38# > the type checker may reject the ``assert_type()`` call::
41 assert_type(name, str | Literal["spam"]) # E?: Equivalent but not identical
44class ForwardReference: