An instance of a Python dict. One or more traits can be passed to the constructor to validate the keys and/or values of the dict. If you need more detailed validation, you may use a custom validator method. .. versionchanged:: 5.0 Added key_trait for validating dict key
| 3855 | |
| 3856 | |
| 3857 | class Dict(Instance["dict[K, V]"]): |
| 3858 | """An instance of a Python dict. |
| 3859 | |
| 3860 | One or more traits can be passed to the constructor |
| 3861 | to validate the keys and/or values of the dict. |
| 3862 | If you need more detailed validation, |
| 3863 | you may use a custom validator method. |
| 3864 | |
| 3865 | .. versionchanged:: 5.0 |
| 3866 | Added key_trait for validating dict keys. |
| 3867 | |
| 3868 | .. versionchanged:: 5.0 |
| 3869 | Deprecated ambiguous ``trait``, ``traits`` args in favor of ``value_trait``, ``per_key_traits``. |
| 3870 | """ |
| 3871 | |
| 3872 | _value_trait = None |
| 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 |
no outgoing calls
searching dependent graphs…