CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-types-pyyaml

Type stubs for PyYAML, a full-featured YAML framework for Python

Overview
Eval results
Files

loading.mddocs/

Loading Functions

Functions for loading YAML documents with different safety levels and processing modes. These functions parse YAML streams and convert them to Python objects.

Capabilities

Safe Loading

Recommended functions for loading YAML from untrusted sources. These functions only construct standard YAML tags and are safe from arbitrary code execution.

def safe_load(stream: bytes | IO[bytes] | str | IO[str]) -> Any:
    """
    Parse a YAML document using SafeLoader.
    
    Parameters:
    - stream: YAML input as string, bytes, or file-like object
    
    Returns:
    - Python object representing the YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

def safe_load_all(stream: bytes | IO[bytes] | str | IO[str]) -> Iterator[Any]:
    """
    Parse all YAML documents from a stream using SafeLoader.
    
    Parameters:
    - stream: YAML input containing multiple documents separated by ---
    
    Yields:
    - Python objects representing each YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

Full Loading

Enhanced loading with additional Python object support, safer than unsafe loading but allows more constructs than safe loading.

def full_load(stream: bytes | IO[bytes] | str | IO[str]) -> Any:
    """
    Parse a YAML document using FullLoader.
    
    Supports Python objects like tuples, sets, and other built-in types
    while remaining safer than UnsafeLoader.
    
    Parameters:
    - stream: YAML input as string, bytes, or file-like object
    
    Returns:
    - Python object representing the YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

def full_load_all(stream: bytes | IO[bytes] | str | IO[str]) -> Iterator[Any]:
    """
    Parse all YAML documents from a stream using FullLoader.
    
    Parameters:
    - stream: YAML input containing multiple documents
    
    Yields:
    - Python objects representing each YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

Generic Loading

Configurable loading functions that accept custom Loader classes for advanced use cases.

def load(stream: bytes | IO[bytes] | str | IO[str], Loader=None) -> Any:
    """
    Parse a YAML document using specified Loader.
    
    Parameters:
    - stream: YAML input as string, bytes, or file-like object
    - Loader: Loader class to use (defaults to FullLoader)
    
    Returns:
    - Python object representing the YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

def load_all(stream: bytes | IO[bytes] | str | IO[str], Loader=None) -> Iterator[Any]:
    """
    Parse all YAML documents from a stream using specified Loader.
    
    Parameters:
    - stream: YAML input containing multiple documents
    - Loader: Loader class to use (defaults to FullLoader)
    
    Yields:
    - Python objects representing each YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

Unsafe Loading

WARNING: These functions can execute arbitrary Python code and should never be used with untrusted input.

def unsafe_load(stream: bytes | IO[bytes] | str | IO[str]) -> Any:
    """
    Parse a YAML document using UnsafeLoader.
    
    DANGER: Can execute arbitrary Python code. Only use with trusted input.
    
    Parameters:
    - stream: YAML input as string, bytes, or file-like object
    
    Returns:
    - Python object representing the YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

def unsafe_load_all(stream: bytes | IO[bytes] | str | IO[str]) -> Iterator[Any]:
    """
    Parse all YAML documents from a stream using UnsafeLoader.
    
    DANGER: Can execute arbitrary Python code. Only use with trusted input.
    
    Parameters:
    - stream: YAML input containing multiple documents
    
    Yields:
    - Python objects representing each YAML document
    
    Raises:
    - YAMLError: If YAML parsing fails
    """

Usage Examples

Basic Safe Loading

import yaml

# Load from string
yaml_str = """
name: John Doe
age: 30
skills:
  - Python
  - YAML
  - Docker
"""

data = yaml.safe_load(yaml_str)
print(data['name'])  # "John Doe"
print(data['skills'])  # ["Python", "YAML", "Docker"]

# Load from file
with open('config.yaml', 'r') as file:
    config = yaml.safe_load(file)

Multiple Documents

import yaml

multi_doc = """
name: Document 1
type: config
---
name: Document 2  
type: data
---
name: Document 3
type: settings
"""

documents = list(yaml.safe_load_all(multi_doc))
print(len(documents))  # 3
print(documents[0]['name'])  # "Document 1"

Error Handling

import yaml

try:
    data = yaml.safe_load('invalid: yaml: content: [')
except yaml.YAMLError as e:
    print(f"YAML parsing error: {e}")
    if hasattr(e, 'problem_mark'):
        mark = e.problem_mark
        print(f"Error at line {mark.line + 1}, column {mark.column + 1}")

Custom Loader

import yaml

# Using a specific loader
data = yaml.load(yaml_content, Loader=yaml.SafeLoader)

# Equivalent to safe_load
data = yaml.safe_load(yaml_content)

Install with Tessl CLI

npx tessl i tessl/pypi-types-pyyaml

docs

advanced-components.md

c-extensions.md

custom-objects.md

dumping.md

errors.md

index.md

loaders-dumpers.md

loading.md

low-level.md

registration.md

tile.json