Files
ruff/crates/red_knot_python_semantic/resources/mdtest/mdtest_config.md
David Peter b01a651e69 [red-knot] Support for TOML configs in Markdown tests (#14785)
## Summary

This adds support for specifying the target Python version from a
Markdown test. It is a somewhat limited ad-hoc solution, but designed to
be future-compatible. TOML blocks can be added to arbitrary sections in
the Markdown block. They have the following format:

````markdown
```toml
[tool.knot.environment]
target-version = "3.13"
```
````

So far, there is nothing else that can be configured, but it should be
straightforward to extend this to things like a custom typeshed path.

This is in preparation for the statically-known branches feature where
we are going to have to specify the target version for lots of tests.

## Test Plan

- New Markdown test that fails without the explicitly specified
`target-version`.
- Manually tested various error paths when specifying a wrong
`target-version` field.
- Made sure that running tests is as fast as before.
2024-12-06 10:22:08 +01:00

1.3 KiB

This test makes sure that red_knot_test correctly parses the TOML configuration blocks and applies the correct settings hierarchically.

The following configuration will be attached to the root section (without any heading):

[environment]
target-version = "3.10"

Basic

Here, we simply make sure that we pick up the global configuration from the root section:

reveal_type(sys.version_info[:2] == (3, 10))  # revealed: Literal[True]

Inheritance

Child

Grandchild

The same should work for arbitrarly nested sections:

reveal_type(sys.version_info[:2] == (3, 10))  # revealed: Literal[True]

Overwriting

Here, we make sure that we can overwrite the global configuration in a child section:

[environment]
target-version = "3.11"
reveal_type(sys.version_info[:2] == (3, 11))  # revealed: Literal[True]

No global state

There is no global state. This section should again use the root configuration:

reveal_type(sys.version_info[:2] == (3, 10))  # revealed: Literal[True]

Overwriting affects children

Children in this section should all use the section configuration:

[environment]
target-version = "3.12"

Child

Grandchild

reveal_type(sys.version_info[:2] == (3, 12))  # revealed: Literal[True]