Skip to content

Conversation

furkanonder
Copy link
Contributor

@furkanonder furkanonder commented Aug 17, 2025

@furkanonder furkanonder added tests Tests in the Lib/test dir skip news 3.15 new features, bugs and security fixes labels Aug 17, 2025
@furkanonder furkanonder requested a review from picnixz August 17, 2025 14:51
@furkanonder furkanonder removed the 3.15 new features, bugs and security fixes label Aug 18, 2025
@furkanonder
Copy link
Contributor Author

@picnixz

I also added a new test case for wrong types from custom serializers. For example when serializers return None (Only bytes and strings are supported as values), dbm fails. The new test ensure proper TypeError/dbm.error exceptions are raised.

@furkanonder furkanonder requested a review from picnixz August 18, 2025 23:09
self.addCleanup(os_helper.rmtree, self.dirname)

def serializer(obj, protocol=None):
return ["value with invalid type"]
Copy link
Member

@picnixz picnixz Sep 17, 2025

Choose a reason for hiding this comment

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

If you want to test with None and with something that is not None, I would suggest using a parametrized test:

@support.subTests("serialized", [None, ["invalid type"]])
def test_custom_invalid_serializer(self, serialized):
    ...
    def serializer(obj, protocol=None):
        return serialized
    ...

and update the comment saying that the TypeError is due to the return
value of the serializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I've implemented the parameterized test approach as you recommended. I explored two different implementations and wanted to share the trade-offs:

Option 1: Using @subTests decorator (as you suggested)

@subTests("serialized", [None, ["invalid type"]])
def test_custom_invalid_serializer(self, serialized):
    # Create unique directory for each subtest to avoid conflicts
    test_dir = f"{self.dirname}_{id(serialized)}"
    os.mkdir(test_dir)
    self.addCleanup(os_helper.rmtree, test_dir)
    test_fn = os.path.join(test_dir, "shelftemp.db")
    
    def serializer(obj, protocol=None):
        return serialized
    # ... rest of test logic

Option 2: Using self.subTest() with a simple loop

def test_custom_invalid_serializer(self):
    os.mkdir(self.dirname)
    self.addCleanup(os_helper.rmtree, self.dirname)
    
    for serialized in [None, ["invalid type"]]:
        with self.subTest(serialized=serialized):
            def serializer(obj, protocol=None):
                return serialized
            # ... rest of test logic

Trade-offs:

  • Option 1: More explicit parameterization and complete isolation, but requires complex directory management
  • Option 2: Simpler, more readable, and uses standard unittest patterns, but subtests share the same directory

Which approach do you prefer?

Copy link
Member

Choose a reason for hiding this comment

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

Use 1, it was meant for such cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants