def _resolve_dependencies(
        self, requested_groups: list[str] | None = None, include_referred: bool = True
    ) -> dict[str, list[Requirement]]:
        """Resolve dependencies for the given groups, and return a list of requirements for each group.

        The .groups attribute will be set to all that refers this requirement directly or indirectly.
        If `include_referred` is True, all self-references and `include-group` will be expanded to
        corresponding requirements. Otherwise, each group only contains explicitly defined requirements.
        """

        def _get_dependencies(group: str) -> tuple[list[Requirement], set[str]]:
            in_metadata = group in metadata_dependencies
            collected_deps: list[str] = []
            referred: set[str] = set()
            deps = metadata_dependencies.get(group, []) if in_metadata else dev_dependencies[group]
            for item in deps:
                if isinstance(item, str):
                    try:
                        name, extras = strip_extras(item)
                    except AssertionError:
                        pass
                    else:
                        if normalize_name(name) == project_name:
                            if extras:
                                allowed = (
                                    set(metadata_dependencies)
                                    if in_metadata
                                    else {*metadata_dependencies, *dev_dependencies}
                                )
                                extras = tuple(normalize_name(extra) for extra in extras)
                                not_allowed = set(extras) - allowed
                                if not_allowed:
                                    raise ProjectError(
                                        f"Optional dependency group '{group}' cannot "
                                        f"include non-existing extras: [{','.join(not_allowed)}]"
                                    )
                                referred.update(extras)
                            continue
                    collected_deps.append(item)
                elif not in_metadata and isinstance(item, dict):
                    if tuple(item.keys()) != ("include-group",):
                        raise ProjectError(f"Invalid dependency group item: {item}")
                    include_group = normalize_name(item["include-group"])
                    if include_group not in dev_dependencies:
                        raise ProjectError(f"Missing group '{include_group}' in `include-group`")
                    referred.add(include_group)
                else:
                    raise ProjectError(f"Invalid dependency in group {group}: {item}")
            result: list[Requirement] = []
            with cd(self.root):
                for line in collected_deps:
                    if line.startswith("-e ") and in_metadata:
                        self.core.ui.warn(
                            f"Skipping editable dependency [b]{line}[/] in the"
                            r" [success]\[project][/] table. Please move it to the "
                            r"[success]\[tool.pdm.dev-dependencies][/] table"
                        )
                        continue
                    req = parse_line(line)
                    req.groups = [group]
                    # make editable packages behind normal ones to override correctly.
                    result.append(req)
            return result, referred

        if requested_groups is None:
            requested_groups = list(self.iter_groups())
        requested_groups = [normalize_name(g) for g in requested_groups]
        referred_groups: dict[str, set[str]] = {}
        metadata_dependencies = {
            normalize_name(k): v for k, v in self.pyproject.metadata.get("optional-dependencies", {}).items()
        }
        metadata_dependencies["default"] = self.pyproject.metadata.get("dependencies", [])
        dev_dependencies = self.pyproject.dev_dependencies
        group_deps: dict[str, list[Requirement]] = {}
        project_name = normalize_name(self.name) if self.name else None
        for group in requested_groups:
            deps, referred = _get_dependencies(group)
            group_deps[group] = deps
            if referred:
                referred_groups[group] = referred
        extra_deps: dict[str, list[Requirement]] = {}
        while referred_groups:
            updated = False
            ref_iter = list(referred_groups.items())
            for group, referred in ref_iter:
                for ref in list(referred):
                    if ref not in requested_groups:
                        deps, r = _get_dependencies(ref)
                        group_deps[ref] = deps
                        if r:
                            referred_groups[ref] = r
                            # append to the ref_iter to process later
                            ref_iter.append((ref, r))
                        requested_groups.append(ref)
                    if ref in referred_groups:  # not resolved yet
                        continue
                    extra_deps.setdefault(group, []).extend(group_deps[ref])
                    for req in itertools.chain(group_deps[ref], extra_deps.get(ref, [])):
                        if group not in req.groups:
                            req.groups.append(group)
                    referred.remove(ref)
                    updated = True
                if not referred:
                    referred_groups.pop(group)
            if not updated:
                raise ProjectError(f"Cyclic dependency group include detected: {set(referred_groups)}")
        if include_referred:
            for group, deps in extra_deps.items():
                group_deps[group].extend(deps)
        return group_deps
Resolve dependencies for the given groups, and return a list of requirements for each group.

        The .groups attribute will be set to all that refers this requirement directly or indirectly.
        If `include_referred` is True, all self-references and `include-group` will be expanded to
        corresponding requirements. Otherwise, each group only contains explicitly defined requirements.
        
src/pdm/project/core.py