How to extend your own verify() methods
April 19, 2026 ยท View on GitHub
Contents
Create a Custom Verify Method
One method is to create a custom verify() method for your particular situation.
For example, as we use it to handle json:
def verify_as_json(
object_to_verify: Any,
reporter: Reporter | None = None,
*, # enforce keyword arguments - https://www.python.org/dev/peps/pep-3102/
deserialize_json_fields: bool = False,
options: Options | None = None,
) -> None:
if deserialize_json_fields:
object_to_verify = utils.deserialize_json_fields(object_to_verify)
options = initialize_options(options, reporter)
json_text = utils.to_json(object_to_verify) + "\n"
verify(
json_text,
None,
encoding="utf-8",
newline="\n",
options=options.for_file.with_extension(".json"),
)
Create a Verifiable Object
Alternatively, you can create an object that knows how to verify itself. See the required interfaces:
And then just call verify(YourVarifiableObject)
If verify is called with an instance of Verifiable it will do a callback, allowing you to do whatever is needed,
for example:
def test_verifiable(self):
class MarkdownParagraph(Verifiable):
def __init__(self, title: str, text: str) -> None:
self.title = title
self.text = text
@override
def __str__(self) -> str:
return remove_indentation_from(
f"""
# {self.title}
{self.text}
"""
)
@override
def get_verify_parameters(self, options: Options) -> VerifyParameters:
return VerifyParameters(options.for_file.with_extension(".md"))
verify(
MarkdownParagraph("Paragraph Title", "This is where the paragraph text is.")
)