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

Code before:
            )
        self.__setitem__(ds_name, dataset)

    def filter(
        self,
        name_regex: str | None = None,
        name_regex_flags: int | re.RegexFlag = re.IGNORECASE,
        type_regex: str | None = None,
        type_regex_flags: int | re.RegexFlag = 0,
        by_type: type | list[type] | None = None,
    ) -> List[str]:  # noqa: UP006
        """Filter dataset names registered in the catalog based on name and/or type.

        This method allows filtering datasets by their names and/or types using optional
        regular expression patterns. Each pattern can also support optional regex flags
        for customization. If no filters are provided, all dataset names are returned.

        Args:
            name_regex: Optional regular expression to filter dataset names by name.
            name_regex_flags: Optional regex flags for the name filter.
                By default, IGNORECASE key is set.
            type_regex: Optional regular expression to filter dataset names by their type.
                The provided regex is matched against the full dataset type path, for example:
                `kedro_datasets.pandas.parquet_dataset.ParquetDataset`.
            type_regex_flags: Optional regex flags for the type filter.
            by_type: Optional dataset types to filter by.

        Returns:
            A list of dataset names that match the filtering criteria based on `name_regex`
            and/or `type_regex` and/or types provided via `by_type`.
            If no filters are provided, all dataset names are returned.

        Raises:
            SyntaxError: If the provided regex patterns are invalid.

        Example:
        ::

            >>> catalog = KedroDataCatalog()
            >>> # get datasets where the substring 'raw' is present
            >>> raw_data = catalog.filter(name_regex='raw')
            >>> # get datasets of a specific type
            >>> csv_datasets = catalog.filter(type_regex='pandas.excel_dataset.ExcelDataset')
            >>> # get datasets where names start with 'model_' and are of a specific type
            >>> model_datasets = catalog.filter(
            ...     name_regex='^model_',
            ...     type_regex='ModelDataset',
            ... )
            >>> # get datasets where names include 'data' and are of a specific types
            >>> from kedro_datasets.pandas import SQLQueryDataset
            >>> from kedro.io import MemoryDataset

Review comment (suggestion):
I think it would be helpful to give an example of a `by_type` argument. And maybe clarify that this is an instance of type of check instead of regex match? And also clarify it can take a single type or list. I know people can also read that from the function signature, but better to be extra clear IMO.

Code after:
            )
        self.__setitem__(ds_name, dataset)

    def filter(
        self,
        name_regex: re.Pattern[str] | str | None = None,
        type_regex: re.Pattern[str] | str | None = None,
        by_type: type | list[type] | None = None,
    ) -> List[str]:  # noqa: UP006
        """Filter dataset names registered in the catalog based on name and/or type.

        This method allows filtering datasets by their names and/or types. Regular expressions
        should be precompiled before passing them to `name_regex` or `type_regex`, but plain
        strings are also supported.

        Args:
            name_regex: Optional compiled regex pattern or string to filter dataset names.
            type_regex: Optional compiled regex pattern or string to filter dataset types.
                The provided regex is matched against the full dataset type path, for example:
                `kedro_datasets.pandas.parquet_dataset.ParquetDataset`.
            by_type: Optional dataset type(s) to filter by. This performs an instance type check
                rather than a regex match. It can be a single dataset type or a list of types.

        Returns:
            A list of dataset names that match the filtering criteria.

        Example:
        ::

            >>> import re
            >>> catalog = KedroDataCatalog()
            >>> # get datasets where the substring 'raw' is present
            >>> raw_data = catalog.filter(name_regex='raw')
            >>> # get datasets where names start with 'model_' (precompiled regex)
            >>> model_datasets = catalog.filter(name_regex=re.compile('^model_'))
            >>> # get datasets of a specific type using type_regex
            >>> csv_datasets = catalog.filter(type_regex='pandas.excel_dataset.ExcelDataset')
            >>> # get datasets where names contain 'train' and type matches 'CSV' in the path
            >>> catalog.filter(name_regex="train", type_regex="CSV")
            >>> # get datasets where names include 'data' and are of a specific type
            >>> from kedro_datasets.pandas import SQLQueryDataset
            >>> catalog.filter(name_regex="data", by_type=SQLQueryDataset)
            >>> # get datasets where names include 'data' and are of multiple specific types
            >>> from kedro.io import MemoryDataset
            >>> catalog.filter(name_regex="data", by_type=[MemoryDataset, SQLQueryDataset])
        """
        filtered = self.keys()

        # Apply name filter if specified
        if name_regex:
            filtered = [