Create an instance from :class:`RawMetadata`. If *validate* is true, all metadata will be validated. All exceptions related to validation will be gathered and raised as an :class:`ExceptionGroup`.
(cls, data: RawMetadata, *, validate: bool = True)
| 648 | |
| 649 | @classmethod |
| 650 | def from_raw(cls, data: RawMetadata, *, validate: bool = True) -> "Metadata": |
| 651 | """Create an instance from :class:`RawMetadata`. |
| 652 | |
| 653 | If *validate* is true, all metadata will be validated. All exceptions |
| 654 | related to validation will be gathered and raised as an :class:`ExceptionGroup`. |
| 655 | """ |
| 656 | ins = cls() |
| 657 | ins._raw = data.copy() # Mutations occur due to caching enriched values. |
| 658 | |
| 659 | if validate: |
| 660 | exceptions: List[Exception] = [] |
| 661 | try: |
| 662 | metadata_version = ins.metadata_version |
| 663 | metadata_age = _VALID_METADATA_VERSIONS.index(metadata_version) |
| 664 | except InvalidMetadata as metadata_version_exc: |
| 665 | exceptions.append(metadata_version_exc) |
| 666 | metadata_version = None |
| 667 | |
| 668 | # Make sure to check for the fields that are present, the required |
| 669 | # fields (so their absence can be reported). |
| 670 | fields_to_check = frozenset(ins._raw) | _REQUIRED_ATTRS |
| 671 | # Remove fields that have already been checked. |
| 672 | fields_to_check -= {"metadata_version"} |
| 673 | |
| 674 | for key in fields_to_check: |
| 675 | try: |
| 676 | if metadata_version: |
| 677 | # Can't use getattr() as that triggers descriptor protocol which |
| 678 | # will fail due to no value for the instance argument. |
| 679 | try: |
| 680 | field_metadata_version = cls.__dict__[key].added |
| 681 | except KeyError: |
| 682 | exc = InvalidMetadata(key, f"unrecognized field: {key!r}") |
| 683 | exceptions.append(exc) |
| 684 | continue |
| 685 | field_age = _VALID_METADATA_VERSIONS.index( |
| 686 | field_metadata_version |
| 687 | ) |
| 688 | if field_age > metadata_age: |
| 689 | field = _RAW_TO_EMAIL_MAPPING[key] |
| 690 | exc = InvalidMetadata( |
| 691 | field, |
| 692 | "{field} introduced in metadata version " |
| 693 | "{field_metadata_version}, not {metadata_version}", |
| 694 | ) |
| 695 | exceptions.append(exc) |
| 696 | continue |
| 697 | getattr(ins, key) |
| 698 | except InvalidMetadata as exc: |
| 699 | exceptions.append(exc) |
| 700 | |
| 701 | if exceptions: |
| 702 | raise ExceptionGroup("invalid metadata", exceptions) |
| 703 | |
| 704 | return ins |
| 705 | |
| 706 | @classmethod |
| 707 | def from_email( |
no test coverage detected