Context manager that can be used to keep track of new errors recorded around a given operation. Errors maintain a stack of such watchers. The handler is called starting at the top of the stack, and is propagated down the stack unless filtered out by one of the ErrorWatcher instances
| 201 | |
| 202 | |
| 203 | class ErrorWatcher: |
| 204 | """Context manager that can be used to keep track of new errors recorded |
| 205 | around a given operation. |
| 206 | |
| 207 | Errors maintain a stack of such watchers. The handler is called starting |
| 208 | at the top of the stack, and is propagated down the stack unless filtered |
| 209 | out by one of the ErrorWatcher instances. |
| 210 | """ |
| 211 | |
| 212 | # public attribute for the special treatment of `reveal_type` by |
| 213 | # `MessageBuilder.reveal_type`: |
| 214 | filter_revealed_type: bool |
| 215 | |
| 216 | def __init__( |
| 217 | self, |
| 218 | errors: Errors, |
| 219 | *, |
| 220 | filter_errors: bool | Callable[[str, ErrorInfo], bool] = False, |
| 221 | save_filtered_errors: bool = False, |
| 222 | filter_deprecated: bool = False, |
| 223 | filter_revealed_type: bool = False, |
| 224 | ) -> None: |
| 225 | self.errors = errors |
| 226 | self._has_new_errors = False |
| 227 | self._filter = filter_errors |
| 228 | self._filter_deprecated = filter_deprecated |
| 229 | self.filter_revealed_type = filter_revealed_type |
| 230 | self._filtered: list[ErrorInfo] | None = [] if save_filtered_errors else None |
| 231 | |
| 232 | def __enter__(self) -> Self: |
| 233 | self.errors._watchers.append(self) |
| 234 | return self |
| 235 | |
| 236 | def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> Literal[False]: |
| 237 | last = self.errors._watchers.pop() |
| 238 | assert last == self |
| 239 | return False |
| 240 | |
| 241 | def on_error(self, file: str, info: ErrorInfo) -> bool: |
| 242 | """Handler called when a new error is recorded. |
| 243 | |
| 244 | The default implementation just sets the has_new_errors flag |
| 245 | |
| 246 | Return True to filter out the error, preventing it from being seen by other |
| 247 | ErrorWatcher further down the stack and from being recorded by Errors |
| 248 | """ |
| 249 | if info.code == codes.DEPRECATED: |
| 250 | # Deprecated is not a type error, so it is handled on opt-in basis here. |
| 251 | if not self._filter_deprecated: |
| 252 | return False |
| 253 | |
| 254 | self._has_new_errors = True |
| 255 | if isinstance(self._filter, bool): |
| 256 | should_filter = self._filter |
| 257 | elif callable(self._filter): |
| 258 | should_filter = self._filter(file, info) |
| 259 | else: |
| 260 | raise AssertionError(f"invalid error filter: {type(self._filter)}") |
no outgoing calls
no test coverage detected
searching dependent graphs…