1"""Stage 2a: Byte sequence validity filtering.
2
3Note: ``from __future__ import annotations`` is intentionally omitted because
4this module is compiled with mypyc, which does not support PEP 563 string
5annotations.
6"""
7
8from chardet.registry import EncodingInfo
9
10
11def filter_by_validity(
12 data: bytes, candidates: tuple[EncodingInfo, ...]
13) -> tuple[EncodingInfo, ...]:
14 """Filter candidates to only those where *data* decodes without errors.
15
16 :param data: The raw byte data to test.
17 :param candidates: Encoding candidates to validate.
18 :returns: The subset of *candidates* that can decode *data*.
19 """
20 if not data:
21 return candidates
22
23 valid = []
24 for enc in candidates:
25 try:
26 data.decode(enc.name, errors="strict")
27 valid.append(enc)
28 except (UnicodeDecodeError, LookupError):
29 continue
30 return tuple(valid)