On Sun, Jan 23, 2022, at 02:06, Jelle Zijlstra wrote:
> def assert_type(obj: T, typ: TypeForm[T], /) -> T: ...
assert_type seems very useful to me.
> def assert_error() -> ContextManager[None]: ...
This feels less right to me:
- `assert_error` is too generic a name, doesn't sound like it's related to typing.
- Usually (in mypy at least) you'd also want to assert the error code
- Often you'd want to use it in a unittest, for example in my experience I'd often want to use this when testing that a function raises a TypeError given a bad argument. Then, you'd need two context managers, e.g `with pytest.raises(TypeError), assert_error():.
- In practice you want the scope of the `assert_error` to be as tight as possible so as not to "over assert", but the CM encourages putting more than necessary under it.
The idea was to add a new type comment `type: ignore-expected[...]`, which ignores if the error code is emitted by the line, and errors if the error code is not emitted by the line. I think the meaning would be intuitive to a reader, assuming they're familiar with `type: ignore`.