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._utils import decodes_without_error
9from chardet.registry import EncodingInfo
10
11
12def filter_by_validity(
13 data: bytes, candidates: tuple[EncodingInfo, ...]
14) -> tuple[EncodingInfo, ...]:
15 """Filter candidates to only those where *data* decodes without errors.
16
17 :param data: The raw byte data to test.
18 :param candidates: Encoding candidates to validate.
19 :returns: The subset of *candidates* that can decode *data*.
20 """
21 if not data:
22 return candidates
23
24 return tuple(enc for enc in candidates if decodes_without_error(data, enc.name))