Skip to content

TypeError in indentation rule when parsing invalid YAML file #771

@scharfy

Description

@scharfy

When running yamllint (v1.37.1) on Python 3.11, I encountered a TypeError in the indentation rule when processing a specific YAML file used in my unit tests.
The file is intentionally invalid YAML (garbage content).

workflow-invalid.yml

THI IS:
}¬} invlid"
    >YAML

Unfortunately, I have not been able to create a minimal reproducible example from scratch — the crash only happens with this particular file in my project.

When I add this file to the ignore section in .yamllint, the error disappears and yamllint runs normally again.

Traceback (most recent call last):
  File "/home/user/.local/bin/yamllint", line 8, in <module>
    sys.exit(run())
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/cli.py", line 227, in run
    prob_level = show_problems(problems, file, args_format=args.format,
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/cli.py", line 102, in show_problems
    for problem in problems:
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/linter.py", line 201, in _run
    for problem in get_cosmetic_problems(buffer, conf, filepath):
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/linter.py", line 136, in get_cosmetic_problems
    for problem in rule.check(rule_conf,
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/rules/indentation.py", line 582, in check
    yield from _check(conf, token, prev, next, nextnext, context)
  File "/home/user/.local/lib/python3.11/site-packages/yamllint/rules/indentation.py", line 343, in _check
    if expected < 0:
TypeError: '<' not supported between instances of 'NoneType' and 'int'

Expected behavior:
Invalid YAML should result in a parsing error message, not a crash.

Workaround:
Adding the file to .yamllint’s ignore list resolves the problem.

Environment:

yamllint: 1.37.1

Python: 3.11

OS: Ubuntu 24.04.3 LTS

Maybe just make sure expected is never NoneType?

Greetings from austria

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions