Types Modules¶
Contents¶
pyavia.types.coax_type |
Try converting x into a series of types, returning first result which passes test: next_type(x) - x == 0. |
pyavia.types.force_type |
Try converting x into a series of types, with no check on the conversion validity. |
pyavia.types.dataclass_fromlist |
Initialise a dataclass of type dc_type using a list of values init_vals ordered to match the class fields (i.e. |
pyavia.types.dataclass_names |
When passed a type or specific instance of a dataclass, returns an ordered list containing the names of the individual fields. |
pyavia.types.make_sentinel |
This factory function is taken directly from ``boltons.typeutils`` and has only cosmetic changes. |
Members¶
Functions for changing data between different / unusual types.
-
class
pyavia.types.
TypeVar
(name, *constraints, bound=None, covariant=False, contravariant=False)¶ Bases:
typing._Final
,typing._Immutable
Type variable.
Usage:
T = TypeVar('T') # Can be anything A = TypeVar('A', str, bytes) # Must be str or bytes
Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function definitions. See class Generic for more information on generic types. Generic functions work as follows:
- def repeat(x: T, n: int) -> List[T]:
- ‘’’Return a list containing n references to x.’’’ return [x]*n
- def longest(x: A, y: A) -> A:
- ‘’’Return the longest of two strings.’’’ return x if len(x) >= len(y) else y
The latter example’s signature is essentially the overloading of (str, str) -> str and (bytes, bytes) -> bytes. Also note that if the arguments are instances of some subclass of str, the return type is still plain str.
At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Type variables defined with covariant=True or contravariant=True can be used to declare covariant or contravariant generic types. See PEP 484 for more details. By default generic types are invariant in all type variables.
Type variables can be introspected. e.g.:
T.__name__ == ‘T’ T.__constraints__ == () T.__covariant__ == False T.__contravariant__ = False A.__constraints__ == (str, bytes)Note that only type variables defined in global scope can be pickled.
-
__bound__
¶
-
__constraints__
¶
-
__contravariant__
¶
-
__covariant__
¶
-
__init__
(name, *constraints, bound=None, covariant=False, contravariant=False)¶ Initialize self. See help(type(self)) for accurate signature.
-
__name__
= 'TypeVar'¶
-
__reduce__
()¶ Helper for pickle.
-
__repr__
()¶ Return repr(self).
-
pyavia.types.
coax_type
(x, *types, default=None)¶ Try converting x into a series of types, returning first result which passes test: next_type(x) - x == 0.
Examples
>>> coax_type(3.5, int, float) # float result. 3.5 >>> coax_type(3.0, int, str) # int result. 3 >>> coax_type("3.0", int, float) # Error: 3.0 != "3.0". Traceback (most recent call last): ... ValueError: Couldn't coax '3.0' to <class 'int'> or <class 'float'>. >>> xa = 3 + 2j >>> coax_type(xa, int, float, default=xa) # Can't conv., gives default. (3+2j)
Parameters: - x – Argument to be converted.
- types (list_like) – Target types to use when trying conversion.
- default – Value to return if conversion was unsuccessful.
Returns: x converted to the first successful type (if possible) or default.
Return type: x_converted
Raises: ValueError
– If default is None and conversion was unsuccessful.
-
pyavia.types.
dataclass_fromlist
(dc_type: Type, init_vals: Sequence)¶ Initialise a dataclass of type dc_type using a list of values init_vals ordered to match the class fields (i.e. as returned by
dataclass_names
). The length of the init_vals cannot exceed the number of dataclass field names. If shorter, remaining fields are unassigned.
-
pyavia.types.
dataclass_names
(dc) → List[str]¶ When passed a type or specific instance of a dataclass, returns an ordered list containing the names of the individual fields.
-
pyavia.types.
fields
(class_or_instance)¶ Return a tuple describing the fields of this dataclass.
Accepts a dataclass or an instance of one. Tuple elements are of type Field.
-
pyavia.types.
force_type
(x, *types)¶ Try converting x into a series of types, with no check on the conversion validity.
Examples
>>> force_type(3.5, int, float) ... # Results in an int, because int(x) accepts float and truncates. 3 >>> force_type("3.5+4j", float, complex) (3.5+4j) >>> force_type(3.5+4j, int, float, str) '(3.5+4j)'
Parameters: - x – Argument to be converted.
- types (list_like) – Target types to use when trying conversion.
Returns: x converted to the first successful type, if possible.
Return type: x_converted
Raises: ValueError
– If no conversion was possible.
-
pyavia.types.
is_dataclass
(obj)¶ Returns True if obj is a dataclass or an instance of a dataclass.
-
pyavia.types.
make_sentinel
(name='_MISSING', var_name=None)¶ This factory function is taken directly from ``boltons.typeutils`` and has only cosmetic changes.
Creates and returns a new instance of a new class, suitable for usage as a “sentinel”, a kind of singleton often used to indicate a value is missing when
None
is a valid input.Examples
>>> make_sentinel(var_name='_MISSING') _MISSING
Sentinels can be used as default values for optional function arguments, partly because of its less-confusing appearance in automatically generated documentation. Sentinels also function well as placeholders in queues and linked lists.
>>> make_sentinel('TEST') == make_sentinel('TEST') False >>> type(make_sentinel('TEST')) == type(make_sentinel('TEST')) False
Parameters: - name (str) – Name of the Sentinel.
- var_name (str (optional)) – Set this name to the name of the variable in its respective module enable pickleability.