types
Attributes¶
TypeAnnotation
module-attribute
¶
A function parameter's type annotation may be any of the following:
1) type, when declaring any of the built-in Python types
2) typing._GenericAlias, when declaring generic collection types or union types using pre-PEP
585 and pre-PEP 604 syntax (e.g. List[int], Optional[int], or Union[int, None])
3) types.UnionType, when declaring union types using PEP604 syntax (e.g. int | None)
4) types.GenericAlias, when declaring generic collection types using PEP 585 syntax (e.g.
list[int])
types.GenericAlias is a subclass of type, but typing._GenericAlias and types.UnionType are
not and must be considered explicitly.
Classes¶
InspectException ¶
Functions¶
all_not_none ¶
Type guard that checks all Optional collection elements are not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Iterable[T | None]
|
Collection of Optional elements. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if no elements are None, False otherwise. When True, narrows the collection type from |
bool
|
|
Source code in fgpyo/util/types.py
is_constructible_from_str ¶
is_constructible_from_str(type_: TypeAnnotation) -> TypeGuard[type]
Returns true if the provided type is a class constructible from a single str argument.
Source code in fgpyo/util/types.py
is_known_str_constructible ¶
is_known_str_constructible(type_: TypeAnnotation) -> TypeGuard[type]
Returns true if type_ is one of the built-in types known to be constructible from a str.
Complements is_constructible_from_str, which detects str-constructibility via constructor
signature inspection. This predicate covers types whose constructors aren't annotated for
introspection (e.g. int, str, float) or whose subclasses don't all share an annotation
(e.g. PurePath).
Source code in fgpyo/util/types.py
is_list_like ¶
make_enum_parser ¶
make_literal_parser ¶
make_literal_parser(literal: TypeAnnotation, parsers: Iterable[Callable[[str], LiteralType]]) -> partial
Generates a parser function for a literal type object.
make_union_parser ¶
make_union_parser(union: TypeAnnotation, parsers: Iterable[Callable[[str], UnionType]]) -> partial
Generates a parser function for a union type object.
none_parser ¶
Returns None if the value is 'None', else raises an error.
parse_bool ¶
Parses strings into bools.
Accounts for the many different text representations of bools that can be used.