def test_merge_dictionary(): target = { "existing_dict": { "foo": "bar", "hello": "world" }, "existing_list": ["hello"], } input_dict = { "existing_dict": { "foo": "baz" }, "existing_list": ["world"], "new_dict": { "name": "Sam" }, } cli_utils.merge_dictionary(target, input_dict) assert target == { "existing_dict": { "foo": "baz", "hello": "world" }, "existing_list": ["hello", "world"], "new_dict": { "name": "Sam" }, }
def do_import( project: Project, filename: str, format: str | None = None, options: Namespace | None = None, ) -> None: """Import project metadata from given file. :param project: the project instance :param filename: the file name :param format: the file format, or guess if not given. :param options: other options parsed to the CLI. """ if not format: for key in FORMATS: if FORMATS[key].check_fingerprint(project, filename): break else: raise PdmUsageError( "Can't derive the file format automatically, " "please specify it via '-f/--format' option." ) else: key = format if options is None: options = Namespace(dev=False, group=None) project_data, settings = FORMATS[key].convert(project, filename, options) pyproject = project.pyproject or tomlkit.document() if "tool" not in pyproject or "pdm" not in pyproject["tool"]: # type: ignore pyproject.setdefault("tool", {})["pdm"] = tomlkit.table() if "project" not in pyproject: pyproject.add("project", tomlkit.table()) # type: ignore pyproject["project"].add( # type: ignore tomlkit.comment("PEP 621 project metadata") ) pyproject["project"].add( # type: ignore tomlkit.comment("See https://www.python.org/dev/peps/pep-0621/") ) merge_dictionary(pyproject["project"], project_data) # type: ignore merge_dictionary(pyproject["tool"]["pdm"], settings) # type: ignore pyproject["build-system"] = { "requires": ["pdm-pep517"], "build-backend": "pdm.pep517.api", } project.pyproject = cast(dict, pyproject) project.write_pyproject()