bagof.core.magic
Attributes
UNSET
module-attribute
A value that indicates that an argument was not set.
Note
This is different from None, which may be a valid value.
Classes
MagicHint
Bases: Generic[T]
Base class for magic objects (factories, converters).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
Any
|
The type hint to use for this magic object. If not provided, the default hint for the class is used. |
UNSET
|
Attributes
BOUND
class-attribute
instance-attribute
The type hint that this magic object is bound to.
DEFAULT
class-attribute
instance-attribute
The default type hint for this magic object.
FALLBACK
class-attribute
instance-attribute
UNWRAP
class-attribute
instance-attribute
The hints that unwrapped, origin and args transparently
unwrap before introspecting [hint][bagof.core.magic.MagicHint.hint].
Note
A TypeVar is resolved to its default, its
(union of) constraints, or its bound - in that order - so that a
typevar is introspected exactly like the hint it stands for. This
matches fallback, which resolves typevars through
get_concrete_type.
Set to (tx.Annotated,) to opt out and introspect typevars as-is.
unwrapped
property
origin
property
args
property
Methods:
error
Raise a MagicError with the given value and message.
MultipleCauses
MagicError
Functions:
get_concrete_type
Get a valid concrete type from a type hint.
- If the hint is annotated, the
Annotatedwrapper is removed. - If the hint has an origin, it is used.
- If the hint is a
TypeVar:- its default value is used, if it has one; otherwise
- its constraints are used, if it has any; otherwise
- its bound is used, if it has one; otherwise
- the fallback type is used, if it is provided; otherwise
- a
TypeErroris raised.
- If the (resolved) hint is a concrete, non-abstract type, it is returned as is; otherwise
- The fallback type is used, if it is provided; otherwise
- A
TypeErroris raised.
get_default
Get a default value from a type hint.
- If the hint is a
Literal, the first value in the literal is returned (None, ifNoneis one of the literal's values). - If the hint is a
Unionthat containsNoneType,Noneis returned. - Otherwise, if the hint is a
Union, we recurse through its sub-hints and return the first default found. - If no default value can be found, a
TypeErroris raised. A factory should then be used.
get_from_registry
Get the best matching value from a registry whose keys are types or type hints.
The best match is the registry key that is the narrowest superclass
(or superhint) of hint, following its MRO; exact matches are always
preferred. If hint is Annotated and no match is
found for it directly, the search is retried against its unwrapped
hint.
issubclassable
is_typeddict
Return true if an object is a TypedDict or a subclass
of it.
Tip
This function differs from
typing.is_typeddict in that it returns True
for TypedDict itself.
safe_issubclass
Safe subclass (does not fail if arguments are not types).
Warning
If cls is a TypedDict, this function looks
at subcls's __orig_bases__, instead of its __bases__.
safe_isinstance
Safe isinstance (does not fail if second argument is not a type).
Warning
If cls is a TypedDict, this function looks
at the obj's __orig_bases__, instead of its __bases__.
ishintstance
issubhint
Check that a hint is a sub-hint for another hint.
A hint is a valid subhint if all values that are valid for the hint are also valid for the superhint.
unwrap
Unwrap a type hint from its origin, if it is in the unwrap list.
If TypeVar is one of the origins to unwrap, it will be
unwrapped to its default, bound, or (union of) constraints.
safe_get_origin
Safe version of tx.get_origin.
Can also unwrap some hints (e.g. Annotated)
if asked.
Note
Unlike typing.get_origin, this returns the input hint
itself, instead of None, when the hint is not a generic type.
get_origin_uw
Safe version of tx.get_origin that unwraps
Annotated hints.
Returns the input type, instead of None, if the input is not a
generic type.
safe_get_args
Safe version of tx.get_args.
Returns an empty tuple if the input is not a generic type.
Can also unwrap some hints (e.g. Annotated) if asked.
get_args_uw
Safe version of tx.get_args that unwraps
Annotated hints.
Returns an empty tuple if the input is not a generic type.
eq_safenan
Map a value to a form that compares equal across NaNs.
Since float("nan") != float("nan"), comparing values that
may contain NaN with == is unsafe. Apply this function to both
operands before comparing them: real NaN values are mapped to the
string "NaN" (so that two NaNs compare equal), while every other
value is returned unchanged.