Life of an mdtest

Red Knot uses mdtests extensively to test the type checker.

mdtests README [astral-sh/ruff]

The ‘red_knot_test’ crate is the harness that parses a Markdown file, extracts all of test cases in the file, and runs them.

The ‘red_knot_python_semantic’ crate has an ‘mdtest’ test suite that uses this harness to run all of the tests in every markdown file in the ‘resources/mdtest’ directory. (It uses the ‘dir-test’ crate to handle the “run a Rust test for each file in a directory” part.)

dir-test crate

High-level steps

For each file in the test:

Python abstract grammar

Checking types

To “check the types” of a module:

Note that we don't pass the parsed representation of the file into ‘check_types’. The ‘semantic_index’ function calls ‘parsed_module’ again, relying on salsa caching to return the already parsed AST.
» Languages » Python » Red Knot