Uh oh!
There was an error while loading. Please reload this page.
gh-71339: Add additional assertion methods for unittest - #128707
Conversation
Add the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartswith() and assertNotStartswith() * assertEndswith() and assertNotEndswith() Also improve error messages for assertIsInstance() and assertNotIsInstance().
hugovk
commented
Jan 10, 2025
Great!
I recommend It doesn't really matter that (And we already have |
serhiy-storchaka
commented
Jan 10, 2025
I wrote them initially as |
serhiy-storchaka
commented
Jan 10, 2025
I created a poll: https://discuss.python.org/t/assertstartwith-vs-assertstartwith/76701. |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Looks good but I agree with Hugo that the name should be assertStartsWith.
cjw296
commented
Jan 11, 2025
Looks like the poll is pretty unanimous :-) The changes look great, but I'll admit I'm unlikely to use them as I tend to avoid the |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
vstinner
left a comment
There was a problem hiding this comment.
LGTM. Just a few minor coding style remarks.
| if not isinstance(obj, cls): | ||
| standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls) | ||
| if isinstance(cls, tuple): | ||
| standardMsg = '%s is not an instance of any of %r' % (safe_repr(obj), cls) |
There was a problem hiding this comment.
Actually, '%s is not an instance of any of %r' % (safe_repr(obj), cls) and f'{safe_repr(obj)!s} is not an instance of any of {cls!r}' produce the same bytecode. So the difference is only in readability, which is at large part subjective. I was not sure that inlining expressions in f-strings would make the code more readable, but if you think so...
| def assertIsSubclass(self, cls, superclass, msg=None): | ||
| try: | ||
| r = issubclass(cls, superclass) |
There was a problem hiding this comment.
You may avoid variables of a single letter: use "res" or "result". Same remark for new functions below.
picnixz
left a comment
There was a problem hiding this comment.
Just some docs nits and LGTM. I'm very happy to have assertHasAttr because I needed it a lot in my personal projects.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
Jan 14, 2025
Nice additions, thanks @serhiy-storchaka. |
…honGH-128707) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
GH-128815 is a backport of this pull request to the 3.13 branch. |
…honGH-128707) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
…-128707) (GH-128815) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
…t.support (pythonGH-128707) (pythonGH-128815) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit c6a566e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit 06cad77)
…-128707) (GH-128815) (GH-129059) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit c6a566e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit 06cad77)
Add the following methods:
Also improve error messages for assertIsInstance() and assertNotIsInstance().
📚 Documentation preview 📚: https://cpython-previews--128707.org.readthedocs.build/