Simplify TestProgram, TestToolsTestRunner#597
Merged
jelmer merged 7 commits intotesting-cabal:masterfrom Mar 26, 2026
Merged
Conversation
We no longer care about Python < 2.7. Or Python == 2.7 for that matter. However, we do care about the additional functionality that our version of TestProgram exposes, none of which has been successfully upstreamed yet. This unfortunately means duplicating a lot of TestProgram's implementation due to python/cpython#67049. Reflect this reality in the code. Signed-off-by: Stephen Finucane <stephen@that.guru>
This function returns the provided argument as-is if it is either a TextIOWrapper or StringIO. On Python 3, those are the only two types the stdout can ever be. At runtime we will pass sys.stdout, which is a TextIOWrapper, and in our tests we will pass a StringIO. This call is therefore unnecessary and can be dropped and the function marked as deprecated since it has no other users. This change also highlights an issue with our types for TextTestResult. We had indicated that the `stream` parameter for same had to be a TextIO, which was considerably narrower than the IO[str] we have typed the `stdout` parameter of TestToolsTestRunner as being. It turns out TextTestResult is only using write() and flush(), both of which are defined on IO[str], thus we can broaden its type, removing a `type: ignore` in the process. Signed-off-by: Stephen Finucane <stephen@that.guru>
These all duplicate what is provided by the typeshed stubs or whta is inferred by our definitions in __init__. Remove them. Signed-off-by: Stephen Finucane <stephen@that.guru>
We were using `functools.partial` to pre-populate the `stdout` parameter of the test runner instance passed to `TestProgram`. However, the `_get_runner` method will be called if we do not pass a `testRunner` argument and *that* will attempt to populate the `stdout` property with `self.stdout`. Signed-off-by: Stephen Finucane <stephen@that.guru>
Signed-off-by: Stephen Finucane <stephen@that.guru>
This try-except was necessary to support an instance of a runner being passed that did not support the `tb_locals` parameter. This appears to be a compat handler for Python 2.x variants of `TextTestRunner`, which did not accept this parameter [1]. This is not the case of the lowest version of Python we currently support, 3.10 [2], meaning we can remove the wrapper. [1] https://github.com/python/cpython/blob/v2.7.1/Lib/unittest/runner.py#L127-L128 [2] https://github.com/python/cpython/blob/v3.10.0/Lib/unittest/runner.py#L128-L130 Signed-off-by: Stephen Finucane <stephen@that.guru>
This try-except was only needed if we provide a test runner (via the testRunner parameter) that was not an instance of TestToolsTestRunner. While upstream might need to support this [1], we do not. Remove the fallback. [1] https://github.com/python/cpython/blob/v3.10.0/Lib/unittest/main.py#L251-L270 Signed-off-by: Stephen Finucane <stephen@that.guru>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We had notes indicating that
TestProgramwas vendored from upstream and should not be necessary. Turns out this isn't really true, and ourTestProgramsubclass has a few extensions that never actually made their way upstream. Remove this note and update it to reflect reality, then go and clean up a few layers of accreted cruft that has built up here over the years and mostly not necessary in a Python 3-only world.@jelmer I expect most of this to be very uncontroversial except for the last one. I'm not sure if my assertion there regarding the types of test runner we accept is valid. I believe it is, but I can drop this commit if you/others think otherwise.