| 3172 | |
| 3173 | |
| 3174 | class _TypedDictMeta(type): |
| 3175 | def __new__(cls, name, bases, ns, total=True, closed=None, |
| 3176 | extra_items=NoExtraItems): |
| 3177 | """Create a new typed dict class object. |
| 3178 | |
| 3179 | This method is called when TypedDict is subclassed, |
| 3180 | or when TypedDict is instantiated. This way |
| 3181 | TypedDict supports all three syntax forms described in its docstring. |
| 3182 | Subclasses and instances of TypedDict return actual dictionaries. |
| 3183 | """ |
| 3184 | for base in bases: |
| 3185 | if type(base) is not _TypedDictMeta and base is not Generic: |
| 3186 | raise TypeError('cannot inherit from both a TypedDict type ' |
| 3187 | 'and a non-TypedDict base class') |
| 3188 | if closed is not None and extra_items is not NoExtraItems: |
| 3189 | raise TypeError(f"Cannot combine closed={closed!r} and extra_items") |
| 3190 | |
| 3191 | if any(issubclass(b, Generic) for b in bases): |
| 3192 | generic_base = (Generic,) |
| 3193 | else: |
| 3194 | generic_base = () |
| 3195 | |
| 3196 | ns_annotations = ns.pop('__annotations__', None) |
| 3197 | |
| 3198 | tp_dict = type.__new__(_TypedDictMeta, name, (*generic_base, dict), ns) |
| 3199 | |
| 3200 | if not hasattr(tp_dict, '__orig_bases__'): |
| 3201 | tp_dict.__orig_bases__ = bases |
| 3202 | |
| 3203 | if ns_annotations is not None: |
| 3204 | own_annotate = None |
| 3205 | own_annotations = ns_annotations |
| 3206 | elif (own_annotate := _lazy_annotationlib.get_annotate_from_class_namespace(ns)) is not None: |
| 3207 | own_annotations = _lazy_annotationlib.call_annotate_function( |
| 3208 | own_annotate, _lazy_annotationlib.Format.FORWARDREF, owner=tp_dict |
| 3209 | ) |
| 3210 | else: |
| 3211 | own_annotate = None |
| 3212 | own_annotations = {} |
| 3213 | msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type" |
| 3214 | own_checked_annotations = { |
| 3215 | n: _type_check(tp, msg, owner=tp_dict, module=tp_dict.__module__) |
| 3216 | for n, tp in own_annotations.items() |
| 3217 | } |
| 3218 | required_keys = set() |
| 3219 | optional_keys = set() |
| 3220 | readonly_keys = set() |
| 3221 | mutable_keys = set() |
| 3222 | |
| 3223 | for base in bases: |
| 3224 | base_required = base.__dict__.get('__required_keys__', set()) |
| 3225 | required_keys |= base_required |
| 3226 | optional_keys -= base_required |
| 3227 | |
| 3228 | base_optional = base.__dict__.get('__optional_keys__', set()) |
| 3229 | required_keys -= base_optional |
| 3230 | optional_keys |= base_optional |
| 3231 |
no outgoing calls
no test coverage detected
searching dependent graphs…