A trait for unicode strings.
| 2908 | |
| 2909 | |
| 2910 | class Unicode(TraitType[G, S]): |
| 2911 | """A trait for unicode strings.""" |
| 2912 | |
| 2913 | default_value = "" |
| 2914 | info_text = "a unicode string" |
| 2915 | |
| 2916 | if t.TYPE_CHECKING: |
| 2917 | |
| 2918 | @t.overload |
| 2919 | def __init__( |
| 2920 | self: Unicode[str, str | bytes], |
| 2921 | default_value: str | Sentinel = ..., |
| 2922 | allow_none: Literal[False] = ..., |
| 2923 | read_only: bool | None = ..., |
| 2924 | help: str | None = ..., |
| 2925 | config: t.Any = ..., |
| 2926 | **kwargs: t.Any, |
| 2927 | ) -> None: |
| 2928 | ... |
| 2929 | |
| 2930 | @t.overload |
| 2931 | def __init__( |
| 2932 | self: Unicode[str | None, str | bytes | None], |
| 2933 | default_value: str | Sentinel | None = ..., |
| 2934 | allow_none: Literal[True] = ..., |
| 2935 | read_only: bool | None = ..., |
| 2936 | help: str | None = ..., |
| 2937 | config: t.Any = ..., |
| 2938 | **kwargs: t.Any, |
| 2939 | ) -> None: |
| 2940 | ... |
| 2941 | |
| 2942 | def __init__( |
| 2943 | self: Unicode[str | None, str | bytes | None], |
| 2944 | default_value: str | Sentinel | None = ..., |
| 2945 | allow_none: bool = ..., |
| 2946 | read_only: bool | None = ..., |
| 2947 | help: str | None = ..., |
| 2948 | config: t.Any = ..., |
| 2949 | **kwargs: t.Any, |
| 2950 | ) -> None: |
| 2951 | ... |
| 2952 | |
| 2953 | def validate(self, obj: t.Any, value: t.Any) -> G: |
| 2954 | if isinstance(value, str): |
| 2955 | return value # type:ignore[return-value] |
| 2956 | if isinstance(value, bytes): |
| 2957 | try: |
| 2958 | return value.decode("ascii", "strict") # type:ignore[return-value] |
| 2959 | except UnicodeDecodeError as e: |
| 2960 | msg = "Could not decode {!r} for unicode trait '{}' of {} instance." |
| 2961 | raise TraitError(msg.format(value, self.name, class_of(obj))) from e |
| 2962 | self.error(obj, value) |
| 2963 | |
| 2964 | def from_string(self, s: str) -> G: |
| 2965 | if self.allow_none and s == "None": |
| 2966 | return None # type:ignore[return-value] |
| 2967 | s = os.path.expanduser(s) |
no outgoing calls
searching dependent graphs…