broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start.strip('<>'):
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', 'us-ascii').lower()
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i if best is None else best
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start.strip('<>'):
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', 'us-ascii').lower()
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i if t == 'text/html' or best is None else best
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The plain-text alternative is shown although a richer supported alternative follows it.

Alternatives are ordered by increasing fidelity, so the LAST part whose effective type is in `supported` wins. A multipart/related alternative counts as its root type: the child whose cid equals the start parameter (which carries angle brackets, cids are stored bare) or else the first child. A text/* part is only renderable when its charset (default us-ascii, case-insensitive) is us-ascii, utf-8 or iso-8859-1. With nothing renderable the first part is shown as a fallback. Result {index, type, fallback}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start:
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', 'us-ascii').lower()
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start[1:]:
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', 'us-ascii').lower()
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A multipart/related alternative whose start parameter names a later root is judged by its first child (an image) and skipped.

Alternatives are ordered by increasing fidelity, so the LAST part whose effective type is in `supported` wins. A multipart/related alternative counts as its root type: the child whose cid equals the start parameter (which carries angle brackets, cids are stored bare) or else the first child. A text/* part is only renderable when its charset (default us-ascii, case-insensitive) is us-ascii, utf-8 or iso-8859-1. With nothing renderable the first part is shown as a fallback. Result {index, type, fallback}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start.strip('<>'):
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', '').lower()
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(part, supported):
    def effective(p):
        if p['type'] == 'multipart/related':
            start = p.get('params', {}).get('start')
            for c in p['parts']:
                if start is not None and c.get('cid') == start.strip('<>'):
                    return c['type']
            return p['parts'][0]['type'] if p['parts'] else None
        return p['type']
    ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
    best = None
    for i, p in enumerate(part['parts']):
        t = effective(p)
        cs = p.get('params', {}).get('charset', 'us-ascii')
        if t in supported and (not t.startswith('text/') or cs in ok_charsets):
            best = i
    if best is None:
        return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
    return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Parts without a charset parameter, or with an upper-case charset name, are treated as unrenderable.

Alternatives are ordered by increasing fidelity, so the LAST part whose effective type is in `supported` wins. A multipart/related alternative counts as its root type: the child whose cid equals the start parameter (which carries angle brackets, cids are stored bare) or else the first child. A text/* part is only renderable when its charset (default us-ascii, case-insensitive) is us-ascii, utf-8 or iso-8859-1. With nothing renderable the first part is shown as a fallback. Result {index, type, fallback}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts']):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1 if not prefix else 0):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Every part is fetched with a section number one lower than the server uses.

Returns [section, type] for every non-multipart entity in depth-first order. A single-part message body is section "1". Children of a multipart are numbered 1..n under the parent's number (nested multiparts get their own number). A message/rfc822 part is listed itself; if its encapsulated body is multipart, that body's children are numbered directly under the message part (P.1, P.2...); a single-part encapsulated body is P.1.

split: open-access