{"id":722,"library":"types-pyyaml","title":"Typing Stubs for PyYAML","description":"This package provides static type annotations (stubs) for the PyYAML library. PyYAML is a full-featured YAML parser and emitter for Python, enabling seamless interaction with YAML data for configurations and structured data. Types-PyYAML allows static type checkers like mypy and pyright to analyze code using PyYAML, improving code quality and catching potential type errors before runtime. This package is part of the typeshed project and aims to provide accurate annotations for PyYAML versions 6.0.*.","status":"active","version":"6.0.12.20250915","language":"python","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","yaml","type-checking"],"install":[{"cmd":"pip install types-PyYAML","lang":"bash","label":"Install types-PyYAML"},{"cmd":"pip install PyYAML types-PyYAML mypy","lang":"bash","label":"Install PyYAML, its stubs, and mypy for type checking"}],"dependencies":[{"reason":"This package provides type stubs for PyYAML; PyYAML itself is required at runtime for any functionality.","package":"PyYAML","optional":false},{"reason":"Requires Python 3.9 or newer.","package":"python","optional":false}],"imports":[{"note":"Types-PyYAML provides type hints for the 'yaml' module imported from the PyYAML library. It does not introduce its own runtime symbols.","symbol":"yaml","correct":"import yaml"},{"note":"Common imports for PyYAML functionality, now with type checking enabled by types-PyYAML.","symbol":"load, dump","correct":"from yaml import load, dump"}],"quickstart":{"code":"import yaml\nfrom typing import Any\n\ndef process_config(config_str: str) -> dict[str, Any]:\n    data: dict[str, Any] = yaml.safe_load(config_str)\n    # Introduce a deliberate type error for demonstration\n    # 'name' is expected to be a string, but we assign a number\n    # mypy should flag this if types-PyYAML is correctly used.\n    if 'name' in data: # type: ignore[reportUntypedBaseMembers]\n        data['name'] = 123  # Intentionally wrong type for demo\n    return data\n\nconfig_data = \"\"\"\nname: Alice\nage: 30\n\"\"\"\n\n# This would normally be executed, but the focus is on static analysis\n# processed_data = process_config(config_data)\n# print(processed_data)\n\n# To run mypy and see the type error:\n# 1. pip install PyYAML types-PyYAML mypy\n# 2. Save this code as `app.py`\n# 3. Run: `mypy app.py`\n# Expected output should include a type error about data['name'] assignment.","lang":"python","description":"This quickstart demonstrates how `types-PyYAML` enables static type checking for `PyYAML` code using `mypy`. After installing `PyYAML`, `types-PyYAML`, and `mypy`, save the code to `app.py` and run `mypy app.py`. Mypy will detect the deliberate type error where an integer is assigned to a key expected to hold a string, thanks to the type information provided by `types-PyYAML`."},"warnings":[{"fix":"Consider pinning your `types-PyYAML` version in your `requirements.txt` (e.g., `types-PyYAML==6.0.12.20250915`) and regularly testing updates. You can also temporarily ignore specific errors using `type: ignore` comments if a stub change is overly strict or controversial, while contributing fixes to typeshed.","message":"Updates to type stubs, even without changes to your runtime code, can introduce new type-checking errors. This is because typeshed aims to improve the accuracy of type annotations, which might expose previously unflagged type inconsistencies in your codebase.","severity":"breaking","affected_versions":"All versions"},{"fix":"If strict type checking is desired, you may need to use `typing.cast` or `type: ignore` to suppress these specific errors. Alternatively, structure your code to use a single `Loader`/`Dumper` definition to avoid type checker confusion, or refactor to reduce direct exposure of the `Loader` type.","message":"When attempting to use the C-accelerated `CLoader` or `CDumper` from PyYAML with a fallback to pure Python `Loader`/`Dumper`, type checkers like mypy can report `Incompatible import` errors. This happens because `CLoader` and `Loader` are distinct types, even though they often serve a similar purpose.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of YAML 1.1's implicit typing rules when writing or consuming YAML files with PyYAML. For stricter YAML 1.2 compliance, consider alternative libraries like `ruamel.yaml` if the `types-PyYAML` stubs' accuracy for PyYAML's behavior is undesirable for your use case.","message":"PyYAML (and thus its stubs) is based on the older YAML 1.1 specification. This can lead to unexpected implicit type conversions (e.g., 'yes', 'no' become booleans; '010' becomes an octal integer; `59:59` interpreted as seconds) compared to the stricter YAML 1.2 specification common in other parsers. The stubs accurately reflect these 1.1 behaviors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"It is recommended to explicitly install both `PyYAML` and `types-PyYAML` and to regularly update both, while ensuring their compatibility. Typeshed recommends either using the same version bounds for stubs as for the package or pinning the stubs to a known good version.","message":"The versioning of `types-PyYAML` is independent of `PyYAML` itself, though the stubs target specific `PyYAML` versions (e.g., `types-PyYAML 6.0.x.YYYYMMDD` targets `PyYAML==6.0.*`). This means updating one does not automatically update the other, and an older stub version might not fully cover a newer PyYAML, or vice versa.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T18:16:52.717Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"pip install types-pyyaml","cause":"The 'types-pyyaml' package, which provides static type stubs for the PyYAML library, is not installed or not accessible by MyPy.","error":"Cannot find implementation or library stub for module named 'yaml'"},{"fix":"pip install types-pyyaml","cause":"The 'types-pyyaml' package, which provides static type stubs for the PyYAML library, is not installed or not accessible by Pyright.","error":"reportMissingTypeStubs: Stub file not found for 'yaml'"},{"fix":"pip install PyYAML","cause":"The 'PyYAML' runtime library is not installed; 'types-pyyaml' provides only static type annotations (stubs) and not the actual 'yaml' module for execution.","error":"ModuleNotFoundError: No module named 'yaml'"},{"fix":"Explicitly import and specify the desired Loader, for example: `from yaml import SafeLoader\ndata = yaml.load(yaml_string, Loader=SafeLoader)`","cause":"MyPy is reporting a type incompatibility because 'yaml.load' is being called with a 'Loader' argument whose type is inferred as 'Any', often due to not explicitly specifying a safe loader or an untyped variable.","error":"Argument \"Loader\" to \"load\" of \"yaml\" has an incompatible type \"Any\"; expected \"Union[Type[Loader], Type[SafeLoader], Type[FullLoader], Type[BaseLoader]]\""}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"6.0.12.20260510","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":1,"disk_size":"83.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":1,"disk_size":"79.0M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":3.8,"import_time_s":0.05,"mem_mb":1,"disk_size":"83M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1,"disk_size":"79M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.23,"mem_mb":1.1,"disk_size":"89.2M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"19.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.27,"mem_mb":1.1,"disk_size":"84.1M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":3.7,"import_time_s":0.2,"mem_mb":1.1,"disk_size":"89M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.2,"mem_mb":1.1,"disk_size":"84M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":0.8,"disk_size":"81.1M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.6M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.16,"mem_mb":0.8,"disk_size":"76.0M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":3.6,"import_time_s":0.16,"mem_mb":0.8,"disk_size":"81M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":0.8,"disk_size":"76M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.13,"mem_mb":1,"disk_size":"81.0M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.3M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.15,"mem_mb":1,"disk_size":"75.8M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":3.6,"import_time_s":0.15,"mem_mb":0.8,"disk_size":"81M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.16,"mem_mb":0.8,"disk_size":"76M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":1.1,"disk_size":"75.5M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":1.1,"disk_size":"74.8M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":4.1,"import_time_s":0.06,"mem_mb":1.1,"disk_size":"75M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":1.1,"disk_size":"74M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}