Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedJan 10, 2025
edited by hugovk
Loading

Add the following methods:

  • assertHasAttr() and assertNotHasAttr()
  • assertIsSubclass() and assertNotIsSubclass()
  • assertStartsWith() and assertNotStartsWith()
  • assertEndsWith() and assertNotEndsWith()

Also improve error messages for assertIsInstance() and assertNotIsInstance().


📚 Documentation preview 📚:https://cpython-previews--128707.org.readthedocs.build/

Add the following methods:* assertHasAttr() and assertNotHasAttr()* assertIsSubclass() and assertNotIsSubclass()* assertStartswith() and assertNotStartswith()* assertEndswith() and assertNotEndswith()Also improve error messages for assertIsInstance() andassertNotIsInstance().
@hugovk
Copy link
Member

Great!

  • assertStartsWith() and assertEndsWith() has been renamed to assertStartswith() and assertEndswith().

I recommendassertStartsWith() andassertEndsWith(), both for readability, and for accessibility -- screen readers will have a better chance with the extra capitals.

It doesn't really matter thatstartswith is all lower case, because we're creating a new method name.

(And we already haveassertIsInstance, which checksisinstance, and notassertIsinstance.)

JelleZijlstra reacted with thumbs up emoji

@serhiy-storchaka
Copy link
MemberAuthor

I wrote them initially asassertStartsWith() andassertEndsWith(), but renamed toassertStartswith() andassertEndswith() as the result of the discussion on the issue.

@serhiy-storchaka
Copy link
MemberAuthor

I created a poll:https://discuss.python.org/t/assertstartwith-vs-assertstartwith/76701.

hugovk reacted with thumbs up emoji

Copy link
Member

@JelleZijlstraJelleZijlstra left a 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.

@cjw296
Copy link
Contributor

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 theUnitTest base class where possible now.

@cjw296cjw296 removed their request for reviewJanuary 11, 2025 18:21
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Copy link
Member

@vstinnervstinner left a 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.

@@ -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)
Copy link
Member

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 :-)

Copy link
MemberAuthor

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...

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)
Copy link
Member

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.

Copy link
Member

@picnixzpicnixz left a 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.

@serhiy-storchakaserhiy-storchaka merged commit06cad77 intopython:mainJan 14, 2025
38 checks passed
@serhiy-storchakaserhiy-storchaka deleted the extra-assertions branchJanuary 14, 2025 08:02
@vstinner
Copy link
Member

Nice additions, thanks@serhiy-storchaka.

AlexWaygood and hugovk reacted with thumbs up emojihugovk reacted with rocket emoji

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestJan 14, 2025
…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)
@bedevere-app
Copy link

GH-128815 is a backport of this pull request to the3.13 branch.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestJan 14, 2025
…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)
serhiy-storchaka added a commit that referenced this pull requestJan 20, 2025
…-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 commit06cad77)
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestJan 20, 2025
…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)
serhiy-storchaka added a commit that referenced this pull requestJan 20, 2025
…-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)
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JelleZijlstraJelleZijlstraJelleZijlstra left review comments

@vstinnervstinnervstinner approved these changes

@jaracojaracojaraco approved these changes

@hugovkhugovkhugovk approved these changes

@picnixzpicnixzpicnixz approved these changes

@AlexWaygoodAlexWaygoodAwaiting requested review from AlexWaygoodAlexWaygood is a code owner

@warsawwarsawAwaiting requested review from warsawwarsaw is a code owner

@FFY00FFY00Awaiting requested review from FFY00FFY00 is a code owner

@vsajipvsajipAwaiting requested review from vsajipvsajip is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

7 participants
@serhiy-storchaka@hugovk@cjw296@vstinner@jaraco@JelleZijlstra@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp