MCPcopy Create free account
hub / github.com/ipython/traitlets / Bytes

Class Bytes

traitlets/traitlets.py:2867–2897  ·  view source on GitHub ↗

A trait for byte strings.

Source from the content-addressed store, hash-verified

2865# for Python 3 conversion and for reliable unicode behaviour on Python 2. So
2866# we don't have a Str type.
2867class 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
2900class CBytes(Bytes, TraitType[bytes, t.Any]):

Callers 3

BytesTraitClass · 0.90
MultiTupleTraitClass · 0.90
BarClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…