Skip to content

Help creating yamllint config to point out PyYAML-invalid yaml #801

@mathomp4

Description

@mathomp4

Sorry for the odd subject title, but I'm still a bit of a newbie to YAML and yamllint so I don't know the right "words" for what's happening.

Namely, we have a file that yamllint is happy with, but PyYAML is not. So, my guess is, I need to fix up my .yamllint.yml to detect this, but I'm not sure what the right rule would be.

To wit, in some code I work on, we have this yaml file:

---
# anchors for recurring cases
monthly: &monthly
  period: P1M
  offset: PT0H  # + 2000-01-01T00:00:00

daily: &daily
  period: PT24H
  offset: PT0H

hourly: &hourly
  period: P1H
  offset: PT30M  # 30 minute offset

monthly_average: &monthly_average
  action: average
  <<: monthly

monthly_variance: &monthly_variance
  action: variance
  <<: monthly

monthly_covariance: &monthly_covariance
  action: variance
  <<: monthly

stats:
  - name: A/TS
    action: average
    itemtype: field
    <<: *daily

Now, yamllint (by default) thinks this is hunky-dory:

❯ yamllint -s stat.yaml
❯ echo $?
0

But if you write a small Python script that does a yaml.safe_load("stat.yaml"), you get:

❯ python3 test.py
Error in YAML syntax: while constructing a mapping
  in "stat.yaml", line 15, column 18
expected a mapping or list of mappings for merging, but found scalar
  in "stat.yaml", line 17, column 7

We are pretty sure the issue is that we have missing * like in:

monthly_average: &monthly_average
  action: average
  <<: monthly

which should be:

monthly_average: &monthly_average
  action: average
  <<: *monthly

If you fix all the missing *, PyYAML is happy.

Is there a rule we can add to our yamllint config to warn us about this? Or is this sort of thing sort of just...undetectable by a linter?

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