Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type annotate format checker methods
The goal of this work is to apply annotations in `jsonschema._format` which match the new additions to the type stubs recently added to typeshed. In short, it defines a `_FormatCheckCallable` as a callable -- typically function -- which takes any object and returns a bool. All of the `is_*` functions are `_FormatCheckCallable`s, and the decorators are carefully annotated as being type-preserving using a typevar. Because the returned objects from these functions are not always bools, this changeset now calls `bool()` on those which are not (e.g. `ipaddress.IPv4Address`). This is really just an explicit form of the check which is going to happen in `conforms` and `check` anyway, so there's no significant new cost. The advantage of this is that we have documented (via the annotations) what a format check function is supposed to do: it returns a bool. We could equally well return `Any` from these functions, relying on `__bool__`, but this could confuse new contributors and users. One unfortunate side effect of these changes is that `FormatChecker.cls_checks` needs to be expanded into a full duplicate of `FormatChecker.checks`. `mypy` isn't able to properly understand what `cls_checks = classmethod(checks)` does -- this is part of a class of semi-sophisticated callable and method manipulations that are known to be problematic for `mypy`. In order to get the annotations correct, the simplest solutions are either to annotate it explicitly (cast or type comment) or to expand it as this changeset has done.
- Loading branch information
Showing
1 changed file
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters