MCPcopy
hub / github.com/pytest-dev/pytest / make_unique_parameterset_ids

Method make_unique_parameterset_ids

src/_pytest/python.py:899–964  ·  view source on GitHub ↗

Make a unique identifier for each ParameterSet, that may be used to identify the parametrization in a node ID. If strict_parametrization_ids is enabled, and duplicates are detected, raises CollectError. Otherwise makes the IDs unique as follows: Format is <prm_1_tok

(self)

Source from the content-addressed store, hash-verified

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&#x27;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(
928 id if id is not HIDDEN_PARAM else "<hidden>"
929 for id, count in id_counts.items()
930 if count > 1
931 )
932 msg = textwrap.dedent(f"""
933 Duplicate parametrization IDs detected, but strict_parametrization_ids is set.
934
935 Test name: {self.nodeid}
936 Parameters: {parameters}
937 Parameter sets: {parametersets}
938 IDs: {ids}
939 Duplicates: {duplicates}
940
941 You can fix this problem using `@pytest.mark.parametrize(..., ids=...)` or `pytest.param(..., id=...)`.
942 """).strip() # noqa: E501
943 raise nodes.Collector.CollectError(msg)
944
945 # Map the ID to its next suffix.
946 id_suffixes: dict[str, int] = defaultdict(int)
947 # Suffix non-unique IDs to make them unique.
948 for index, id in enumerate(resolved_ids):
949 if id_counts[id] > 1:
950 if id is HIDDEN_PARAM:
951 self._complain_multiple_hidden_parameter_sets()
952 suffix = ""
953 if id and id[-1].isdigit():
954 suffix = "_"
955 new_id = f"{id}{suffix}{id_suffixes[id]}"
956 while new_id in set(resolved_ids):

Calls 6

_resolve_idsMethod · 0.95
safereprFunction · 0.90
joinMethod · 0.80
stripMethod · 0.80