The guess_type_from_argname function attempts to infer a Python type and a collection container based on a string argument name. It uses pattern matching and a set of predefined name lists (for booleans, integers, floats, and strings) to make a 'best guess'.
Return Value
Returns a Tuple[Optional[str], List[str]]:
Optional[str]: The inferred element type (e.g., 'int', 'str', 'float', 'bool'). Returns None if no type can be determined.List[str]: A list of container types (e.g., ['List'], ['Set'], ['Iterable']). If the type is a scalar (not a container), this list is empty [].
Matching Logic Examples:
- Container + Type:
list_ints $\rightarrow$ ('int', ['List']) - Type + Container:
latitude_list $\rightarrow$ ('float', ['List']) - Container of Type:
set_of_widths $\rightarrow$ ('int', ['Set']) - Boolean Prefixes:
is_valid $\rightarrow$ ('bool', []) - Plurals:
users $\rightarrow$ ('Any', ['Sequence']) (via recursive singular check) - Filesystem:
path_to_file $\rightarrow$ (None, []) (explicitly avoids guessing str for paths to encourage pathlib.Path)
from autotyping.guess_type import guess_type_from_argname
# Example: Container with type suffix
element_type, containers = guess_type_from_argname("list_ints")
print(element_type, containers) # Output: int ['List']
# Example: Type with container suffix
element_type, containers = guess_type_from_argname("latitude_list")
print(element_type, containers) # Output: float ['List']
# Example: Boolean name
element_type, containers = guess_type_from_argname("is_active")
print(element_type, containers) # Output: bool []
# Example: Scalar integer name
element_type, containers = guess_type_from_argname("width")
print(element_type, containers) # Output: int []