class_docstr: Representation of distribution metadata.
  
  Compared to :class:`RawMetadata`, this class provides objects representing
  metadata fields instead of only using built-in types. Any invalid metadata
  will cause :exc:`InvalidMetadata` to be raised (with a
  :py:attr:`~BaseException.__cause__` attribute as appropriate).
class_docstr_tok: ["Representation","of","distribution","metadata",".","Compared","to",":","class",":","`","RawMetadata","`",",","this","class","provides","objects","representing","metadata","fields","instead","of","only","using","built-in","types",".","Any","invalid","metadata","will","cause",":","exc",":","`","InvalidMetadata","`","to","be","raised","(","with","a",":","py",":","attr",":","`","~BaseException.__cause__","`","attribute","as","appropriate",")","."]
docstr: 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`.
docstr_tok: ["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","`","."]
code: def from_raw(
      cls, data: RawMetadata, *, validate: bool = True
  ) -> "Metadata":
      """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`.
      """
      ins = cls()
      ins._raw = (
          data.copy()
      )  # Mutations occur due to caching enriched values.
  
      if validate:
          exceptions: List[Exception] = []
          try:
              metadata_version = ins.metadata_version
              metadata_age = _VALID_METADATA_VERSIONS.index(
                  metadata_version
              )
          except InvalidMetadata as metadata_version_exc:
              exceptions.append(metadata_version_exc)
              metadata_version = None
  
          # Make sure to check for the fields that are present, the required
          # fields (so their absence can be reported).
          fields_to_check = frozenset(ins._raw) | _REQUIRED_ATTRS
          # Remove fields that have already been checked.
          fields_to_check -= {"metadata_version"}
  
          for key in fields_to_check:
              try:
                  if metadata_version:
                      # Can't use getattr() as that triggers descriptor protocol which
                      # will fail due to no value for the instance argument.
                      try:
                          field_metadata_version = cls.__dict__[
                              key
                          ].added
                      except KeyError:
                          exc = InvalidMetadata(
                              key, f"unrecognized field: {key!r}"
                          )
                          exceptions.append(exc)
                          continue
                      field_age = _VALID_METADATA_VERSIONS.index(
                          field_metadata_version
                      )
                      if field_age > metadata_age:
                          field = _RAW_TO_EMAIL_MAPPING[key]
                          exc = InvalidMetadata(
                              field,
                              "{field} introduced in metadata version "
                              "{field_metadata_version}, not {metadata_version}",
                          )
                          exceptions.append(exc)
                          continue
                  getattr(ins, key)
              except InvalidMetadata as exc:
                  exceptions.append(exc)
  
          if exceptions:
              raise ExceptionGroup("invalid metadata", exceptions)
  
      return ins
  
imports: import email.feedparser
  import email.header
  import email.message
  import email.parser
  import email.policy
  import sys
  import typing
  from typing import Any, Callable, Dict, Generic, List, Optional, Tuple, Type, Union, cast
  from .None import requirements, specifiers, utils, version
label_desc: Use image node_id 1 for calling the Metadata obj's underlying member method code with example usage: obj.from_raw(cls, data) and returns: ins