Repository: kedro-org/kedro
File: kedro/io/data_catalog.py (Python)

Code before:
            return True
        matched_pattern = self._match_pattern(self._dataset_patterns, data_set_name)
        if matched_pattern:
            # If the dataset is a patterned dataset, materialise it and add it to
            # the catalog
            data_set_config = self._resolve_config(data_set_name, matched_pattern)
            ds_layer = data_set_config.pop("layer", None)
            if ds_layer:
                if not self.layers:
                    self.layers = {}
                self.layers.setdefault(ds_layer, set()).add(data_set_name)
            data_set = AbstractDataSet.from_config(
                data_set_name,
                data_set_config,
                self._load_versions.get(data_set_name),
                self._save_version,
            )
            self.add(data_set_name, data_set)
            return True
        return False

    def _resolve_config(
        self,
        data_set_name: str,
        matched_pattern: str,
    ) -> dict[str, Any]:
        """Get resolved AbstractDataSet from a factory config"""
        result = parse(matched_pattern, data_set_name)
        config_copy = copy.deepcopy(self._dataset_patterns[matched_pattern])
        # Resolve the factory config for the dataset
        for key, value in config_copy.items():
            if isinstance(value, Iterable) and "}" in value:
                # result.named: gives access to all dict items in the match result.
                # format_map fills in dict values into a string with {...} placeholders
                # of the same key name.
                try:
                    config_copy[key] = str(value).format_map(result.named)
                except KeyError as exc:
                    raise DatasetError(
                        f"Unable to resolve '{key}' for the pattern '{matched_pattern}'"
                    ) from exc
        return config_copy

    def load(self, name: str, version: str = None) -> Any:
        """Loads a registered data set.

        Args:
            name: A data set to be loaded.
            version: Optional argument for concrete data version to be loaded.
                Works only with versioned datasets.


Review comment (suggestion):
IMO `__contains__` should really only check if a dataset is in the catalog or not, but not add it. I would move that responsibility to the `_get_dataset` method instead. Mainly because `__contains__` is a magic function and is meant for implementing the membership operation, whereas now it does more than just that. 

Code after:

            self.add(data_set_name, data_set)
        if data_set_name not in self._data_sets:
            error_msg = f"Dataset '{data_set_name}' not found in the catalog"

            # Flag to turn on/off fuzzy-matching which can be time consuming and
            # slow down plugins like `kedro-viz`
            if suggest:
                matches = difflib.get_close_matches(
                    data_set_name, self._data_sets.keys()
                )
                if matches:
                    suggestions = ", ".join(matches)
                    error_msg += f" - did you mean one of these instead: {suggestions}"
            raise DatasetNotFoundError(error_msg)
        data_set = self._data_sets[data_set_name]
        if version and isinstance(data_set, AbstractVersionedDataSet):
            # we only want to return a similar-looking dataset,
            # not modify the one stored in the current catalog
            data_set = data_set._copy(  # pylint: disable=protected-access
                _version=version
            )

        return data_set

    def __contains__(self, data_set_name):
        """Check if an item is in the catalog as a materialised dataset or pattern"""
        matched_pattern = self._match_pattern(self._dataset_patterns, data_set_name)
        if data_set_name in self._data_sets or matched_pattern:
            return True
        return False

    def _resolve_config(
        self,
        data_set_name: str,
        matched_pattern: str,
    ) -> dict[str, Any]:
        """Get resolved AbstractDataSet from a factory config"""
        result = parse(matched_pattern, data_set_name)
        config_copy = copy.deepcopy(self._dataset_patterns[matched_pattern])
        # Resolve the factory config for the dataset
        for key, value in config_copy.items():
            if isinstance(value, Iterable) and "}" in value:
                # result.named: gives access to all dict items in the match result.
                # format_map fills in dict values into a string with {...} placeholders
                # of the same key name.
                try:
                    config_copy[key] = str(value).format_map(result.named)
                except KeyError as exc:
                    raise DatasetError(
                        f"Unable to resolve '{key}' for the pattern '{matched_pattern}'"