type_matching¶
The type_matching module
holds classes and functions related to object types
and comparisons between objects.
Functions¶
- class StrictTypeMatcher(type_: type | tuple | UnionType | None)¶
Bases:
TypeMatcher
,Generic
[T
]A
StrictTypeMatcher
used to check whether a variable is exactly a specific type. Instances of this class are callable, and can be called with a value to return a boolean representing whether the value is the type of theStrictTypeMatcher
. The class itself can be used as a parametrized type hint for plugging into instances of itself andTypeMatcher
.
- class TypeMatcher(type_: type | tuple | UnionType | None)¶
Bases:
Generic
[T
]A
TypeMatcher
used to check whether a variable is a specific type. Instances of this class are callable, and can be called with a value to return a boolean representing whether the value is the type of theTypeMatcher
. The class itself can be used as a parametrized type hint for plugging into instances of itself.Warning
This class makes a distinction between booleans and integers, which is not standard, but in several cases is more logical behavior.
- property type¶
What type is the TypeMatcher checking for?
- check_dict_equality(dict1: dict, dict2: dict)¶
Checks for dictionary equality regardless of whether the dictionaries are recursive or not.
- check_function_equality(func1: function, func2: Any)¶
Checks for equality of two functions. This equality is not standard equality, but is closer to how a human would interpret similarity of functions. It is intended to be location-agnostic as far as is possible, and is tested for functions nested within other functions and static methods in classes.
Warning
Decorated functions will generally fail to compare equal.
- check_iterable_not_string(value: Any) bool ¶
Check if a value is iterable, but not a string.
- check_list_equality(list1: list | tuple, list2: list | tuple)¶
Checks for list equality regardless of whether the lists are recursive or not.
- hash_function(func: function)¶
This can hash a function insofar as its static image at the time of hashing. Since functions are technically mutable, it is heavily advised that use of this is avoided unless a function is truly going to be treated as immutable. This means:
No attributes may be set on a function after hashing.
The function should not use *global* variables that are changed after hashing.
The function should have no internal constants which change.
This should not be used on decorated functions.
- strict_check_dict_equality(dict1: dict, dict2: dict)¶
Checks for dictionary equality regardless of whether the dictionaries are recursive or not. Also makes the distinction of
True is not 1
andFalse is not 0
.
- strict_check_list_equality(list1: list | tuple, list2: list | tuple)¶
Checks for list equality regardless of whether the lists are recursive or not. Also makes the distinction of
True is not 1
andFalse is not 0
.
Classes¶
- class TypeMatcher(type_: type | tuple | UnionType | None)¶
Bases:
Generic
[T
]A
TypeMatcher
used to check whether a variable is a specific type. Instances of this class are callable, and can be called with a value to return a boolean representing whether the value is the type of theTypeMatcher
. The class itself can be used as a parametrized type hint for plugging into instances of itself.Warning
This class makes a distinction between booleans and integers, which is not standard, but in several cases is more logical behavior.
- property type¶
What type is the TypeMatcher checking for?
- class TransformerFilter(input_rules: Dict[type, callable], raise_on_fail: bool = True)¶
Bases:
object
A filter with rules and methods to transform values put through it based on their type. There are two ways to use this class:
1. An instance of this class can be used as a property in another class. It can be declared outside the class, and re-used in multiple classes. When this property is set, the type of the value being set will be checked against each type specified in the
input_rules
, and if it matches any of them, the function at that key withininput_rules
will be called with that value as its sole argument, then will store the result as the property.2. An instance of this class can be used as a function itself, being called on a value to attempt to transform it in the same way it would as a property.
Note
In the case that the value received by a
TransformerFilter
does not match any type ininput_rules
it will store or return...
unlessraise_on_fail
is set toTrue
.- Parameters:
input_rules (Dict[type, callable]) – A dictionary using types as keys and functions as the values. Each function should correspond with its key’s type, and should be built to appropriately convert that type.
raise_on_fail (bool) – Whether the
TransformerFilter
should raise an error when attempting to set a value to a type it doesn’t have a converter for. IfFalse
, then any invalid types will result in...
.
- copy() TransformerFilter ¶
Creates a copy of a
TransformerFilter
.