Пример #1
0
 def rewrite_anonymous_TypedDict(self, typed_dict: type) -> type:
     class_name = get_typed_dict_class_name(self._class_name_hint)
     required_fields, optional_fields = field_annotations(typed_dict)
     has_required_fields = len(required_fields) != 0
     has_optional_fields = len(optional_fields) != 0
     if not has_required_fields and not has_optional_fields:
         raise Exception(
             "Expected empty TypedDicts to be shrunk as Dict[Any, Any]"
             " but got an empty TypedDict anyway")
     elif has_required_fields and not has_optional_fields:
         self._add_typed_dict_class_stub(required_fields, class_name)
     elif not has_required_fields and has_optional_fields:
         self._add_typed_dict_class_stub(optional_fields,
                                         class_name,
                                         total=False)
     else:
         self._add_typed_dict_class_stub(required_fields, class_name)
         base_class_name = class_name
         class_name = get_typed_dict_class_name(
             self._class_name_hint) + 'NonTotal'
         self._add_typed_dict_class_stub(optional_fields,
                                         class_name,
                                         base_class_name,
                                         total=False)
     return make_forward_ref(class_name)
Пример #2
0
 def test_field_annotations(self, required_fields, optional_fields):
     typed_dict = make_typed_dict(required_fields=required_fields,
                                  optional_fields=optional_fields)
     assert field_annotations(typed_dict) == (required_fields,
                                              optional_fields)