Skip to content

Use assertIsInstance and assertNotIsInstance unittest methods #2700

Closed

Description

@peterjc

We have now dropped Python 2 support, which means we can take advantage of many of the unittest framework methods where were not present on Python 2.

This issue is specifically for using assertIsInstance and assertNotIsInstance, both new in Python 3.2:

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIsInstance
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertNotIsInstance

These grep searches find lots of candidates to update, but will miss any split over multiple lines.:

$ grep assertTrue *.py | grep "isinstance" | head
common_BioSQL.py:        self.assertTrue(isinstance(test_record.seq, BioSeq.DBSeq))
common_BioSQL.py:            self.assertTrue(isinstance(feature, SeqFeature))
...
$ grep assertFalse *.py | grep "isinstance"
common_BioSQL.py:        self.assertFalse(isinstance(tripled, BioSeq.DBSeq))
common_BioSQL.py:        self.assertFalse(isinstance(tripled, BioSeq.DBSeq))
common_BioSQL.py:        self.assertFalse(isinstance(tripled, BioSeq.DBSeq))

There are so many that it would be worthwhile trying to automate this?

Cross reference #2698 for assertIn and assertNotIn, and #2699 for assertIs and assertIsNot.

Activity

  1. added
    good first issue
    on Feb 12, 2020
  2. peterjc commented on Mar 3, 2020

    @peterjc
    MemberAuthor

    Almost all of the above were fixed with #2776,

    $ grep assertTrue *.py | grep "isinstance" | head
    test_PDB_KDTree.py:            self.assertTrue(isinstance(hits, list), hits)
    

    Probably can be just self.assertIsInstance(hits, list) as the default failure message is pretty good?

    Also I found a few more with a different search:

    $ grep assert *.py | grep "isinstance"
    search_tests_common.py:            assert isinstance(val_b, dict)
    seq_tests_common.py:        assert isinstance(old_r.location[0], FeatureLocation)
    ...
    
  3. deekerno commented on Mar 3, 2020

    @deekerno
    Contributor

    Oops, I thought I fixed test_PDB_KDTree. My apologies. I can do that one as well as fixing the other ones in that other search that you mentioned.

  4. peterjc commented on Mar 3, 2020

    @peterjc
    MemberAuthor

    That'd be great - thanks for tackling this rather tedious bit of code cleanup 🥇

  5. MarkusPiotrowski commented on Mar 3, 2020

    @MarkusPiotrowski
    Contributor

    There is a flake8 plugin called flake8-assertive which list a number of assert... issues in our tests folder:

    281   A500 prefer assertLessEqual() for '<=' comparisons
    68    A501 prefer assertIn() for 'in' expressions
    422   A502 prefer assertFalse() instead of comparing to False
    771
    

    The error codes include several similar issues each, e.g. A500 can also be 'prefer assert Equal() for '==' comparisons' etc. etc.

  6. deekerno commented on Mar 3, 2020

    @deekerno
    Contributor

    Several tests in both search_tests_common.py and seq_tests_common.py are not subclasses of unittest.TestCase, and would need to be restructured to leverage the unittest methods. As it's common code for a number of tests, I'm not comfortable making large changes to those modules given my recency to the project. Is there any guidance on how to proceed from here?

  7. peterjc commented on Mar 3, 2020

    @peterjc
    MemberAuthor

    Sorry @adcrn, I forgot that - yes, this only applies to the tests already using unittest.

    Using flake8-assertive will be more reliable than grep 👍

  8. added a commit that references this issue on May 9, 2020
  9. peterjc commented on May 9, 2020

    @peterjc
    MemberAuthor

    My tiny pull request #2873 should close this issue.

  10. removed
    good first issue
    on May 9, 2020
  11. added a commit that references this issue on May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions