Typing stubs for atomicwrites
types-atomicwrites is a PEP 561 type stub package for the atomicwrites library, currently at version 1.4.5.1. It provides type annotations for use by static type checkers like MyPy and PyCharm. While it is part of the broader typeshed project, this specific stub package is explicitly noted as unmaintained and will not be updated. Furthermore, the underlying atomicwrites library itself has been marked as deprecated by its original maintainer, who recommends using Python 3's built-in os.replace or os.rename functions for most atomic file writing needs.
Common errors
-
ModuleNotFoundError: No module named 'atomicwrites'
cause `types-atomicwrites` only provides type hints and does not include the runtime code for the `atomicwrites` library itself. The actual `atomicwrites` package is missing.fixInstall the runtime library: `pip install atomicwrites`. -
Type checker errors (e.g., error: Missing type parameters for generic type "AtomicWriter") or unexpected runtime behavior when using `atomicwrites` features.
cause The `types-atomicwrites` stub package is unmaintained and may be out of sync with newer versions of the `atomicwrites` library, leading to inaccurate type hints or incompatible signatures.fixDowngrade `atomicwrites` to a version compatible with `types-atomicwrites==1.4.5.1` (which corresponds to `atomicwrites==1.4.*`), or consider migrating to `os.replace`/`os.rename` or an alternative atomic writing library. -
CI/CD pipeline failures related to `atomicwrites` dependency resolution (e.g., specific versions not found).
cause The `atomicwrites` package experienced a temporary deletion of old versions from PyPI due to maintainer issues, which, despite being restored, indicates potential instability.fixEnsure `atomicwrites` is pinned to a specific, known-good version (e.g., `atomicwrites==1.4.0`) in your `requirements.txt` or `pyproject.toml` to avoid unexpected dependency resolution failures. Given its unmaintained status, consider migrating away from `atomicwrites`.
Warnings
- breaking The `types-atomicwrites` package is officially unmaintained and will not receive further updates. This means future versions of `atomicwrites` may not have corresponding, accurate type stubs, potentially leading to incorrect type checking results or runtime errors if type-checked code relies on outdated signatures.
- deprecated The upstream `atomicwrites` library itself is considered deprecated by its original maintainer, who advises using `os.replace` or `os.rename` for atomic file operations in Python 3. This deprecation stems from issues including PyPI's 2FA requirements and the maintainer's decision not to continue maintenance.
- gotcha `atomicwrites` (and thus code relying on its stubs) may not be truly atomic in all scenarios, especially on Windows or when files are moved across different filesystems. This can lead to data corruption or unexpected states in case of system failure.
Install
-
pip install types-atomicwrites -
pip install atomicwrites
Imports
- atomic_write
from types_atomicwrites import atomic_write
from atomicwrites import atomic_write
- AtomicWriter
from atomicwrites import AtomicWriter
Quickstart
from atomicwrites import atomic_write
def save_data_atomically(filename: str, content: str):
try:
with atomic_write(filename, overwrite=True) as f:
f.write(content)
print(f"Successfully wrote to {filename}")
except Exception as e:
print(f"Error writing to {filename}: {e}")
# Example usage:
save_data_atomically("my_config.txt", "config_key=value\nother_setting=123")