Skip to content

Replace PyYAML #955

@chirizxc

Description

@chirizxc

Description

PyYAML parses yaml so that msgspec can validate the data, but it doesn't fully pass yaml-test-suite tests and fails even on small examples like this:

from msgspec.yaml import decode

print(
    decode(
        "! 15",
        type=str,
        strict=True,
    ),  # msgspec.ValidationError: Expected `str`, got `int`
)

From YAML v1.2 spec:

Example 2.27 Invoice:

from msgspec.yaml import decode

yaml = """\
--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843
date   : 2001-01-23
bill-to: &id001
  given  : Chris
  family : Dumars
  address:
    lines: |
      458 Walkman Dr.
      Suite #292
    city    : Royal Oak
    state   : MI
    postal  : 48046
ship-to: *id001
product:
- sku         : BL394D
  quantity    : 4
  description : Basketball
  price       : 450.00
- sku         : BL4438H
  quantity    : 1
  description : Super Hoop
  price       : 2392.00
tax  : 251.42
total: 4443.52
comments:
  Late afternoon is best.
  Backup contact is Nancy
  Billsmer @ 338-4338."""

print(decode(yaml))
# msgspec.DecodeError: could not determine a constructor for the tag 'tag:clarkevans.com,2002:invoice'
#  in "<unicode string>", line 1, column 5

Also:

from msgspec.yaml import decode

# Example 2.23 Various Explicit Tags
yaml = """\
---
not-date: !!str 2002-04-28

picture: !!binary |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=

application specific tag: !something |
 The semantics of the tag
 above may be different for
 different documents."""

print(decode(yaml))
# msgspec.DecodeError: could not determine a constructor for the tag '!something'
#   in "<unicode string>", line 10, column 27

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