Skip to content

[ty] Improve .toml support in the ty playground#23476

Merged
MichaReiser merged 3 commits intoastral-sh:mainfrom
sinon:toml-support
Mar 17, 2026
Merged

[ty] Improve .toml support in the ty playground#23476
MichaReiser merged 3 commits intoastral-sh:mainfrom
sinon:toml-support

Conversation

@sinon
Copy link
Contributor

@sinon sinon commented Feb 22, 2026

Closes: astral-sh/ty#2671

Summary

Adds clearer toml support to the playground editor. This makes replicating issues in playground where users have a pre-existing ty.toml or pyproject.toml more straight forward. Note: ty-wasm already detected and worked with these files in the playground and respected these settings files without any additional change, so this change is more focussed on the visual fidelity of the experience.

Consists of two main changes:

  • Add TOML svg (this follows the existing pattern of vendoring the svg data from material-extensions/vscode-material-icon-theme and including attribution)
  • Add an override that when .toml extension is encountered the ruby syntax highlight is set. There is currently no in-built Monarch grammar or off the shelf 3rd party one that can be added as a dependency. I did have Claude have a bash at generating the grammar it produced something complex and probably correct but for now I opted for setting the ruby grammar as this gives the basic syntax highlighting needed for most common uses in ty config, without the potential maintenance overhead. This is recommended on the toml feature request issue as the best fallback: TOML language support microsoft/monaco-editor#2798
  • A new TOML Monarch grammar written and tested with LLM assistance see: [ty] Improve .toml support in the ty playground #23476 (comment) for testing info

Test Plan

  • Run npm start --workspace ty-playground
  • Delete ty.json
  • Create ty.toml
    • verify adjusting settings has the expected affect on the typechecking rules enabled
    • verify that icon appears
    • verify that tables / strings are syntax highlighted with the existing Ayu theme.
    • Togggle light/dark mode
  • Delete ty.toml create a pyproject.toml repeat the verification steps from ty.toml
image

@AlexWaygood AlexWaygood added playground A playground-specific issue ty Multi-file analysis & type inference labels Feb 22, 2026
@carljm carljm requested a review from MichaReiser February 24, 2026 17:34
@MichaReiser
Copy link
Member

Thanks for looking into this.

The lack of TOML support in Monaco is the main reason the playground uses JSON for its configuration and it's a pity that Claude didn't manage to write a grammar for TOML.

I'm not sure we should make this change for two reasons:

  • I think it can lead to a very inconsistent experience if a ty.json is present, as that always takes precedence (see here). While this is a pre-existing issue, promoting TOML support gives the impression that it is supported when this isn't fully the case
  • I don't think we should use the Ruby grammar. Its highlighting isn't much better than recognizing strings, which a custom grammar should be able to do as easily.

What I did in Ruff's playground was add support for pasting a TOML configuration (or copying it as TOML). Although I don't think it's a feature commonly used. Although I don't know how commonly this feature is used.

@sinon
Copy link
Contributor Author

sinon commented Mar 4, 2026

The lack of TOML support in Monaco is the main reason the playground uses JSON for its configuration and it's a pity that Claude didn't manage to write a grammar for TOML.

Sorry, maybe I wasn't clear in PR description Claude wrote a grammar that appeared to work correctly in my testing but I wasn't sure about the first draft of this being: "Here's some AI code, I have manually tested it but don't have the background to self-review in the depth I would like, want to take on the maintenance burden for it? 😉"

Here is a Gist of it: https://gist.github.com/sinon/d3840c6cba19a5d744c9b4a4f5a23dc6

@MichaReiser
Copy link
Member

I'm fine with an AI generated-grammar. I'd appreciate it if you read through it to double-check that it makes sense overall.

This would bring TOML support close to JSON, without supporting completions. But adding completions is rather involved because it requires schema parsing etc.

After second consideration. I'd be fine merging this PR with an updated TOML grammar but I still think that JSON should be the default because it provides a better experience overall.

@sinon sinon marked this pull request as draft March 16, 2026 15:43
@sinon
Copy link
Contributor Author

sinon commented Mar 16, 2026

Re-added the LLM generated grammar, with some tweaks from testing with the following toml file (with some ty specific toml that didn't initially render right with the grammar from the gist)

# TOML v1.0 syntax fixture for manually testing the playground tokenizer.
# Paste this into a `.toml` file in the editor to exercise comments, keys,
# strings, numbers, booleans, arrays, inline tables, tables, and array tables.

title = "TOML syntax fixture"
summary = "Exercises most TOML value forms, punctuation, and nesting." # Inline comment

bare_key = "bare key"
bare-key = "bare key with hyphen"
1234 = "numeric bare key"
"quoted key" = "double-quoted key"
'literal key' = "single-quoted key"
dotted.key.path = "dotted bare keys"
"quoted"."dotted".path = "quoted dotted keys"

empty_string = ""
basic_string = "Escapes: \b \t \n \f \r \" \\ \u03B1 \U0001F680"
literal_string = 'C:\Users\tester\Documents\config.toml'
multiline_basic_string = """
Roses are red
Violets are blue \
this line continues on the same logical line.
Escapes still work here: \n \t \u03B1
"""
multiline_literal_string = '''
Line one
Line two with backslashes left untouched: C:\Users\tester\Documents\config.toml
'''

positive_integer = +99
negative_integer = -17
underscored_integer = 1_000_000
hex_integer = 0xDEADBEEF
octal_integer = 0o755
binary_integer = 0b1101_0010

float_value = 3.1415
signed_float = +1.0
exponent_float = 5e+22
fractional_float = -0.01
underscored_float = 224_617.445_991_228
positive_inf = +inf
negative_inf = -inf
quiet_nan = nan

bool_true = true
bool_false = false

offset_datetime = 1979-05-27T07:32:00Z
offset_datetime_with_fraction = 1979-05-27T00:32:00.999999-07:00
local_datetime = 1979-05-27T07:32:00
local_date = 1979-05-27
local_time = 07:32:00.123

simple_array = [1, 2, 3]
string_array = ["red", "yellow", "green"]
nested_array = [[1, 2], [3, 4, 5]]
boolean_array = [true, false, true]
inline_table = { name = "Ruff", type = "playground", stable = true }
nested_inline_table = { database = { server = "192.168.1.1", ports = [8001, 8001, 8002] } }

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "Likes # inside strings because it should not start a comment."
dob = 1979-05-27T07:32:00Z

[database]
server = "192.168.1.1"
ports = [8000, 8001, 8002]
connection_max = 5000
enabled = true
temp_targets = { cpu = 79.5, case = 72.0 }

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

[clients]
data = [["gamma", "delta"], [1, 2]]
hosts = ["alpha", "omega"]

[tool.ty.environment]
extra-paths = ["./shared/my-search-path"]
include = false

[[products]]
name = "Hammer"
sku = 738594937

[[products]]
name = "Nail"
sku = 284758393
color = "gray"

[[tool.ty.overrides]]
include = ["tests/**", "**/test_*.py"]
image image

@sinon sinon marked this pull request as ready for review March 16, 2026 19:06
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@MichaReiser MichaReiser merged commit 801cc9e into astral-sh:main Mar 17, 2026
43 checks passed
carljm added a commit that referenced this pull request Mar 17, 2026
* main:
  [ty] Filter out unsatisfiable inference attempts during generic call narrowing (#24025)
  [ty] Avoid inferring intersection types for call arguments (#23933)
  [ty] Pin mypy_primer in `setup_primer_project.py` (#24020)
  [`pycodestyle`] Recognize `pyrefly:` as a pragma comment (`E501`) (#24019)
  Add company AI policy to contributing guide (#24021)
  [ty] Remove the mypy_primer CI workflow (#24016)
  Update prek dependencies (#23980)
  [ty] Smarter semantic token classification for attribute access on union type (#23841)
  [ty] ecosystem-analyzer: Inline diffs and panic messages (#24015)
  [ty] Improve `.toml` support in the ty playground (#23476)
  PEP 639 license information (#19661)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

playground A playground-specific issue ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support TOML configuration in the playground

3 participants