the.bay.news

Suggestion: add a typing.assert_cast() function, which just does assert isinstance(x, T)

Discussions on Python.org
Suggestion: add a typing.assert_cast() function, which just does assert isinstance(x, T)
I’m regularly defining this function, which helps make my code shorter and easier to read. I think it may be a good idea to add something like this to the typing module: def assert_cast[T](t: type[T], x: object) -> T: if not isinstance(x, t): raise TypeError(f'Expecting {x!r} to be of type {t!r}') return x It is very useful when I want my code to type-check, and I want a quick failure if my assumptions aren’t satisfied. This comes a lot when parsing objects coming from json, an...

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.