Make IDs for a parametrization.
| 868 | @final |
| 869 | @dataclasses.dataclass(frozen=True) |
| 870 | class IdMaker: |
| 871 | """Make IDs for a parametrization.""" |
| 872 | |
| 873 | __slots__ = ( |
| 874 | "argnames", |
| 875 | "config", |
| 876 | "idfn", |
| 877 | "ids", |
| 878 | "nodeid", |
| 879 | "parametersets", |
| 880 | ) |
| 881 | |
| 882 | # The argnames of the parametrization. |
| 883 | argnames: Sequence[str] |
| 884 | # The ParameterSets of the parametrization. |
| 885 | parametersets: Sequence[ParameterSet] |
| 886 | # Optionally, a user-provided callable to make IDs for parameters in a |
| 887 | # ParameterSet. |
| 888 | idfn: Callable[[Any], object | None] | None |
| 889 | # Optionally, explicit IDs for ParameterSets by index. |
| 890 | ids: Sequence[object | None] | None |
| 891 | # Optionally, the pytest config. |
| 892 | # Used for controlling ASCII escaping, determining parametrization ID |
| 893 | # strictness, and for calling the :hook:`pytest_make_parametrize_id` hook. |
| 894 | config: Config | None |
| 895 | # Optionally, the ID of the node being parametrized. |
| 896 | # Used only for clearer error messages. |
| 897 | nodeid: str | None |
| 898 | |
| 899 | def make_unique_parameterset_ids(self) -> list[str | _HiddenParam]: |
| 900 | """Make a unique identifier for each ParameterSet, that may be used to |
| 901 | identify the parametrization in a node ID. |
| 902 | |
| 903 | If strict_parametrization_ids is enabled, and duplicates are detected, |
| 904 | raises CollectError. Otherwise makes the IDs unique as follows: |
| 905 | |
| 906 | Format is <prm_1_token>-...-<prm_n_token>[counter], where prm_x_token is |
| 907 | - user-provided id, if given |
| 908 | - else an id derived from the value, applicable for certain types |
| 909 | - else <argname><parameterset index> |
| 910 | The counter suffix is appended only in case a string wouldn't be unique |
| 911 | otherwise. |
| 912 | """ |
| 913 | resolved_ids = list(self._resolve_ids()) |
| 914 | # All IDs must be unique! |
| 915 | if len(resolved_ids) != len(set(resolved_ids)): |
| 916 | # Record the number of occurrences of each ID. |
| 917 | id_counts = Counter(resolved_ids) |
| 918 | |
| 919 | if self._strict_parametrization_ids_enabled(): |
| 920 | parameters = ", ".join(self.argnames) |
| 921 | parametersets = ", ".join( |
| 922 | [saferepr(list(param.values)) for param in self.parametersets] |
| 923 | ) |
| 924 | ids = ", ".join( |
| 925 | id if id is not HIDDEN_PARAM else "<hidden>" for id in resolved_ids |
| 926 | ) |
| 927 | duplicates = ", ".join( |
no outgoing calls