Astropy I/O Typing ( provides type annotations through the module. These type annotations allow users to specify the expected types of variables, function parameters, and return values when working with I/O. By using type annotations, developers can improve code readability, catch potential type-related errors early, and enable better code documentation and tooling support.

For example, the following function uses type annotations to specify that the filename parameter can be any type of path-like object (e.g. a string, byte-string, or pathlib.Path object).

from import fits
from import PathLike

def read_fits_file(filename: PathLike) -> fits.HDUList:

The module also provides type aliases for file-like objects that support reading and writing. The following example uses the ReadableFileLike type alias to specify that the fileobj parameter can be any file-like object that supports reading. Using a TypeVar, the return type of the function is specified to be the same type as the file-like object can read.

from typing import TypeVar
from import ReadableFileLike

R = TypeVar('R')  # type of object returned by

def read_from_file(fileobj: ReadableFileLike[R]) -> R:
     """Reads from a file-like object and returns the result."""

Reference/API# Module#

Type annotations for

These are type annotations for I/O-related functions and classes. Some of the type objects can also be used as runtime-checkable Protocol objects.


ReadableFileLike(*args, **kwargs)

A file-like object that supports reading with a method read.

WriteableFileLike(*args, **kwargs)

A file-like object that supports writing with a method write.

Class Inheritance Diagram#

Inheritance diagram of,