I don't see much overlap between user-defined type guards and `typing.cast`. `cast` is never used for conditional type narrowing, nor can it affect control flow within a program. `cast` also doesn't involve any logic at runtime; it simply returns the value that it was passed. Plus, as you've pointed out, `cast` does not enforce that the output type is a narrower type than the input type.
A user-defined type guard is a user-defined function that is called in a conditional expression within an if/else statement. It contains logic that executes at runtime, and the bool response changes the flow of control in the program. At static type checking time, a user-defined type guard allows the type checker to understand more about the implied type of an expression within the if/else code blocks.
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: levkivskyi@gmail.com