Skip to main content

Intelligent syntax highlighting and validation for Python template strings (PEP 750)

Project description

t-linter 🐍✨

Intelligent syntax highlighting and validation for Python template strings (PEP 750).

License: MIT VSCode Marketplace PyPI

📣 💼 Maintainer update: Open to opportunities. 🔗 koxudaxi.dev

📖 Documentation

👉 t-linter.koxudaxi.dev


Overview

t-linter provides intelligent syntax highlighting and linting for Python template strings (PEP 750) through multiple distribution channels:

  • 🔧 Command-line tool: Install via PyPI (pip install t-linter) for direct CLI usage and LSP server
  • 🎨 VSCode Extension: Install from the Visual Studio Code Marketplace for seamless editor integration

T-Linter VSCode Extension in action

Features

  • 🎨 Smart Syntax Highlighting - Detects embedded languages in t"..." strings
  • 🔍 Type-based Detection - Understands Annotated[Template, "html"] annotations
  • 🚀 Fast - Built with Rust and Tree-sitter for optimal performance
  • 🔧 Extensible - Support for HTML, T-HTML, SQL, JavaScript, CSS, JSON, YAML, TOML, and more

For HTML, T-HTML, JSON, YAML, and TOML, t-linter now splits responsibilities:

  • semanticTokens: Tree-sitter only, for low-latency highlighting
  • check: strict parsing through the tstring-html, tstring-thtml, tstring-json, tstring-yaml, and tstring-toml backends
  • formatting: canonical formatting through the same Rust backends

Installation

Option 1: VSCode Extension (Recommended for VSCode users)

Step 1: Install the t-linter binary Install t-linter as a project dependency (recommended):

pip install t-linter

For better project isolation, add it to your project's requirements:

# Using pip with requirements.txt
echo "t-linter" >> requirements.txt
pip install -r requirements.txt

# Or using uv (recommended for faster installs)
uv add t-linter

Step 2: Install the VSCode extension Install the extension from the Visual Studio Code Marketplace:

  1. Open VSCode
  2. Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
  3. Search for "t-linter"
  4. Click Install on "T-Linter - Python Template Strings Highlighter & Linter" by koxudaxi

Step 3: Disable Python Language Server To prevent conflicts with t-linter's syntax highlighting, disable the Python language server:

  1. Open VSCode Settings (Ctrl+, / Cmd+,)
  2. Search for "python.languageServer"
  3. Set it to "None"

Alternatively, add to your settings.json:

{
    "python.languageServer": "None"
}

Step 4: Configure the server path (if needed) If t-linter is not in your PATH, configure the server path in VSCode settings:

  1. Find your t-linter path by running in terminal:

    which t-linter     # macOS/Linux
    where t-linter     # Windows
    
  2. Open VSCode Settings (Ctrl+, / Cmd+,)

  3. Search for "t-linter.serverPath"

  4. Set the full path to your t-linter executable:

    • Windows: C:\Users\YourName\AppData\Local\Programs\Python\Python3xx\Scripts\t-linter.exe
    • macOS: /Users/yourname/.local/bin/t-linter or /usr/local/bin/t-linter
    • Linux: /home/yourname/.local/bin/t-linter or /usr/local/bin/t-linter

→ Install from VSCode Marketplace

Option 2: PyPI Package Only (CLI tool and LSP server)

For command-line usage or integration with other editors, install t-linter as a project dependency:

pip install t-linter

Or add to your project's dependencies:

# Using requirements.txt
t-linter

# Using uv
uv add t-linter

# Or manually in pyproject.toml
[project]
dependencies = [
    "t-linter",
    # other dependencies...
]

This provides the t-linter command-line tool and LSP server without the VSCode extension.

→ View on PyPI

Option 3: Build from Source

For development or bleeding-edge features:

git clone https://github.com/koxudaxi/t-linter
cd t-linter
cargo install --path crates/t-linter

Usage

VSCode Extension

After installing both the PyPI package and VSCode extension, t-linter will automatically provide syntax highlighting for Python template strings.

Troubleshooting: If syntax highlighting doesn't work:

  1. Ensure t-linter is installed: Run t-linter --version in terminal
  2. Check that Python language server is disabled: python.languageServer should be set to "None"
  3. Check the server path in VSCode settings: t-linter.serverPath
  4. Restart VSCode after making changes

Command Line Interface

If you installed via PyPI, you can use t-linter from the command line:

Run the language server (for editor integration):

t-linter lsp

In the LSP, diagnostics are debounced and published from the dedicated Rust backends for HTML, T-HTML, JSON, YAML, and TOML templates. Formatting requests rewrite the whole template literal using the backend formatter while keeping interpolation source such as {name!r:>5} intact.

Check files for issues:

# Check Python files for template string issues
t-linter check file.py
t-linter check src/

# Output formats
t-linter check file.py --format json
t-linter check file.py --format github  # GitHub Actions annotations
t-linter check file.py --error-on-issues  # Exit with error code if issues found

check supports human, json, and github output formats.

check --format controls report output formatting only. Use the format subcommand to rewrite supported template literals in place:

# Format Python files containing HTML/T-HTML/JSON/YAML/TOML templates
t-linter format file.py
t-linter format src/

# Check whether formatting would change any files
t-linter format --check file.py

# Override the formatter line length
t-linter format --line-length 100 file.py

# Format stdin
cat file.py | t-linter format --stdin-filename file.py -

Configuration can be provided via pyproject.toml:

[tool.t-linter]
line-length = 80
extend-exclude = ["generated", "vendor"]
ignore-file = ".t-linterignore"

Supported keys:

  • line-length: formatter print width for HTML and T-HTML templates only
  • exclude: override the built-in default excludes
  • extend-exclude: add more exclude patterns on top of the defaults
  • ignore-file: path to a gitignore-style ignore file, relative to the project root

By default, t-linter also reads .t-linterignore from the project root if it exists.

Exit codes:

  • 0: Run completed successfully
  • 1: Issues were found and --error-on-issues was set
  • 2: Operational failure such as an unreadable file

Example input:

from typing import Annotated
from string.templatelib import Template

payload: Annotated[Template, "json"] = t"""[1,,2]"""

Example human output:

example.py:4:46: error[embedded-parse-error] Invalid json syntax in template string (language=json)
1 files scanned, 1 templates scanned, 1 diagnostics, 0 failed files

Example json output:

{
  "files": [
    {
      "file": "example.py",
      "template_count": 1,
      "diagnostics": [
        {
          "rule": "embedded-parse-error",
          "severity": "error",
          "language": "json",
          "message": "Invalid json syntax in template string",
          "file": "example.py",
          "start_line": 4,
          "start_column": 46,
          "end_line": 4,
          "end_column": 47
        }
      ]
    }
  ],
  "diagnostics": [
    {
      "rule": "embedded-parse-error",
      "severity": "error",
      "language": "json",
      "message": "Invalid json syntax in template string",
      "file": "example.py",
      "start_line": 4,
      "start_column": 46,
      "end_line": 4,
      "end_column": 47
    }
  ],
  "summary": {
    "files_scanned": 1,
    "templates_scanned": 1,
    "diagnostics": 1,
    "failed_files": 0
  }
}

Example github output:

::error file=example.py,line=4,col=46,title=t-linter(embedded-parse-error)::Invalid json syntax in template string (language=json)

Get template string statistics (🚧 Coming soon):

# Analyze template string usage in your codebase
t-linter stats .  # Current directory
t-linter stats src/  # Specific directory

# Expected output (when implemented):
# - Number of template strings by language
# - Template string locations
# - Language detection methods used
# - Type alias usage statistics

Roadmap

Planned Features

  • Language Server Protocol (LSP) - Fully implemented
  • Syntax Highlighting - Supports HTML, T-HTML, SQL, JavaScript, CSS, JSON, YAML, TOML
  • Type Alias Support - Recognizes type html = Annotated[Template, "html"]
  • Linting (check command) - Validate template strings for syntax errors
  • 🚧 Statistics (stats command) - Analyze template string usage across codebases
  • 📋 Cross-file Type Resolution - Track type aliases across module boundaries
  • 📋 Auto-completion - Context-aware completions within template strings

Quick Start Example

Here's how to use template strings with automatic syntax highlighting:

from typing import Annotated
from string.templatelib import Template

# HTML template with syntax highlighting
page: Annotated[Template, "html"] = t"""
<!DOCTYPE html>
<html>
    <head>
        <title>{title}</title>
        <style>
            body { font-family: Arial, sans-serif; }
            .highlight { color: #007acc; }
        </style>
    </head>
    <body>
        <h1 class="highlight">{heading}</h1>
        <p>{content}</p>
    </body>
</html>
"""

# SQL query with syntax highlighting
query: Annotated[Template, "sql"] = t"""
SELECT u.name, u.email, p.title 
FROM users u 
JOIN posts p ON u.id = p.author_id 
WHERE u.created_at > {start_date}
ORDER BY u.name
"""

# T-HTML template with component syntax
type thtml = Annotated[Template, "thtml"]
card: thtml = t"""
<Card title="{title}">
    <Badge tone="success">{status}</Badge>
</Card>
"""

# Type aliases for reusable templates
type css = Annotated[Template, "css"]
type js = Annotated[Template, "javascript"]
type yaml_config = Annotated[Template, "yaml"]
type toml_config = Annotated[Template, "toml"]

styles: css = t"""
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: {padding}px;
}
"""

settings: yaml_config = t"""
app:
  name: {app_name}
  debug: true
"""

pyproject: toml_config = t"""
[project]
name = "{project_name}"
version = "{version}"
"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

t_linter-0.5.1.tar.gz (84.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

t_linter-0.5.1-py3-none-win_amd64.whl (3.7 MB view details)

Uploaded Python 3Windows x86-64

t_linter-0.5.1-py3-none-manylinux_2_28_x86_64.whl (4.3 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

t_linter-0.5.1-py3-none-manylinux_2_28_aarch64.whl (4.2 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

t_linter-0.5.1-py3-none-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file t_linter-0.5.1.tar.gz.

File metadata

  • Download URL: t_linter-0.5.1.tar.gz
  • Upload date:
  • Size: 84.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for t_linter-0.5.1.tar.gz
Algorithm Hash digest
SHA256 4c952f1d5a387d6f1e2f805e4377346cc038a05aa299d2add5df0006f7ef4cd8
MD5 753543dd4cbaa2e736e11f9538ce8a60
BLAKE2b-256 5d725b37ee19d7a2a83ba03797ded290a5504cbf1765f9545218161a87b4cb75

See more details on using hashes here.

File details

Details for the file t_linter-0.5.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: t_linter-0.5.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for t_linter-0.5.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 374186facda011168df034002f7bc1ce9b2013a68d011a04bb066caefb20617e
MD5 246bdcc83d2c415c69a20b31ca06401d
BLAKE2b-256 0796235b65c006e1ac47f38f8b72d3d74cf4bd73f660d6a253d480e0faaa6c16

See more details on using hashes here.

File details

Details for the file t_linter-0.5.1-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for t_linter-0.5.1-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a3a0afab309b88efbde5840bcb1b3475e74d454a06365e67eb501aafcc1ee060
MD5 b620a1983b33ca9fcaa779728910c8d9
BLAKE2b-256 7b4a540d16e2fd5c5905909546043858970024884e31f3cc0bfcfffc0db28369

See more details on using hashes here.

File details

Details for the file t_linter-0.5.1-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for t_linter-0.5.1-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 739372f81c3e940dabbacfb2dd29b34bd8bda4d232ea5dee1041328983b47b96
MD5 8c85231b0611d1b2cec0f1f3b8990e03
BLAKE2b-256 912e5ffaba7a5bb15dc126adb270c308c441d59d065d60e7d46814bbc791e122

See more details on using hashes here.

File details

Details for the file t_linter-0.5.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for t_linter-0.5.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c76c47501323b3adfa291142dfa0e03701d57e814538a907ea5f2dee63197712
MD5 a9535d55b0bcbf687b2a7ceca90e0571
BLAKE2b-256 6e17878d7483d8fc7f2f919ac047fe2f5e56cb5e054d6659a0b90e80026d88ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page