typedpy 2.2.2__tar.gz → 2.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -145,7 +145,7 @@ def _check_for_final_violations(classes):
145
145
  )
146
146
  )
147
147
  if "ImmutableStructure" in globals() and is_sub_class(
148
- c, ImmutableStructure
148
+ c, ImmutableStructure
149
149
  ):
150
150
  raise TypeError(
151
151
  "Tried to extend {}, which is an ImmutableStructure. This is forbidden".format(
@@ -210,9 +210,9 @@ class UniqueMixin:
210
210
  def __manage_uniqueness__(self):
211
211
  myclass = self.__class__
212
212
  if (
213
- getattr(myclass, MUST_BE_UNIQUE, False)
214
- and len(getattr(myclass, "_ALL_INSTANCES", set()))
215
- < MAX_NUMBER_OF_INSTANCES_TO_VERIFY_UNIQUENESS
213
+ getattr(myclass, MUST_BE_UNIQUE, False)
214
+ and len(getattr(myclass, "_ALL_INSTANCES", set()))
215
+ < MAX_NUMBER_OF_INSTANCES_TO_VERIFY_UNIQUENESS
216
216
  ):
217
217
  hash_of_instance = self.__hash__()
218
218
  if hash_of_instance in getattr(myclass, "_ALL_INSTANCES", set()):
@@ -236,14 +236,14 @@ class UniqueMixin:
236
236
  structure_class_name
237
237
  ]
238
238
  if (
239
- getattr(self, MUST_BE_UNIQUE, False)
240
- and len(instance_by_value_for_current_struct)
241
- < MAX_NUMBER_OF_INSTANCES_TO_VERIFY_UNIQUENESS
239
+ getattr(self, MUST_BE_UNIQUE, False)
240
+ and len(instance_by_value_for_current_struct)
241
+ < MAX_NUMBER_OF_INSTANCES_TO_VERIFY_UNIQUENESS
242
242
  ):
243
243
  hash_of_field_val = value.__hash__()
244
244
  if (
245
- instance_by_value_for_current_struct.get(hash_of_field_val, instance)
246
- != instance
245
+ instance_by_value_for_current_struct.get(hash_of_field_val, instance)
246
+ != instance
247
247
  ):
248
248
  raise ValueError(
249
249
  "Instance copy of field {} in {}, which is defined as unique. Instance is {}".format(
@@ -320,13 +320,13 @@ class Field(UniqueMixin, metaclass=_FieldMeta):
320
320
  else owner.__dict__[self._name]
321
321
  )
322
322
  is_immutable = (
323
- instance is not None
324
- and getattr(instance, IS_IMMUTABLE, False)
325
- or getattr(self, IS_IMMUTABLE, False)
323
+ instance is not None
324
+ and getattr(instance, IS_IMMUTABLE, False)
325
+ or getattr(self, IS_IMMUTABLE, False)
326
326
  )
327
327
  needs_defensive_copy = (
328
- not isinstance(res, (ImmutableMixin, int, float, str, bool, enum.Enum))
329
- or res is None
328
+ not isinstance(res, (ImmutableMixin, int, float, str, bool, enum.Enum))
329
+ or res is None
330
330
  )
331
331
  return deepcopy(res) if (is_immutable and needs_defensive_copy) else res
332
332
 
@@ -334,7 +334,7 @@ class Field(UniqueMixin, metaclass=_FieldMeta):
334
334
  if getattr(self, IS_IMMUTABLE, False) and self._name in instance.__dict__:
335
335
  raise ValueError("{}: Field is immutable".format(self._name))
336
336
  if getattr(self, IS_IMMUTABLE, False) and not getattr(
337
- self, "_custom_deep_copy_implementation", False
337
+ self, "_custom_deep_copy_implementation", False
338
338
  ):
339
339
  try:
340
340
  instance.__dict__[self._name] = deepcopy(value)
@@ -349,7 +349,7 @@ class Field(UniqueMixin, metaclass=_FieldMeta):
349
349
  instance.__dict__[self._name] = value
350
350
  instance.__manage__uniqueness_of_all_fields__()
351
351
  if getattr(instance, "_instantiated", False) and not getattr(
352
- instance, "_skip_validation", False
352
+ instance, "_skip_validation", False
353
353
  ):
354
354
  instance.__validate__()
355
355
 
@@ -413,34 +413,34 @@ def _get_all_fields_by_name(cls):
413
413
  def _instantiate_fields_if_needed(cls_dict: dict, defaults: dict):
414
414
  for key, val in cls_dict.items():
415
415
  if (
416
- key
417
- not in {
418
- REQUIRED_FIELDS,
419
- ADDITIONAL_PROPERTIES,
420
- IS_IMMUTABLE,
421
- DEFAULTS,
422
- OPTIONAL_FIELDS,
423
- IGNORE_NONE_VALUES,
424
- }
425
- and not isinstance(val, Field)
426
- and not key.startswith("__")
427
- and (
416
+ key
417
+ not in {
418
+ REQUIRED_FIELDS,
419
+ ADDITIONAL_PROPERTIES,
420
+ IS_IMMUTABLE,
421
+ DEFAULTS,
422
+ OPTIONAL_FIELDS,
423
+ IGNORE_NONE_VALUES,
424
+ }
425
+ and not isinstance(val, Field)
426
+ and not key.startswith("__")
427
+ and (
428
428
  Field in getattr(val, "__mro__", []) or is_function_returning_field(val)
429
- )
429
+ )
430
430
  ):
431
431
  new_val = val(default=defaults[key]) if key in defaults else val()
432
432
  cls_dict[key] = new_val
433
433
 
434
434
 
435
435
  def _apply_default_and_update_required_not_to_include_fields_with_defaults(
436
- cls_dict: dict, defaults: dict, fields: list
436
+ cls_dict: dict, defaults: dict, fields: list
437
437
  ):
438
438
  required_fields = set(cls_dict.get(REQUIRED_FIELDS, []))
439
439
  optional_fields = set(cls_dict.get(OPTIONAL_FIELDS, []))
440
440
  required_fields_predefined = REQUIRED_FIELDS in cls_dict
441
441
  for field_name in fields:
442
442
  if field_name in defaults and not getattr(
443
- cls_dict[field_name], "_default", None
443
+ cls_dict[field_name], "_default", None
444
444
  ):
445
445
  cls_dict[field_name]._try_default_value(defaults[field_name])
446
446
  cls_dict[field_name]._default = defaults[field_name]
@@ -547,15 +547,15 @@ def _type_is_generic(v):
547
547
  generic_alias = getattr(typing, "_GenericAlias", Foo)
548
548
  special_generic_alias = getattr(typing, "_SpecialGenericAlias", Foo)
549
549
  return (
550
- (python_ver_36 and isinstance(v, typing_base))
551
- or (
552
- python_ver_atleast_than_37
553
- and isinstance(v, (generic_alias, special_generic_alias))
554
- )
555
- or (
556
- python_ver_atleast_39
557
- and origin in {list, dict, tuple, set, frozenset, deque, typing.Union}
558
- )
550
+ (python_ver_36 and isinstance(v, typing_base))
551
+ or (
552
+ python_ver_atleast_than_37
553
+ and isinstance(v, (generic_alias, special_generic_alias))
554
+ )
555
+ or (
556
+ python_ver_atleast_39
557
+ and origin in {list, dict, tuple, set, frozenset, deque, typing.Union}
558
+ )
559
559
  )
560
560
 
561
561
 
@@ -614,7 +614,7 @@ def add_annotations_to_class_dict(cls_dict):
614
614
  from .fields import AnyOf
615
615
 
616
616
  if isinstance(the_type, AnyOf) and getattr(
617
- the_type, "_is_optional", False
617
+ the_type, "_is_optional", False
618
618
  ):
619
619
  optional_fields.add(k)
620
620
  if k in cls_dict:
@@ -691,6 +691,7 @@ class Structure(UniqueMixin, metaclass=StructMeta):
691
691
  _ignore_none(bool): optional
692
692
  Ignore assignment to None for any field value.
693
693
  Default is False.
694
+ Required fields never ignore None (since they are required)
694
695
 
695
696
  Decorating it with @unique ensures that no all instances of this structure will be unique. It
696
697
  will raise an exception otherwise (see "Uniqueness" section).
@@ -723,14 +724,18 @@ class Structure(UniqueMixin, metaclass=StructMeta):
723
724
  if key in self.__dict__:
724
725
  raise ValueError("Structure is immutable")
725
726
  value = deepcopy(value)
726
- if getattr(self, IGNORE_NONE_VALUES, False) and value is None:
727
+ if all([getattr(self, IGNORE_NONE_VALUES, False),
728
+ value is None,
729
+ key not in getattr(self.__class__, REQUIRED_FIELDS, [])]
730
+ ):
727
731
  return
732
+
728
733
  super().__setattr__(key, value)
729
734
 
730
735
  if (
731
- getattr(self, "_instantiated", False)
732
- and not _is_dunder(key)
733
- and not _is_sunder(key)
736
+ getattr(self, "_instantiated", False)
737
+ and not _is_dunder(key)
738
+ and not _is_sunder(key)
734
739
  ):
735
740
  self.__manage_uniqueness__()
736
741
 
@@ -766,7 +771,7 @@ class Structure(UniqueMixin, metaclass=StructMeta):
766
771
 
767
772
  name = self.__class__.__name__
768
773
  if name.startswith("StructureReference_") and self.__class__.__bases__ == (
769
- Structure,
774
+ Structure,
770
775
  ):
771
776
  name = "Structure"
772
777
  props = []
@@ -797,7 +802,7 @@ class Structure(UniqueMixin, metaclass=StructMeta):
797
802
 
798
803
  def __delitem__(self, key):
799
804
  if isinstance(getattr(self, REQUIRED_FIELDS), list) and key in getattr(
800
- self, REQUIRED_FIELDS
805
+ self, REQUIRED_FIELDS
801
806
  ):
802
807
  raise ValueError("{} is mandatory".format(key))
803
808
  del self.__dict__[key]
@@ -842,9 +847,9 @@ class Structure(UniqueMixin, metaclass=StructMeta):
842
847
  required = props.get(REQUIRED_FIELDS, field_names)
843
848
  additional_props = props.get(ADDITIONAL_PROPERTIES, True)
844
849
  if (
845
- len(field_names) == 1
846
- and required == field_names
847
- and additional_props is False
850
+ len(field_names) == 1
851
+ and required == field_names
852
+ and additional_props is False
848
853
  ):
849
854
  return item in getattr(self, field_names[0], {})
850
855
 
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedpy
3
- Version: 2.2.2
3
+ Version: 2.2.3
4
4
  Summary: Type-safe Python
5
5
  Home-page: http://github.com/loyada/typedpy
6
6
  Author: Danny Loya
7
7
  Author-email: dan.loya@gmail.com
8
8
  License: MIT
9
- Download-URL: https://github.com/loyada/typedpy/archive/v2.2.2.tar.gz
9
+ Download-URL: https://github.com/loyada/typedpy/archive/v2.2.3.tar.gz
10
10
  Description: [![][travis img]][travis]
11
11
  [![][docs img]][docs]
12
12