{"record":{"id":"d199c507493fb2c0","repo":"pytest-dev/pytest","slug":"pytester-makefile-expects-a-file-extension-try","errorCode":null,"errorMessage":"pytester.makefile expects a file extension, try .{ext} instead of {ext}","messagePattern":"pytester\\.makefile expects a file extension, try \\.(.+?) instead of (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/pytester.py","lineNumber":772,"sourceCode":"\n        This is done automatically upon instantiation.\n        \"\"\"\n        self._monkeypatch.chdir(self.path)\n\n    def _makefile(\n        self,\n        ext: str,\n        lines: Sequence[Any | bytes],\n        files: Mapping[str, _FileContent],\n        encoding: str = \"utf-8\",\n    ) -> Path:\n        items = list(files.items())\n\n        if ext is None:\n            raise TypeError(\"ext must not be None\")\n\n        if ext and not ext.startswith(\".\"):\n            raise ValueError(\n                f\"pytester.makefile expects a file extension, try .{ext} instead of {ext}\"\n            )\n\n        def to_text(s: Any | bytes) -> str:\n            return s.decode(encoding) if isinstance(s, bytes) else str(s)\n\n        if lines:\n            source = \"\\n\".join(to_text(x) for x in lines)\n            basename = self._name\n            items.insert(0, (basename, source))\n\n        ret = None\n        for basename, value in items:\n            p = self.path.joinpath(basename).with_suffix(ext)\n            p.parent.mkdir(parents=True, exist_ok=True)\n            source_ = Source(value)\n            source = \"\\n\".join(to_text(line) for line in source_.lines)\n            p.write_text(source.strip(), encoding=encoding)","sourceCodeStart":754,"sourceCodeEnd":790,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pytester.py#L754-L790","documentation":"Thrown by Pytester._makefile() when ext is a non-empty string that does not start with '.'. pytester.makefile() requires the leading dot to avoid silently creating files like 'testpy' instead of 'test.py'.","triggerScenarios":"Calling pytester.makefile('py', 'content') instead of makefile('.py', ...); passing an extension obtained from Path.suffix (which omits the dot for extensionless files) or from a config value that strips the dot.","commonSituations":"Muscle memory from other APIs that want 'py' not '.py'; reading extensions from config files that store them without the dot; refactoring where makepyfile() was replaced by the generic makefile().","solutions":["Prefix the extension with a dot: pass '.py' not 'py'.","Use makepyfile()/maketxtfile()/makeini() shortcuts which handle the dot for you.","Normalize the extension: ext = '.' + ext.lstrip('.') before calling."],"exampleFix":"// before\npytester.makefile(\"py\", test_foo=\"assert True\")\n// after\npytester.makefile(\".py\", test_foo=\"assert True\")\n// or\npytester.makepyfile(test_foo=\"assert True\")","handlingStrategy":"validation","validationCode":"if ext and not ext.startswith('.'):\n    ext = '.' + ext\npytester.makefile(ext, content)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass extensions with a leading dot.","Prefer makepyfile()/makeini()/maketxtfile() shortcuts.","Normalize: ext = '.' + ext.lstrip('.') in helpers."],"tags":["pytester","makefile","extension","validation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}