A trait for byte strings.
| 2865 | # for Python 3 conversion and for reliable unicode behaviour on Python 2. So |
| 2866 | # we don't have a Str type. |
| 2867 | class Bytes(TraitType[bytes, bytes]): |
| 2868 | """A trait for byte strings.""" |
| 2869 | |
| 2870 | default_value = b"" |
| 2871 | info_text = "a bytes object" |
| 2872 | |
| 2873 | def validate(self, obj: t.Any, value: t.Any) -> bytes | None: |
| 2874 | if isinstance(value, bytes): |
| 2875 | return value |
| 2876 | self.error(obj, value) |
| 2877 | |
| 2878 | def from_string(self, s: str) -> bytes | None: |
| 2879 | if self.allow_none and s == "None": |
| 2880 | return None |
| 2881 | if len(s) >= 3: |
| 2882 | # handle deprecated b"string" |
| 2883 | for quote in ('"', "'"): |
| 2884 | if s[:2] == f"b{quote}" and s[-1] == quote: |
| 2885 | old_s = s |
| 2886 | s = s[2:-1] |
| 2887 | warn( |
| 2888 | "Supporting extra quotes around Bytes is deprecated in traitlets 5.0. " |
| 2889 | f"Use {s!r} instead of {old_s!r}.", |
| 2890 | DeprecationWarning, |
| 2891 | stacklevel=2, |
| 2892 | ) |
| 2893 | break |
| 2894 | return s.encode("utf8") |
| 2895 | |
| 2896 | def subclass_init(self, cls: type[t.Any]) -> None: |
| 2897 | pass # fully opt out of instance_init |
| 2898 | |
| 2899 | |
| 2900 | class CBytes(Bytes, TraitType[bytes, t.Any]): |
no outgoing calls
no test coverage detected
searching dependent graphs…