Create a dict trait type from a Python dict. The default value is created by doing ``dict(default_value)``, which creates a copy of the ``default_value``. Parameters ---------- value_trait : TraitType [ optional ] The specified trait type to chec
(
self,
value_trait: TraitType[t.Any, t.Any] | dict[K, V] | Sentinel | None = None,
per_key_traits: t.Any = None,
key_trait: TraitType[t.Any, t.Any] | None = None,
default_value: dict[K, V] | Sentinel | None = Undefined,
**kwargs: t.Any,
)
| 3873 | _key_trait = None |
| 3874 | |
| 3875 | def __init__( |
| 3876 | self, |
| 3877 | value_trait: TraitType[t.Any, t.Any] | dict[K, V] | Sentinel | None = None, |
| 3878 | per_key_traits: t.Any = None, |
| 3879 | key_trait: TraitType[t.Any, t.Any] | None = None, |
| 3880 | default_value: dict[K, V] | Sentinel | None = Undefined, |
| 3881 | **kwargs: t.Any, |
| 3882 | ) -> None: |
| 3883 | """Create a dict trait type from a Python dict. |
| 3884 | |
| 3885 | The default value is created by doing ``dict(default_value)``, |
| 3886 | which creates a copy of the ``default_value``. |
| 3887 | |
| 3888 | Parameters |
| 3889 | ---------- |
| 3890 | value_trait : TraitType [ optional ] |
| 3891 | The specified trait type to check and use to restrict the values of |
| 3892 | the dict. If unspecified, values are not checked. |
| 3893 | per_key_traits : Dictionary of {keys:trait types} [ optional, keyword-only ] |
| 3894 | A Python dictionary containing the types that are valid for |
| 3895 | restricting the values of the dict on a per-key basis. |
| 3896 | Each value in this dict should be a Trait for validating |
| 3897 | key_trait : TraitType [ optional, keyword-only ] |
| 3898 | The type for restricting the keys of the dict. If |
| 3899 | unspecified, the types of the keys are not checked. |
| 3900 | default_value : SequenceType [ optional, keyword-only ] |
| 3901 | The default value for the Dict. Must be dict, tuple, or None, and |
| 3902 | will be cast to a dict if not None. If any key or value traits are specified, |
| 3903 | the `default_value` must conform to the constraints. |
| 3904 | |
| 3905 | Examples |
| 3906 | -------- |
| 3907 | a dict whose values must be text |
| 3908 | >>> d = Dict(Unicode()) |
| 3909 | |
| 3910 | d2['n'] must be an integer |
| 3911 | d2['s'] must be text |
| 3912 | >>> d2 = Dict(per_key_traits={"n": Integer(), "s": Unicode()}) |
| 3913 | |
| 3914 | d3's keys must be text |
| 3915 | d3's values must be integers |
| 3916 | >>> d3 = Dict(value_trait=Integer(), key_trait=Unicode()) |
| 3917 | |
| 3918 | """ |
| 3919 | |
| 3920 | # handle deprecated keywords |
| 3921 | trait = kwargs.pop("trait", None) |
| 3922 | if trait is not None: |
| 3923 | if value_trait is not None: |
| 3924 | raise TypeError( |
| 3925 | "Found a value for both `value_trait` and its deprecated alias `trait`." |
| 3926 | ) |
| 3927 | value_trait = trait |
| 3928 | warn( |
| 3929 | "Keyword `trait` is deprecated in traitlets 5.0, use `value_trait` instead", |
| 3930 | DeprecationWarning, |
| 3931 | stacklevel=2, |
| 3932 | ) |