Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-71339: Add additional assertion methods for unittest#128707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Add the following methods:* assertHasAttr() and assertNotHasAttr()* assertIsSubclass() and assertNotIsSubclass()* assertStartswith() and assertNotStartswith()* assertEndswith() and assertNotEndswith()Also improve error messages for assertIsInstance() andassertNotIsInstance().
Great!
I recommend It doesn't really matter that (And we already have |
I wrote them initially as |
I created a poll:https://discuss.python.org/t/assertstartwith-vs-assertstartwith/76701. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks good but I agree with Hugo that the name should be assertStartsWith.
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>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM. Just a few minor coding style remarks.
Lib/unittest/case.py Outdated
@@ -1321,15 +1321,77 @@ def assertIsInstance(self, obj, cls, msg=None): | |||
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer | |||
default message.""" | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You may use f-strings :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Actually,'%s is not an instance of any of %r' % (safe_repr(obj), cls)
andf'{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...
Lib/unittest/case.py Outdated
standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls) | ||
self.fail(self._formatMessage(msg, standardMsg)) | ||
def assertIsSubclass(self, cls, superclass, msg=None): | ||
try: | ||
r = issubclass(cls, superclass) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You may avoid variables of a single letter: use "res" or "result". Same remark for new functions below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Just some docs nits and LGTM. I'm very happy to haveassertHasAttr
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.
06cad77
intopython:mainUh oh!
There was an error while loading.Please reload this page.
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 commit06cad77)
GH-128815 is a backport of this pull request to the3.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 commit06cad77)
…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 commitc6a566e)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>(cherry picked from commit06cad77)
…-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 commitc6a566e)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>(cherry picked from commit06cad77)
Uh oh!
There was an error while loading.Please reload this page.
Add the following methods:
Also improve error messages for assertIsInstance() and assertNotIsInstance().
📚 Documentation preview 📚:https://cpython-previews--128707.org.readthedocs.build/