Handle config values read in INI mode. In INI mode, values are stored as str or list[str] only, and coerced from string based on the registered type.
(
self,
name: str,
canonical_name: str,
type: str,
value: str | list[str],
default: Any,
)
| 1757 | assert_never(mode) |
| 1758 | |
| 1759 | def _getini_ini( |
| 1760 | self, |
| 1761 | name: str, |
| 1762 | canonical_name: str, |
| 1763 | type: str, |
| 1764 | value: str | list[str], |
| 1765 | default: Any, |
| 1766 | ): |
| 1767 | """Handle config values read in INI mode. |
| 1768 | |
| 1769 | In INI mode, values are stored as str or list[str] only, and coerced |
| 1770 | from string based on the registered type. |
| 1771 | """ |
| 1772 | # Note: some coercions are only required if we are reading from .ini |
| 1773 | # files, because the file format doesn't contain type information, but |
| 1774 | # when reading from toml (in ini mode) we will get either str or list of |
| 1775 | # str values (see load_config_dict_from_file). For example: |
| 1776 | # |
| 1777 | # ini: |
| 1778 | # a_line_list = "tests acceptance" |
| 1779 | # |
| 1780 | # in this case, we need to split the string to obtain a list of strings. |
| 1781 | # |
| 1782 | # toml (ini mode): |
| 1783 | # a_line_list = ["tests", "acceptance"] |
| 1784 | # |
| 1785 | # in this case, we already have a list ready to use. |
| 1786 | if type == "paths": |
| 1787 | dp = ( |
| 1788 | self.inipath.parent |
| 1789 | if self.inipath is not None |
| 1790 | else self.invocation_params.dir |
| 1791 | ) |
| 1792 | input_values = shlex.split(value) if isinstance(value, str) else value |
| 1793 | return [dp / x for x in input_values] |
| 1794 | elif type == "args": |
| 1795 | return shlex.split(value) if isinstance(value, str) else value |
| 1796 | elif type == "linelist": |
| 1797 | if isinstance(value, str): |
| 1798 | return [t for t in map(lambda x: x.strip(), value.split("\n")) if t] |
| 1799 | else: |
| 1800 | return value |
| 1801 | elif type == "bool": |
| 1802 | return _strtobool(str(value).strip()) |
| 1803 | elif type == "string": |
| 1804 | return value |
| 1805 | elif type == "int": |
| 1806 | if not isinstance(value, str): |
| 1807 | raise TypeError( |
| 1808 | f"Expected an int string for option {name} of type integer, but got: {value!r}" |
| 1809 | ) from None |
| 1810 | return int(value) |
| 1811 | elif type == "float": |
| 1812 | if not isinstance(value, str): |
| 1813 | raise TypeError( |
| 1814 | f"Expected a float string for option {name} of type float, but got: {value!r}" |
| 1815 | ) from None |
| 1816 | return float(value) |
no test coverage detected