pytest-dev/pytest · error · TypeError
ext must not be None
Error message
ext must not be None
What it means
Raised by Pytester._makefile (the backend of makefile/makepyfile) when the ext argument is None. makefile requires a concrete file extension string to construct the output filename via Path.with_suffix(ext), and None is not a valid suffix. This is a hard precondition check before any file is written.
Source
Thrown at src/_pytest/pytester.py:769
def chdir(self) -> None:
"""Cd into the temporary directory.
This is done automatically upon instantiation.
"""
self._monkeypatch.chdir(self.path)
def _makefile(
self,
ext: str,
lines: Sequence[Any | bytes],
files: Mapping[str, _FileContent],
encoding: str = "utf-8",
) -> Path:
items = list(files.items())
if ext is None:
raise TypeError("ext must not be None")
if ext and not ext.startswith("."):
raise ValueError(
f"pytester.makefile expects a file extension, try .{ext} instead of {ext}"
)
def to_text(s: Any | bytes) -> str:
return s.decode(encoding) if isinstance(s, bytes) else str(s)
if lines:
source = "\n".join(to_text(x) for x in lines)
basename = self._name
items.insert(0, (basename, source))
ret = None
for basename, value in items:
p = self.path.joinpath(basename).with_suffix(ext)
p.parent.mkdir(parents=True, exist_ok=True)View on GitHub (pinned to 98b357f69e)
Solutions
- Pass a concrete extension including the dot, e.g. '.py' or '.txt'.
- Default ext to '.py' in your own helper when the source value is None.
- Use makepyfile(...) instead of makefile when you always want Python files, since it supplies the extension for you.
Example fix
// before pytester.makefile(ext, foo=source) # ext may be None // after pytester.makefile(ext or ".py", foo=source)
Defensive patterns
Strategy: validation
Validate before calling
def safe_makefile(pytester, ext, **kw):
if ext is None:
ext = ".py"
return pytester.makefile(ext, **kw) Type guard
def is_valid_ext(ext: object) -> bool:
return isinstance(ext, str) Prevention
- Never pass None as ext; default to '.py' in helpers.
- Prefer makepyfile() for Python files.
When it happens
Trigger: Calling pytester.makefile(None, ...) explicitly, or a wrapper/helper that forwards a None ext, or calling makepyfile internals that default ext to None for a package-less layout.
Common situations: Programmatically building the ext argument from a config value that resolved to None; copy-pasting a makepyfile call pattern into a custom makefile invocation; a refactor that changed the default of a helper to None.
Related errors
- pytester.makefile expects a file extension, try .{ext} inste
- name is not allowed to contain path separators
- relative tolerance for a scalar value must be an int, float
- expected value must support abs(...) when relative tolerance
- absolute tolerance for a scalar value must be an int, float
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/ee011038c8a7e7a7.json.
Report an issue: GitHub.