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

Method make_unique_parameterset_ids

src/_pytest/python.py:899–964  ·  src/_pytest/python.py::IdMaker.make_unique_parameterset_ids

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 class="st">"""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&class="cm">#x27;t be unique
911 otherwise.
912 class="st">"""
913 resolved_ids = list(self._resolve_ids())
914 class="cm"># All IDs must be unique!
915 if len(resolved_ids) != len(set(resolved_ids)):
916 class="cm"># Record the number of occurrences of each ID.
917 id_counts = Counter(resolved_ids)
918
919 if self._strict_parametrization_ids_enabled():
920 parameters = class="st">", ".join(self.argnames)
921 parametersets = class="st">", ".join(
922 [saferepr(list(param.values)) for param in self.parametersets]
923 )
924 ids = class="st">", ".join(
925 id if id is not HIDDEN_PARAM else class="st">"<hidden>" for id in resolved_ids
926 )
927 duplicates = class="st">", ".join(
928 id if id is not HIDDEN_PARAM else class="st">"<hidden>"
929 for id, count in id_counts.items()
930 if count > 1
931 )
932 msg = textwrap.dedent(fclass="st">"""
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 class="st">""").strip() class="cm"># noqa: E501
943 raise nodes.Collector.CollectError(msg)
944
945 class="cm"># Map the ID to its next suffix.
946 id_suffixes: dict[str, int] = defaultdict(int)
947 class="cm"># 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 = class="st">""
953 if id and id[-1].isdigit():
954 suffix = class="st">"_"
955 new_id = fclass="st">"{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