← Back to index

typeforms_typeform.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 15
FP: 25
FN: 1
Optional: 0 / 0
1"""
2Tests TypeForm functionality.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/type-forms.html#typeform
6
7import types
8from typing import Annotated, Any, ClassVar, Final, Literal, Optional, Self, TypeVarTuple, Unpack, assert_type
9from typing_extensions import TypeForm
12# > ``TypeForm[T]`` describes the set of all type form objects that represent
13# > the type ``T`` or types that are assignable to ``T``.
15ok1: TypeForm[str | None] = str | None # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
16ok2: TypeForm[str | None] = str # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
17ok3: TypeForm[str | None] = None # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
18ok4: TypeForm[str | None] = Literal[None] # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
19ok5: TypeForm[str | None] = Optional[str] # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
20ok6: TypeForm[str | None] = "str | None" # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
21ok7: TypeForm[str | None] = Any # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
23err1: TypeForm[str | None] = str | int # E
[invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
24err2: TypeForm[str | None] = list[str | None] # E
[invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
27# > ``TypeForm[Any]`` is assignable both to and from any other ``TypeForm`` type.
29v_any: TypeForm[Any] = int | str
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
30v_str: TypeForm[str] = str
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
31assert_type(v_any, TypeForm[Any])
Unexpected error [type-assertion-failure] Type `<types.UnionType special-form 'int | str'>` does not match asserted type `Unknown` [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
32assert_type(v_str, TypeForm[str])
Unexpected error [type-assertion-failure] Type `<class 'str'>` does not match asserted type `Unknown` [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
34v_any = v_str # OK
35v_str = v_any # OK
37# > The type expression ``TypeForm``, with no type argument provided, is
38# > equivalent to ``TypeForm[Any]``.
40def func1(x: TypeForm) -> None:
Unexpected error [invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
41 assert_type(x, TypeForm[Any])
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
44# > Valid type expressions are assignable to ``TypeForm`` through implicit TypeForm evaluation.
46v1_actual: types.UnionType = str | None # OK
47v1_type_form: TypeForm[str | None] = str | None # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
49v2_actual: types.GenericAlias = list[int] # OK
Unexpected error [invalid-assignment] Object of type `<class 'list[int]'>` is not assignable to `GenericAlias`
50v2_type_form: TypeForm = list[int] # OK
Unexpected error [invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
52v3: TypeForm[int | str] = Annotated[int | str, "metadata"] # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
53v4: TypeForm[set[str]] = "set[str]" # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
55func1(v3) # OK
56func1(v4) # OK
57func1(int) # OK
58func1("int") # OK
59func1("not a type") # E
Expected a ty diagnostic for this line
62# > Expressions that are not valid type expressions should not evaluate to a ``TypeForm`` type.
64Ts = TypeVarTuple("Ts")
65var = 1
67bad1: TypeForm = tuple() # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
68bad2: TypeForm = (1, 2) # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
69bad3: TypeForm = 1 # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
70bad4: TypeForm = Self # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
71bad5: TypeForm = ClassVar[int] # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
72bad6: TypeForm = Final[int] # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
73bad7: TypeForm = Unpack[Ts] # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
74bad8: TypeForm = Optional # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
75bad9: TypeForm = "int + str" # E
[invalid-type-form] Variable of type `_SpecialForm` is not allowed in a type expression
78# > ``TypeForm`` acts as a function that can be called with a single valid type expression.
80x1 = TypeForm(str | None)
Unexpected error [call-non-callable] Object of type `_SpecialForm` is not callable
81assert_type(x1, TypeForm[str | None])
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
83x2 = TypeForm("list[int]")
Unexpected error [call-non-callable] Object of type `_SpecialForm` is not callable
84assert_type(x2, TypeForm[list[int]])
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
86x3 = TypeForm("type(1)") # E
[call-non-callable] Object of type `_SpecialForm` is not callable
87x4 = type(1) # OK
88x5 = TypeForm(type(1)) # E
[call-non-callable] Object of type `_SpecialForm` is not callable
91# > ``TypeForm`` is covariant in its single type parameter.
93def get_type_form() -> TypeForm[int]:
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
94 return int
97t1: TypeForm[int | str] = get_type_form() # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
98t2: TypeForm[str] = get_type_form() # E
[invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
101# > ``type[T]`` is a subtype of ``TypeForm[T]``.
103def get_type() -> type[int]:
104 return int
107t3: TypeForm[int | str] = get_type() # OK
Unexpected error [invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression
108t4: TypeForm[str] = get_type() # E
[invalid-type-form] Invalid subscript of object of type `_SpecialForm` in type expression