Vale Python Package

3.13.0.0 · active · verified Thu Apr 16

The `vale` Python package provides a convenient way to install and use the Vale command-line grammar and style checking tool within Python environments. Vale itself is a static analysis tool for prose, written in Go. This Python package acts as a wrapper, automatically downloading the appropriate Vale binary on first execution. The package aims to simplify the inclusion of Vale as a dependency in Python applications or libraries, without requiring manual installation of the Vale CLI.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Vale` object and lint a string. Note that Vale requires a `.vale.ini` configuration file and a `StylesPath` directory to operate correctly. This example creates minimal temporary configuration files. In a real application, you would manage these configuration files within your project structure. The Vale CLI binary is downloaded the first time `vale.lint()` is called, which might take a moment.

import os
from vale import Vale

# Vale requires a .vale.ini config file and a StylesPath directory.
# For a quickstart, we'll create minimal ones in a temporary directory.
# In a real project, these would be managed in your project structure.

config_content = """
StylesPath = styles
MinAlertLevel = warning

[*.md]
BasedOn = proselint
"""

style_content = """
# styles/proselint/SomeRule/Vocab.yml
# Minimal example rule (if you had a custom rule)
"""

# Create a temporary directory structure
# In a real application, you'd manage config_dir and style_dir appropriately
import tempfile
import shutil

try:
    with tempfile.TemporaryDirectory() as temp_dir:
        config_dir = os.path.join(temp_dir, ".vale")
        styles_dir = os.path.join(config_dir, "styles", "proselint")
        os.makedirs(styles_dir, exist_ok=True)

        with open(os.path.join(config_dir, ".vale.ini"), "w") as f:
            f.write(config_content)
        
        # You'd typically install actual styles here or reference system-wide ones
        # For this quickstart, we just need the directories to exist.

        vale_instance = Vale(config_path=os.path.join(config_dir, ".vale.ini"))

        text_to_lint = "This is a very simple test. I will be using bad grammar."

        # Lint the text. Output format is typically JSON.
        # The default output is a list of dictionaries, one for each alert.
        alerts = vale_instance.lint(text_to_lint, syntax='md')

        print(f"Found {len(alerts)} alerts:")
        for alert in alerts:
            print(f"  - [{alert['Severity']}] {alert['Message']} (Rule: {alert['Check']})")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure Vale CLI is successfully downloaded on first run and config is valid.")

view raw JSON →