teyit icon indicating copy to clipboard operation
teyit copied to clipboard

Refactor `assert x == y` into `self.assertEqual(x, y)`

Open sobolevn opened this issue 3 years ago • 4 comments
trafficstars

I think the second option is better because:

  1. It does not get removed in -O mode
  2. It has much cleaner error message

sobolevn avatar Feb 13 '22 09:02 sobolevn

Are there any tests that use assert x == y in an unittest? I've never seen such code, but if you have any examples feel free to add an implementation for that.

isidentical avatar Feb 13 '22 13:02 isidentical

CPython has lots of assert tests with unittest. Examples:

  • https://github.com/python/cpython/blob/main/Lib/test/test_types.py#L229-L230
  • https://github.com/python/cpython/blob/main/Lib/test/test_typing.py#L4109-L4110
  • https://github.com/python/cpython/blob/main/Lib/test/test_dis.py#L1432
  • etc

I think that this is an error, but I would not push this change there 🙂 I will send a PR with this feature somewhere this week, though 👍

sobolevn avatar Feb 13 '22 14:02 sobolevn

Ah, TIL. Would gladly accept a PR about it.

isidentical avatar Feb 13 '22 14:02 isidentical

I think that there are some important limitations to this rule:

  1. It should be limited to ast.[Async]FunctionDef scope inside ast.ClassDef
  2. We must be sure that it is a test function. For now, I am going to detect a test as "a method" with test prefix or postfix
  3. We must be sure that self argument exists

sobolevn avatar Feb 28 '22 12:02 sobolevn