or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-components.mdc-extensions.mdcustom-objects.mddumping.mderrors.mdindex.mdloaders-dumpers.mdloading.mdlow-level.mdregistration.md
tile.json

tessl/pypi-types-pyyaml

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

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/types-pyyaml@5.4.x

To install, run

npx @tessl/cli install tessl/pypi-types-pyyaml@5.4.0

index.mddocs/

PyYAML Type Stubs

Type stubs for PyYAML, a full-featured YAML framework for Python. This package provides comprehensive type annotations enabling static type checking for YAML parsing and serialization operations, covering the complete YAML processing pipeline from reading to dumping with support for both safe and unsafe operations.

Package Information

  • Package Name: types-PyYAML
  • Language: Python
  • Installation: pip install types-PyYAML

Core Imports

import yaml

For specific functionality:

from yaml import load, dump, safe_load, safe_dump
from yaml import SafeLoader, SafeDumper, FullLoader
from yaml import YAMLError, YAMLObject

Basic Usage

import yaml

# Safe loading (recommended for untrusted input)
data = yaml.safe_load('hello: world\nlist:\n  - item1\n  - item2')
print(data)  # {'hello': 'world', 'list': ['item1', 'item2']}

# Safe dumping
yaml_string = yaml.safe_dump({'name': 'John', 'age': 30, 'items': [1, 2, 3]})
print(yaml_string)

# Full loading (more features, safer than unsafe)
with open('config.yaml', 'r') as file:
    config = yaml.full_load(file)

# Dumping to file
with open('output.yaml', 'w') as file:
    yaml.safe_dump(data, file, default_flow_style=False)

Architecture

PyYAML provides a complete YAML processing pipeline with multiple abstraction levels:

  • High-level API: Convenience functions (load, dump, safe_load, safe_dump)
  • Processing Pipeline: Reader → Scanner → Parser → Composer → Constructor for loading
  • Output Pipeline: Representer → Serializer → Emitter for dumping
  • Customization Layer: Custom constructors, representers, resolvers for advanced use cases
  • Performance Layer: C extensions (CLoader, CDumper) for improved performance

Capabilities

High-Level Loading Functions

Core functions for loading YAML documents with different safety levels and processing modes.

def safe_load(stream) -> Any: ...
def safe_load_all(stream) -> Iterator[Any]: ...
def full_load(stream) -> Any: ...
def full_load_all(stream) -> Iterator[Any]: ...
def load(stream, Loader=None) -> Any: ...
def load_all(stream, Loader=None) -> Iterator[Any]: ...
def unsafe_load(stream) -> Any: ...
def unsafe_load_all(stream) -> Iterator[Any]: ...

Loading Functions

High-Level Dumping Functions

Core functions for serializing Python objects to YAML with different safety levels and formatting options.

def safe_dump(data, stream=None, **kwargs) -> str | None: ...
def safe_dump_all(documents, stream=None, **kwargs) -> str | None: ...
def dump(data, stream=None, Dumper=None, **kwargs) -> str | None: ...
def dump_all(documents, stream=None, Dumper=None, **kwargs) -> str | None: ...

Dumping Functions

Loaders and Dumpers

Configurable loader and dumper classes that combine different processing components for customized YAML processing.

class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver): ...
class SafeLoader(Reader, Scanner, Parser, Composer, SafeConstructor, Resolver): ...
class FullLoader(Reader, Scanner, Parser, Composer, FullConstructor, Resolver): ...
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver): ...
class UnsafeLoader(Reader, Scanner, Parser, Composer, Constructor, Resolver): ...
class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver): ...
class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver): ...
class Dumper(Emitter, Serializer, Representer, Resolver): ...

Loaders and Dumpers

Error Handling

Exception classes for handling various YAML processing errors with position information and detailed error messages.

class YAMLError(Exception): ...
class MarkedYAMLError(YAMLError):
    context: str | None
    context_mark: Mark | None
    problem: str | None
    problem_mark: Mark | None
    note: str | None

class Mark:
    name: str
    index: int
    line: int
    column: int
    def get_snippet(self, indent=4, max_length=75) -> str: ...

Error Handling

Custom YAML Objects

Base classes and utilities for creating custom YAML-serializable Python objects with automatic tag registration.

class YAMLObject(metaclass=YAMLObjectMetaclass):
    yaml_loader: Any
    yaml_dumper: Any
    yaml_tag: Any
    yaml_flow_style: Any
    @classmethod
    def from_yaml(cls, loader, node): ...
    @classmethod
    def to_yaml(cls, dumper, data): ...

Custom Objects

Registration Functions

Functions for registering custom constructors, representers, and resolvers to extend YAML processing capabilities.

def add_constructor(tag: str, constructor: Callable, Loader=None) -> None: ...
def add_multi_constructor(tag_prefix: str, multi_constructor: Callable, Loader=None) -> None: ...
def add_representer(data_type: type, representer: Callable, Dumper=None) -> None: ...
def add_multi_representer(data_type: type, multi_representer: Callable, Dumper=None) -> None: ...
def add_implicit_resolver(tag: str, regexp: Pattern[str], first=None, Loader=None, Dumper=None) -> None: ...
def add_path_resolver(tag: str, path: Iterable[Any], kind=None, Loader=None, Dumper=None) -> None: ...

Registration

Low-Level Processing

Lower-level YAML processing functions for advanced use cases requiring fine-grained control over the processing pipeline.

def scan(stream, Loader=None): ...
def parse(stream, Loader=None): ...
def compose(stream, Loader=None): ...
def compose_all(stream, Loader=None): ...
def serialize(node, stream=None, Dumper=None, **kwargs): ...
def serialize_all(nodes, stream=None, Dumper=None, **kwargs): ...
def emit(events, stream=None, Dumper=None, **kwargs): ...

Low-Level Processing

Advanced Components

Internal processing components for custom loader and dumper construction, including constructors, representers, resolvers, and processing pipeline components.

class BaseConstructor: ...
class SafeConstructor(BaseConstructor): ...
class FullConstructor(SafeConstructor): ...
class BaseRepresenter: ...
class SafeRepresenter(BaseRepresenter): ...
class Representer(SafeRepresenter): ...
class BaseResolver: ...
class Resolver(BaseResolver): ...

Advanced Components

C Extensions

High-performance C-based implementations of loaders, dumpers, and processing components for improved performance.

class CBaseLoader(CParser, BaseConstructor, BaseResolver): ...
class CLoader(CParser, SafeConstructor, Resolver): ...
class CSafeLoader(CParser, SafeConstructor, Resolver): ...
class CFullLoader(CParser, FullConstructor, Resolver): ...
class CUnsafeLoader(CParser, UnsafeConstructor, Resolver): ...
class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver): ...
class CDumper(CEmitter, SafeRepresenter, Resolver): ...
class CSafeDumper(CEmitter, SafeRepresenter, Resolver): ...

# Type alias
CSafeDumper = CDumper

C Extensions

Common Types

# Module constants
__with_libyaml__: Any  # Indicates whether C extensions are available
__version__: str  # PyYAML version string

# Type aliases used throughout the API
_Yaml = Any  # YAML data type
_Str = str | Union[Text, str]  # String type for Python 2/3 compatibility

# Stream types
StreamType = Union[str, bytes, IO[str], IO[bytes], Text, IO[Text]]

# Constructor and representer type variables  
_Constructor = TypeVar("_Constructor", bound=BaseConstructor)
_Representer = TypeVar("_Representer", bound=BaseRepresenter)