Typed
provide.foundation.parsers.typed
¶
TODO: Add module docstring.
Functions¶
extract_concrete_type
¶
Extract concrete type from type annotation, handling unions, optionals, and string annotations.
This function handles: - Union types (str | None, Union[str, None]) - Optional types (str | None) - Regular types (str, int, bool) - String annotations (from future import annotations) - Generic types (list[int], dict[str, str])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
|
Any
|
Type annotation from function signature or attrs field |
required |
Returns:
| Type | Description |
|---|---|
type
|
Concrete type that can be used for parsing |
Examples:
>>> extract_concrete_type(str | None)
<class 'str'>
>>> extract_concrete_type('str | None')
<class 'str'>
>>> extract_concrete_type(list[int])
list[int]
Source code in provide/foundation/parsers/typed.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
parse_typed_value
¶
Parse a string value to a specific type.
Handles basic types (int, float, bool, str) and generic types (list, dict). For attrs fields, pass field.type as target_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String value to parse |
required |
target_type
|
type
|
Target type to convert to |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed value of the target type |
Examples:
>>> parse_typed_value("42", int)
42
>>> parse_typed_value("true", bool)
True
>>> parse_typed_value("a,b,c", list)
['a', 'b', 'c']