Skip to content

Add type annotations - #33

Open
stephenfin wants to merge 5 commits into
testing-cabal:masterfrom
stephenfin:typing
Open

stephenfin wants to merge 5 commits into
testing-cabal:masterfrom
stephenfin:typing

Conversation

@stephenfin

@stephenfin stephenfin commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

This looks more complicated that it is, but the commit-by-commit view should help with review. I can split this up into multiple PRs if needed.

Commits

  • Support debug mode in OptimisingTestSuite.run
  • Restrict suite unwrapping to unittest.TestSuite
  • Add mypy
  • Add types to package and enable strict mode
  • Make TestResourceManager a generic

@jelmer jelmer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The new debug=True branch in OptimisingTestSuite.run isn't explained that I can see. If the goal is to match unittest.TestSuite.run's signature, it is worth saying so in the commit message and the NEWS entry - and if it isn't, I'd drop it from this PR and propose it on its own, since it ships two new behaviours (calling test.debug(), propagating exceptions) without a motivation.

  • Please add a note to NEWS that addTest now rejects non-unittest.TestSuite iterables; it's a visible behaviour change even if the new signature makes it defensible.

  • test_none_resource_can_be_shared_reset_and_released reaches into _resource and _getCurrentResource and sequences a lot of state changes in one test. Can it be tightened to just the observable behaviour (dirty/reset/finishedWith lifecycle with None), without peeking at the box?

Comment thread NEWS Resolved
Comment thread testresources/__init__.py Resolved
@stephenfin
stephenfin marked this pull request as draft December 4, 2026 10:38
@stephenfin

Copy link
Copy Markdown
Contributor Author

Marking as draft while I work on this. I've split the Python changes out into #34 since they don't depend on this.

@stephenfin

stephenfin commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • The new debug=True branch in OptimisingTestSuite.run isn't explained that I can see. If the goal is to match unittest.TestSuite.run's signature, it is worth saying so in the commit message and the NEWS entry - and if it isn't, I'd drop it from this PR and propose it on its own, since it ships two new behaviours (calling test.debug(), propagating exceptions) without a motivation.

Yeah, this was a bug (IMO) since anyone calling debug() on OptimisingTestSuite would see a TypeError. I've now split this out to a separate commit. Let me know if you'd like a separate PR instead.

  • Please add a note to NEWS that addTest now rejects non-unittest.TestSuite iterables; it's a visible behaviour change even if the new signature makes it defensible.

Agreed. Done.

  • test_none_resource_can_be_shared_reset_and_released reaches into _resource and _getCurrentResource and sequences a lot of state changes in one test. Can it be tightened to just the observable behaviour (dirty/reset/finishedWith lifecycle with None), without peeking at the box?

Yeah, I wasn't happy with this but wanted something to ensure my changes from _currentResource -> _resource didn't result in None getting ignored. I've simplified this to only test the public API. I've also switched to camel case (which I don't like, but it's consistent so... 🤷 )

@stephenfin
stephenfin marked this pull request as ready for review December 4, 2026 11:24
@stephenfin

Copy link
Copy Markdown
Contributor Author

btw, do we want to enable issues here and disable the projects and wiki features? Asking here since I don't have another forum to do so.

'unittest.TestSuite.run()' provides a 'debug' flag [1], which can be
passed explicitly or implicitly by calling 'unittest.TestSuite.debug()'
[2]. When passed, it calls 'debug()' on the 'TestCase' rather than
'__call__()' [3].

We didn't support this parameter in 'OptimisingTestSuite.run', meaning
anyone calling the inherited 'debug()' method would see a TypeError.
Fix this and accept the 'debug' argument. Copy the behavior of the
parent class and call 'test.debug()' when it is enabled. This lets test
exceptions propagate to the caller instead of being collected in the
result, while leaving normal test execution unchanged.

[1] https://github.com/python/cpython/blob/v3.11.0/Lib/unittest/suite.py#L102
[2] https://github.com/python/cpython/blob/v3.11.0/Lib/unittest/suite.py#L135
[3] https://github.com/python/cpython/blob/v3.11.0/Lib/unittest/suite.py#L121-L124

Signed-off-by: Stephen Finucane <stephen@that.guru>
'OptimisingTestSuite.addTest()' currently treats any iterable as a
suite but this isn't a great signal: not only can it cause us to mistake
a test case for a test suite if it happens to implement iteration but
it's also very difficult to type check.

Instead of the try-except on 'iter', use an 'isinstance' check against
'unittest.TestSuite' and pass everything else through to the parent
'addTest()' method. This has potential runtime impacts if someone is
passing iterable suite-like objects that do not inherit from
'unittest.TestSuite', but that seems unlikely and callable objects
accepted by the base 'addTest()' can still be added as individual tests.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Signed-off-by: Stephen Finucane <stephen@that.guru>
This is a first pass that simply gets mypy passing in strict mode. There
is a lot of room for improvement here, particularly around describing
the types returned by TestResourceManager methods (currently typed as
'object') but we will tackle those separately, as follow-ups.

Signed-off-by: Stephen Finucane <stephen@that.guru>
This allows users to be a lot more expressive in their tests, should
they so wish.

Signed-off-by: Stephen Finucane <stephen@that.guru>
@stephenfin stephenfin changed the title Add type annotations, drop Python 3.10, and add Python 3.15 support Add type annotations Dec 4, 2026
@jelmer

jelmer commented Dec 4, 2026

Copy link
Copy Markdown
Member

btw, do we want to enable issues here and disable the projects and wiki features? Asking here since I don't have another forum to do so.

Yeah, that would be good. We'll need a project admin for that though, which I am not. @rbtcollins

This branch has not been deployed

No deployments
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