Skip to content

Simplify TestProgram, TestToolsTestRunner#597

Merged
jelmer merged 7 commits intotesting-cabal:masterfrom
stephenfin:simplify-runner
Mar 26, 2026
Merged

Simplify TestProgram, TestToolsTestRunner#597
jelmer merged 7 commits intotesting-cabal:masterfrom
stephenfin:simplify-runner

Conversation

@stephenfin
Copy link
Copy Markdown
Contributor

We had notes indicating that TestProgram was vendored from upstream and should not be necessary. Turns out this isn't really true, and our TestProgram subclass 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.

  • trivial: Remove vendoring notes on TestProgram
  • Remove unnecessary use of unicode_output_stream
  • Remove unnecessary type annotations
  • Remove unnecessary testRunner argument
  • Use open context manager
  • Simplify TestProgram._get_runner
  • Simplify TestProgram._get_runner further

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>
@jelmer jelmer merged commit 208d2db into testing-cabal:master Mar 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants