typing

Various type annotation aids.

Type Hints

PathLike

Type hint for objects that represent filesystem paths.

PathType

Invariant TypeVar constrained to str, pathlib.Path and os.PathLike.

AnyNumber

Type hint for numbers.

WrapperDescriptorType

The type of methods of some built-in data types and base classes.

MethodWrapperType

The type of bound methods of some built-in data types and base classes.

MethodDescriptorType

The type of methods of some built-in data types.

ClassMethodDescriptorType

The type of unbound class methods of some built-in data types.

PathLike

Type hint for objects that represent filesystem paths.

Alias of Union[str, Path, PathLike]

PathType = TypeVar(PathType, str, Path, PathLike)

Type:    TypeVar

Invariant TypeVar constrained to str, pathlib.Path and os.PathLike.

Type variable for objects that represent filesystem paths.

New in version 2.2.0.

AnyNumber

Type hint for numbers.

Changed in version 0.4.6: Moved from domdf_python_tools.pagesizes

Alias of Union[float, int, Decimal]

WrapperDescriptorType

The type of methods of some built-in data types and base classes, such as object.__init__() or object.__lt__().

New in version 0.8.0.

MethodWrapperType

The type of bound methods of some built-in data types and base classes. For example, it is the type of object().__str__.

New in version 0.8.0.

MethodDescriptorType

The type of methods of some built-in data types, such as str.join().

New in version 0.8.0.

ClassMethodDescriptorType

The type of unbound class methods of some built-in data types, such as dict.__dict__['fromkeys'].

New in version 0.8.0.

Protocols

JsonLibrary

typing.Protocol for libraries that implement the same API as json.

HasHead

typing.Protocol for classes that have a head method.

String

Protocol for classes that implement __str__.

FrameOrSeries

Invariant TypeVar constrained to pandas.Series and pandas.DataFrame.

SupportsIndex

typing.Protocol for classes that support __index__.

SupportsLessThan

typing.Protocol for classes that support __lt__.

SupportsLessEqual

typing.Protocol for classes that support __le__.

SupportsGreaterThan

typing.Protocol for classes that support __gt__.

SupportsGreaterEqual

typing.Protocol for classes that support __ge__.

protocol JsonLibrary[source]

Bases: Protocol

typing.Protocol for libraries that implement the same API as json.

Useful for annotating functions which take a JSON serialisation-deserialisation library as an argument.

Classes that implement this protocol must have the following methods / attributes:

static dumps(obj, *, skipkeys=..., ensure_ascii=..., check_circular=..., allow_nan=..., cls=..., indent=..., separators=..., default=..., sort_keys=..., **kwds)[source]

Serialize obj to a JSON formatted str.

Parameters
Return type

str

static loads(s, *, cls=..., object_hook=..., parse_float=..., parse_int=..., parse_constant=..., object_pairs_hook=..., **kwds)[source]

Deserialize s to a Python object.

Parameters
Return type

Any

protocol HasHead[source]

Bases: Protocol

typing.Protocol for classes that have a head method.

This includes pandas.DataFrame and pandas.Series.

New in version 0.8.0.

This protocol is runtime checkable.

Classes that implement this protocol must have the following methods / attributes:

__non_callable_proto_members__ = {}

Type:    set

head(n=5)[source]

Return the first n rows.

Parameters

n (int) – Number of rows to select. Default 5.

Return type

HasHead

Returns

The first n rows of the caller object.

to_string(*args, **kwargs)[source]

Render the object to a console-friendly tabular output.

Return type

Optional[str]

protocol String[source]

Bases: Protocol

Protocol for classes that implement __str__.

Changed in version 0.8.0: Moved from domdf_python_tools.stringlist.

This protocol is runtime checkable.

Classes that implement this protocol must have the following methods / attributes:

__non_callable_proto_members__ = {}

Type:    set

__str__()[source]

Return str(self).

Return type

str

protocol FrameOrSeries

New in version 1.0.0.

Classes that implement this protocol must have the following methods / attributes:

protocol SupportsIndex[source]

Bases: Protocol

typing.Protocol for classes that support __index__.

New in version 2.0.0.

Classes that implement this protocol must have the following methods / attributes:

__index__()[source]
Return type

int

protocol SupportsLessThan[source]

Bases: Protocol

typing.Protocol for classes that support __lt__.

New in version 3.0.0.

Classes that implement this protocol must have the following methods / attributes:

__lt__(_SupportsLessThan__other)[source]

Return self < value.

Return type

bool

protocol SupportsLessEqual[source]

Bases: Protocol

typing.Protocol for classes that support __le__.

New in version 3.0.0.

Classes that implement this protocol must have the following methods / attributes:

__le__(_SupportsLessEqual__other)[source]

Return self <= value.

Return type

bool

protocol SupportsGreaterThan[source]

Bases: Protocol

typing.Protocol for classes that support __gt__.

New in version 3.0.0.

Classes that implement this protocol must have the following methods / attributes:

__gt__(_SupportsGreaterThan__other)[source]

Return self > value.

Return type

bool

protocol SupportsGreaterEqual[source]

Bases: Protocol

typing.Protocol for classes that support __ge__.

New in version 3.0.0.

Classes that implement this protocol must have the following methods / attributes:

__ge__(_SupportsGreaterEqual__other)[source]

Return self >= value.

Return type

bool

Utility Functions

check_membership(obj, type_)[source]

Check if the type of obj is one of the types in a typing.Union, typing.Sequence etc.

Parameters
Return type

bool