code: def addresses_from_address_families(address_mapper, specs):
    
    # Capture a Snapshot covering all paths for these Specs, then group by directory.
    snapshot = yield Get(Snapshot, PathGlobs, _spec_to_globs(address_mapper, specs))
    dirnames = {dirname(f) for f in snapshot.files}
    address_families = yield [Get(AddressFamily, Dir(d)) for d in dirnames]
    address_family_by_directory = {af.namespace: af for af in address_families}
  
    matched_addresses = OrderedSet()
    for spec in specs:
      # NB: if a spec is provided which expands to some number of targets, but those targets match
      # --exclude-target-regexp, we do NOT fail! This is why we wait to apply the tag and exclude
      # patterns until we gather all the targets the spec would have matched without them.
      try:
        addr_families_for_spec = spec.matching_address_families(address_family_by_directory)
      except Spec.AddressFamilyResolutionError as e:
        raise raise_from(ResolveError(e), e)
  
      try:
        all_addr_tgt_pairs = spec.address_target_pairs_from_address_families(addr_families_for_spec)
      except Spec.AddressResolutionError as e:
        raise raise_from(AddressLookupError(e), e)
      except SingleAddress._SingleAddressResolutionError as e:
        _raise_did_you_mean(e.single_address_family, e.name, source=e)
  
      matched_addresses.update(
        addr for (addr, tgt) in all_addr_tgt_pairs
        if specs.matcher.matches_target_address_pair(addr, tgt)
      )
  
    # NB: This may be empty, as the result of filtering by tag and exclude patterns!
    yield BuildFileAddresses(tuple(matched_addresses))
docs: Given an AddressMapper and list of Specs, return matching BuildFileAddresses.
  
    :raises: :class:`ResolveError` if:
       - there were no matching AddressFamilies, or
       - the Spec matches no addresses for SingleAddresses.
    :raises: :class:`AddressLookupError` if no targets are matched for non-SingleAddress specs.