inspect
Attributes¶
FieldType
module-attribute
¶
TypeAlias for dataclass Fields or attrs Attributes. It will correspond to the correct type for the corresponding _DataclassesOrAttrClass
Functions¶
attr_from ¶
attr_from(cls: Type[_AttrFromType], kwargs: Dict[str, str], parsers: Optional[Dict[type, Callable[[str], Any]]] = None) -> _AttrFromType
Builds an attr or dataclasses class from key-word arguments
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[_AttrFromType]
|
the attr or dataclasses class to be built |
required |
kwargs
|
Dict[str, str]
|
a dictionary of keyword arguments |
required |
parsers
|
Optional[Dict[type, Callable[[str], Any]]]
|
a dictionary of parser functions to apply to specific types |
None
|
Source code in fgpyo/util/inspect.py
dict_parser ¶
dict_parser(cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None) -> partial
Returns a function that parses a stringified dict into a Dict of the correct type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
the type of the class object this is being parsed for (used to get default val for parsers) |
required |
type_
|
TypeAlias
|
the type of the attribute to be parsed parsers: an optional mapping from type to the function to use for parsing that type (allows for parsing of more complex types) |
required |
Source code in fgpyo/util/inspect.py
get_fields ¶
get_fields(cls: Union[_DataclassesOrAttrClass, Type[_DataclassesOrAttrClass]]) -> Tuple[FieldType, ...]
Get the fields tuple from either a dataclasses or attr dataclass (or instance)
Source code in fgpyo/util/inspect.py
get_fields_dict ¶
get_fields_dict(cls: Union[_DataclassesOrAttrClass, Type[_DataclassesOrAttrClass]]) -> Mapping[str, FieldType]
Get the fields dict from either a dataclasses or attr dataclass (or instance)
Source code in fgpyo/util/inspect.py
is_attr_class ¶
list_parser ¶
list_parser(cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None) -> partial
Returns a function that parses a "stringified" list into a List of the correct type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
the type of the class object this is being parsed for (used to get default val for parsers) |
required |
type_
|
TypeAlias
|
the type of the attribute to be parsed |
required |
parsers
|
Optional[Dict[type, Callable[[str], Any]]]
|
an optional mapping from type to the function to use for parsing that type (allows for parsing of more complex types) |
None
|
Source code in fgpyo/util/inspect.py
set_parser ¶
set_parser(cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None) -> partial
Returns a function that parses a stringified set into a Set of the correct type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
the type of the class object this is being parsed for (used to get default val for parsers) |
required |
type_
|
TypeAlias
|
the type of the attribute to be parsed |
required |
parsers
|
Optional[Dict[type, Callable[[str], Any]]]
|
an optional mapping from type to the function to use for parsing that type (allows for parsing of more complex types) |
None
|
Source code in fgpyo/util/inspect.py
split_at_given_level ¶
split_at_given_level(field: str, split_delim: str = ',', increase_depth_chars: Iterable[str] = ('{', '(', '['), decrease_depth_chars: Iterable[str] = ('}', ')', ']')) -> List[str]
Splits a nested field by its outer-most level
Note that this method may produce incorrect results fields containing strings containing unpaired characters that increase or decrease the depth
Not currently smart enough to deal with fields enclosed in quotes ('' or "") - TODO
Source code in fgpyo/util/inspect.py
tuple_parser ¶
tuple_parser(cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None) -> partial
Returns a function that parses a stringified tuple into a Tuple of the correct type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type
|
the type of the class object this is being parsed for (used to get default val for parsers) |
required |
type_
|
TypeAlias
|
the type of the attribute to be parsed |
required |
parsers
|
Optional[Dict[type, Callable[[str], Any]]]
|
an optional mapping from type to the function to use for parsing that type (allows for parsing of more complex types) |
None
|