Cast a single-key dict from a string. Evaluated when parsing CLI configuration from a string. Dicts expect strings of the form key=value. Returns a one-key dictionary, which will be merged in :meth:`.from_string_list`.
(self, s: str)
| 4100 | return combined |
| 4101 | |
| 4102 | def item_from_string(self, s: str) -> dict[K, V]: |
| 4103 | """Cast a single-key dict from a string. |
| 4104 | |
| 4105 | Evaluated when parsing CLI configuration from a string. |
| 4106 | |
| 4107 | Dicts expect strings of the form key=value. |
| 4108 | |
| 4109 | Returns a one-key dictionary, |
| 4110 | which will be merged in :meth:`.from_string_list`. |
| 4111 | """ |
| 4112 | |
| 4113 | if "=" not in s: |
| 4114 | raise TraitError( |
| 4115 | f"'{self.__class__.__name__}' options must have the form 'key=value', got {s!r}" |
| 4116 | ) |
| 4117 | key, value = s.split("=", 1) |
| 4118 | |
| 4119 | # cast key with key trait, if defined |
| 4120 | if self._key_trait: |
| 4121 | key = self._key_trait.from_string(key) |
| 4122 | |
| 4123 | # cast value with value trait, if defined (per-key or global) |
| 4124 | value_trait = (self._per_key_traits or {}).get(key, self._value_trait) |
| 4125 | if value_trait: |
| 4126 | value = value_trait.from_string(value) |
| 4127 | return {key: value} # type:ignore[dict-item] |
| 4128 | |
| 4129 | |
| 4130 | class TCPAddress(TraitType[G, S]): |
no test coverage detected