#๐Ÿ”’ unittest.subTest eats exceptions, oh my!

11 messages ยท Page 1 of 1 (latest)

worldly rock
#

This is mostly for @half field benefit, my hard to debug unit test of my pmap() function (parallel map(), kin to my amap() function, concurrent async map).

stable muralBOT
#

@worldly rock

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

worldly rock
#

!paste

stable muralBOT
#
Pasting large amounts of code

So that everyone can easily read your code, you can paste it in this website:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

worldly rock
#

I'm trying to verify the behaviour when with_exceptions=False i.e. an exception in the map function is let out to the caller/consumer.

Core is this inner function:

    def test_pmap(n_modes):
      n, modes = n_modes
      ##with self.subTest(n=n, **modes):
      if True:
        print("pmap", n, modes)
        results = list(pmap(self.sleep_n, self.gen(n), **modes))
        for res in results:
          print("  pmap ->", res)
        self.assertEqual(len(results), n)
        return n, modes

which had the self.subTest context manager uncommented.

This is consumed later in this loop at the bottom of the test:

      if fast:
        tested = pmap(test_pmap, test_modes)
      else:
        tested = map(test_pmap, test_modes)
      ##for n, modes in tested:
      print(f'{tested=}')
      retval = "BOGUS"
      for retval in tested:
        print(f'{retval=}')
        if retval is None:
          breakpoint()
        else:
          n, modes = retval
          print(f'{fast=} OK', n, modes)
#

That for retval in tested was formerly:

for n, modes in tested:

and I was getting None from tested. You can see the test_pmap function above has an unconditional return which cannot return None.

#

It turns out that the subTest context manager was catching the exception and quietly falling off the end of the function because the exception prevented Python getting to the final return.
And the implied return None at the end of every python function did just that.

#

@half field ^^ this is what was going on.

I was surprised, but it is correct behaviour.

Going to move the final return to outside the subTest part, which was there really to anntoate failures.

stable muralBOT
#

@worldly rock

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.