Re: Proposal: typing.assert_type() and with typing.assert_error()

I like both of these suggestions. I use both assertions on revealed types and on yielded errors on the types in phantom-types. The names are clear to me, and the fact they'd be namespaced under "typing" makes it blatantly clear that they are related to concepts of typing. I'd suggest defining assert_error() to accept arbitrary kwargs, so that context can be passed in specific for type checkers, e.g.: assert_error(mypy=["arg-type"], pyright=[...])

From a library author's perspective, I have long wanted something akin to `assert_error` so that I can ensure that my typed interfaces do not permit false negatives among static type-checkers. I was surprised to see Eric's tip that one can combine `# type: ignore` with the ability for type-checkers to raise when these comments are used unnecessarily -- this achieves exactly the functionality I have been hoping for! That being said, this solution is far too obscure; I have been searching for such a solution for months, and only found it, by chance, by lurking on this thread. FWIW I prefer this sort of pattern pattern over the use of a context manager, via `assert_error`. That being said, there is certainly a need for well-documented, standardized ways to check that typed code is being understood correctly by type-checkers.
participants (2)
-
Anton Agestam
-
rsoklaski@gmail.com