From c0f54134537c8653d21684f5e9e0b9ba44496312 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:25:07 +1000 Subject: [PATCH 1/7] fix missing type annotations on `makepyfile` and `maketxtfile` methods --- src/_pytest/pytester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 1cd5f05dd7e..4f13ef30f30 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -862,7 +862,7 @@ def makepyprojecttoml(self, source: str) -> Path: """ return self.makefile(".toml", pyproject=source) - def makepyfile(self, *args, **kwargs) -> Path: + def makepyfile(self, *args: str, **kwargs: str) -> Path: r"""Shortcut for .makefile() with a .py extension. Defaults to the test name with a '.py' extension, e.g test_foobar.py, overwriting @@ -882,7 +882,7 @@ def test_something(pytester): """ return self._makefile(".py", args, kwargs) - def maketxtfile(self, *args, **kwargs) -> Path: + def maketxtfile(self, *args: str, **kwargs: str) -> Path: r"""Shortcut for .makefile() with a .txt extension. Defaults to the test name with a '.txt' extension, e.g test_foobar.txt, overwriting From a30c8a13b0729935b61f88f1620893819ece90d5 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:28:53 +1000 Subject: [PATCH 2/7] add changelog file --- changelog/14080.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/14080.bugfix.rst diff --git a/changelog/14080.bugfix.rst b/changelog/14080.bugfix.rst new file mode 100644 index 00000000000..1140b4716db --- /dev/null +++ b/changelog/14080.bugfix.rst @@ -0,0 +1 @@ +fix missing type annotations on ``Pytester.makepyfile`` and ``Pytester.maketxtfile`` methods. From fbf6dc2f4ce7871f471c910ccd9b60999db40536 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:32:17 +1000 Subject: [PATCH 3/7] support `bytes` in `makepyfile` and `maketxtfile` methods --- src/_pytest/pytester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 4f13ef30f30..048a30fa419 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -862,7 +862,7 @@ def makepyprojecttoml(self, source: str) -> Path: """ return self.makefile(".toml", pyproject=source) - def makepyfile(self, *args: str, **kwargs: str) -> Path: + def makepyfile(self, *args: str | bytes, **kwargs: str | bytes) -> Path: r"""Shortcut for .makefile() with a .py extension. Defaults to the test name with a '.py' extension, e.g test_foobar.py, overwriting @@ -882,7 +882,7 @@ def test_something(pytester): """ return self._makefile(".py", args, kwargs) - def maketxtfile(self, *args: str, **kwargs: str) -> Path: + def maketxtfile(self, *args: str | bytes, **kwargs: str | bytes) -> Path: r"""Shortcut for .makefile() with a .txt extension. Defaults to the test name with a '.txt' extension, e.g test_foobar.txt, overwriting From 26edeeff8efe8f4a0fe2bc6f2db1af93ac5dac4e Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:45:04 +1000 Subject: [PATCH 4/7] fix type error when calling `_makepyfile` with `bytes` --- src/_pytest/pytester.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 048a30fa419..4b1f42986c2 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -756,7 +756,7 @@ def _makefile( self, ext: str, lines: Sequence[Any | bytes], - files: dict[str, str], + files: dict[str, str | bytes], encoding: str = "utf-8", ) -> Path: items = list(files.items()) From 724831fe02897b06354805d8987fc121db125ae1 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:48:46 +1000 Subject: [PATCH 5/7] fix variance error --- src/_pytest/pytester.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 4b1f42986c2..2c87e2caf29 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -10,6 +10,7 @@ from collections.abc import Callable from collections.abc import Generator from collections.abc import Iterable +from collections.abc import Mapping from collections.abc import Sequence import contextlib from fnmatch import fnmatch @@ -756,7 +757,7 @@ def _makefile( self, ext: str, lines: Sequence[Any | bytes], - files: dict[str, str | bytes], + files: Mapping[str, str | bytes], encoding: str = "utf-8", ) -> Path: items = list(files.items()) From 607f911ebe161bc55ef065eb9545347b0f71a5d9 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:53:16 +1000 Subject: [PATCH 6/7] support `list` and `tuple`s which get joined by newlines --- src/_pytest/pytester.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 2c87e2caf29..a2292e2bf11 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -645,6 +645,7 @@ def __init__(self) -> None: def restore(self) -> None: sys.path[:], sys.meta_path[:] = self.__saved +_FileContent = tuple[str | bytes, ...] | list[str | bytes] | str | bytes @final class Pytester: @@ -757,7 +758,7 @@ def _makefile( self, ext: str, lines: Sequence[Any | bytes], - files: Mapping[str, str | bytes], + files: Mapping[str, _FileContent], encoding: str = "utf-8", ) -> Path: items = list(files.items()) @@ -863,7 +864,7 @@ def makepyprojecttoml(self, source: str) -> Path: """ return self.makefile(".toml", pyproject=source) - def makepyfile(self, *args: str | bytes, **kwargs: str | bytes) -> Path: + def makepyfile(self, *args: _FileContent, **kwargs: _FileContent) -> Path: r"""Shortcut for .makefile() with a .py extension. Defaults to the test name with a '.py' extension, e.g test_foobar.py, overwriting @@ -883,7 +884,7 @@ def test_something(pytester): """ return self._makefile(".py", args, kwargs) - def maketxtfile(self, *args: str | bytes, **kwargs: str | bytes) -> Path: + def maketxtfile(self, *args: _FileContent, **kwargs: _FileContent) -> Path: r"""Shortcut for .makefile() with a .txt extension. Defaults to the test name with a '.txt' extension, e.g test_foobar.txt, overwriting From a8952995d4956ae464986ed508b527524405e1b2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 04:53:35 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/pytester.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index a2292e2bf11..2313b45ce02 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -645,8 +645,10 @@ def __init__(self) -> None: def restore(self) -> None: sys.path[:], sys.meta_path[:] = self.__saved + _FileContent = tuple[str | bytes, ...] | list[str | bytes] | str | bytes + @final class Pytester: """