Typing and Generics
typing.cast
Tells a static checker to treat val as typ. Returns val unchanged without checking, converting or copying it. Validate untrusted input instead of relying on a cast.
Formal Syntax & Structure
python syntaxpython
typing.cast(typ, val)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| typ | type expression | Requested static type. |
| val | object | Value returned unchanged. |
Return Value
object
Typing construct or runtime result as described; static typing rules are not runtime validation.
Working Example
Example Snippetpython
from typing import cast
value: object = ['a']result=cast(list[str],value)print(result is value)