yamllint

1.38.0 · active · verified Sun Mar 29

yamllint is a linter for YAML files written in Python, version 1.38.0. It checks not only for syntax validity but also for common weirdnesses like key repetition and cosmetic issues such as line length, trailing spaces, and indentation. The project is actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically lint a YAML string using `yamllint`. It creates a `YamlLintConfig` and then uses `linter.run` with an `io.StringIO` object to process the YAML content, iterating over any `LintProblem` objects found.

import io
from yamllint.config import YamlLintConfig
from yamllint import linter

yaml_content = """
key: value 
another_key:   with_trailing_spaces
  nested: item
"""

# Use a default configuration or load a custom one
config_string = "extends: default\nrules:\n  trailing-spaces: enable\n  indentation: enable"
conf = YamlLintConfig(content=config_string)

# Lint the YAML content from a string buffer
problems = linter.run(io.StringIO(yaml_content), conf)

found_issues = []
for p in problems:
    found_issues.append(f"Line {p.line}, Column {p.column}: {p.desc} (Rule: {p.rule})")

if found_issues:
    print("YAML linting issues found:")
    for issue in found_issues:
        print(issue)
else:
    print("No YAML linting issues found.")

view raw JSON →