CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyyaml

YAML parser and emitter for Python with complete YAML 1.1 support, Unicode handling, and optional LibYAML bindings for high performance

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

loading-parsing.mddocs/

Loading and Parsing

Comprehensive YAML loading capabilities with multiple security levels and processing stages. Includes high-level loading functions and low-level parsing operations for advanced use cases.

Capabilities

High-Level Loading

Load YAML documents into Python objects using different loader classes with varying security levels and feature sets.

def load(stream, Loader):
    """
    Parse the first YAML document in a stream and produce the corresponding Python object.
    
    Args:
        stream (str | bytes | IO): YAML content as string, bytes, or file-like object
        Loader (type): Loader class to use (SafeLoader, FullLoader, Loader, UnsafeLoader)
        
    Returns:
        Any: Python object representing the YAML document
        
    Raises:
        YAMLError: If the document cannot be parsed
        MarkedYAMLError: If there's a syntax error with position information
    """

def load_all(stream, Loader):
    """
    Parse all YAML documents in a stream and produce corresponding Python objects.
    
    Args:
        stream (str | bytes | IO): YAML content containing multiple documents
        Loader (type): Loader class to use
        
    Yields:
        Any: Python objects representing each YAML document
        
    Raises:
        YAMLError: If any document cannot be parsed
    """

def full_load(stream):
    """
    Parse the first YAML document in a stream and produce the corresponding Python object.
    
    Resolve most tags but with security restrictions. Recommended for trusted input.
    
    Args:
        stream (str | bytes | IO): YAML content
        
    Returns:
        Any: Python object representing the YAML document
    """

def full_load_all(stream):
    """
    Parse all YAML documents in a stream and produce corresponding Python objects.
    
    Resolve most tags but with security restrictions.
    
    Args:
        stream (str | bytes | IO): YAML content containing multiple documents
        
    Yields:
        Any: Python objects representing each YAML document
    """

def unsafe_load(stream):
    """
    Parse the first YAML document in a stream and produce the corresponding Python object.
    
    Resolve all tags, even those known to be unsafe on untrusted input.
    WARNING: This can execute arbitrary Python code.
    
    Args:
        stream (str | bytes | IO): YAML content
        
    Returns:
        Any: Python object representing the YAML document
    """

def unsafe_load_all(stream):
    """
    Parse all YAML documents in a stream and produce corresponding Python objects.
    
    Resolve all tags, even those known to be unsafe on untrusted input.
    WARNING: This can execute arbitrary Python code.
    
    Args:
        stream (str | bytes | IO): YAML content containing multiple documents
        
    Yields:
        Any: Python objects representing each YAML document
    """

Usage Examples

import yaml
from yaml import SafeLoader, FullLoader, Loader

yaml_content = """
person:
  name: John Doe
  age: 30
  birth_date: 1993-05-15
  skills:
    - Python
    - YAML
  metadata:
    created: 2023-01-01T10:00:00Z
"""

# Safe loading (basic types only)
data_safe = yaml.load(yaml_content, SafeLoader)
print(type(data_safe['person']['birth_date']))  # str

# Full loading (more types, but restricted)
data_full = yaml.full_load(yaml_content)
print(type(data_full['person']['birth_date']))  # datetime.date

# Explicit loader specification
data_explicit = yaml.load(yaml_content, Loader=FullLoader)

# Loading multiple documents
multi_doc = """
---
doc: 1
name: First Document
---
doc: 2
name: Second Document
"""

for doc in yaml.load_all(multi_doc, SafeLoader):
    print(f"Document {doc['doc']}: {doc['name']}")

Low-Level Parsing

Access lower-level parsing stages for advanced processing and custom workflows.

def scan(stream, Loader=Loader):
    """
    Scan a YAML stream and produce scanning tokens.
    
    Args:
        stream (str | bytes | IO): YAML content
        Loader (type, optional): Loader class to use for scanning
        
    Yields:
        Token: Scanning tokens (StreamStartToken, ScalarToken, etc.)
    """

def parse(stream, Loader=Loader):
    """
    Parse a YAML stream and produce parsing events.
    
    Args:
        stream (str | bytes | IO): YAML content
        Loader (type, optional): Loader class to use for parsing
        
    Yields:
        Event: Parsing events (StreamStartEvent, ScalarEvent, etc.)
    """

def compose(stream, Loader=Loader):
    """
    Parse the first YAML document in a stream and produce the corresponding representation tree.
    
    Args:
        stream (str | bytes | IO): YAML content
        Loader (type, optional): Loader class to use
        
    Returns:
        Node | None: Root node of the representation tree, or None if no document
    """

def compose_all(stream, Loader=Loader):
    """
    Parse all YAML documents in a stream and produce corresponding representation trees.
    
    Args:
        stream (str | bytes | IO): YAML content containing multiple documents
        Loader (type, optional): Loader class to use
        
    Yields:
        Node: Root nodes of representation trees
    """

Usage Examples

import yaml
from yaml import SafeLoader

yaml_content = "name: John\nage: 30"

# Scanning - produces tokens
tokens = list(yaml.scan(yaml_content, SafeLoader))
for token in tokens:
    print(f"{type(token).__name__}: {token}")

# Parsing - produces events  
events = list(yaml.parse(yaml_content, SafeLoader))
for event in events:
    print(f"{type(event).__name__}: {event}")

# Composing - produces representation tree
node = yaml.compose(yaml_content, SafeLoader)
print(f"Root node: {type(node).__name__}")
print(f"Tag: {node.tag}")
print(f"Value type: {type(node.value)}")

# Walking the tree
if hasattr(node, 'value'):
    for key_node, value_node in node.value:
        print(f"  {key_node.value}: {value_node.value}")

Security Levels

SafeLoader (Safest)

  • Only basic YAML types (string, number, boolean, list, dict, null)
  • No Python object instantiation
  • Safe for completely untrusted input

FullLoader (Recommended)

  • Most YAML types including dates, timestamps
  • Some Python types but with restrictions
  • Prevents known dangerous operations
  • Good balance of features and security

Loader/UnsafeLoader (Dangerous)

  • All YAML types including arbitrary Python objects
  • Can execute Python code during loading
  • Only for completely trusted input
  • Required for full PyYAML feature set

Supported Input Types

All loading functions accept:

  • str: YAML content as Unicode string
  • bytes: YAML content as UTF-8/UTF-16/UTF-32 encoded bytes
  • IO[str]: Text file-like objects (open files, StringIO, etc.)
  • IO[bytes]: Binary file-like objects with YAML content

File Loading Examples

import yaml

# From file path (manual)
with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f)

# From binary file
with open('config.yaml', 'rb') as f:
    config = yaml.safe_load(f)  # Auto-detects encoding

# From StringIO
from io import StringIO
yaml_io = StringIO("key: value")
data = yaml.safe_load(yaml_io)

# From URL or network source
import urllib.request
with urllib.request.urlopen('https://example.com/config.yaml') as response:
    config = yaml.safe_load(response)

Error Handling

Loading operations can raise various exceptions:

try:
    data = yaml.safe_load(yaml_content)
except yaml.YAMLError as e:
    print(f"YAML Error: {e}")
except yaml.MarkedYAMLError as e:
    print(f"Syntax Error at line {e.problem_mark.line + 1}: {e.problem}")

Common error scenarios:

  • Malformed YAML syntax
  • Invalid Unicode sequences
  • Circular references in data
  • Memory limits exceeded for large documents
  • Security restrictions violated (with restricted loaders)

Node Classes

Representation tree nodes used by the compose functions and low-level processing:

class Node:
    """
    Base class for all YAML representation nodes.
    
    Attributes:
        tag (str): YAML tag for the node
        value: Node-specific value (varies by subclass)
        start_mark (Mark): Position where node starts
        end_mark (Mark): Position where node ends
    """

class ScalarNode(Node):
    """
    Represents scalar values (strings, numbers, booleans, null).
    
    Attributes:
        tag (str): YAML tag
        value (str): Raw scalar value as string
        start_mark (Mark): Start position
        end_mark (Mark): End position
        style (str): Scalar style (' ', '|', '>', '"', "'", etc.)
    """

class SequenceNode(CollectionNode):
    """
    Represents YAML sequences (lists/arrays).
    
    Attributes:
        tag (str): YAML tag
        value (list[Node]): List of child nodes
        start_mark (Mark): Start position
        end_mark (Mark): End position
        flow_style (bool): True for flow style [a, b], False for block style
    """

class MappingNode(CollectionNode):
    """
    Represents YAML mappings (dictionaries/objects).
    
    Attributes:
        tag (str): YAML tag
        value (list[tuple[Node, Node]]): List of (key_node, value_node) pairs
        start_mark (Mark): Start position
        end_mark (Mark): End position
        flow_style (bool): True for flow style {a: b}, False for block style
    """

Event Classes

Event objects produced by parsing functions for low-level YAML processing:

class Event:
    """Base class for all YAML events."""

class StreamStartEvent(Event):
    """Start of YAML stream."""

class StreamEndEvent(Event):
    """End of YAML stream."""

class DocumentStartEvent(Event):
    """
    Start of YAML document.
    
    Attributes:
        explicit (bool): True if explicit document start (---)
        version (tuple): YAML version (e.g., (1, 1))
        tags (dict): Tag directive mappings
    """

class DocumentEndEvent(Event):
    """
    End of YAML document.
    
    Attributes:
        explicit (bool): True if explicit document end (...)
    """

class ScalarEvent(NodeEvent):
    """
    Scalar value event.
    
    Attributes:
        anchor (str): Anchor name if present
        tag (str): YAML tag
        implicit (tuple[bool, bool]): Implicit tag resolution flags
        value (str): Scalar value
        style (str): Scalar style
    """

class SequenceStartEvent(CollectionStartEvent):
    """Start of sequence/list."""

class SequenceEndEvent(CollectionEndEvent):
    """End of sequence/list."""

class MappingStartEvent(CollectionStartEvent):
    """Start of mapping/dictionary."""

class MappingEndEvent(CollectionEndEvent):
    """End of mapping/dictionary."""

class AliasEvent(NodeEvent):
    """
    Alias reference event.
    
    Attributes:
        anchor (str): Referenced anchor name
    """

Install with Tessl CLI

npx tessl i tessl/pypi-pyyaml

docs

customization.md

dumping-serialization.md

error-handling.md

index.md

loaders-dumpers.md

loading-parsing.md

safe-operations.md

tile.json