isidentical / teyit

Formatter for your Python unit tests
MIT License
104 stars 7 forks source link

Rewrite assertRegexpMatches and assertNotRegexpMatches #4

Closed hugovk closed 3 years ago

hugovk commented 3 years ago

Deprecated since Python 3.2:

Removed in Python 3.11:


These are also removed in 3.11 but not part of this PR:

Deprecated since 3.1:

And since 3.2:

I wasn't sure if they can be included? *Raises* are usually called with a context manager. And assertDictContainsSubset needs some argument juggling: https://stackoverflow.com/a/59777678/724176.

isidentical commented 3 years ago

Wow, thanks for the patch and the detailed write-up! Regarding the stuff that requires extra argument-juggling, if you'd like you can try to play with an AST-based transformation by implementing visit_assertDictContainsSubset to the _AssertRewriter.

hugovk commented 3 years ago

Thanks, I've had a go in another branch:

https://github.com/hugovk/teyit/commit/b3dc017f2e40546f748829a3f652364b2e0533b5 has args = [right, left, *args] but we'd need that left to contain something that will end up as {**y, **x}

E   AssertionError: 'self.assertEqual(y, x, msg=msg)' != 'self.assertEqual(y, {**y, **x}, msg=msg)'
E   - self.assertEqual(y, x, msg=msg)
E   + self.assertEqual(y, {**y, **x}, msg=msg)
E   ?                     ++++++++ +

https://github.com/hugovk/teyit/runs/3995628461?check_suite_focus=true

How can we make put {**y, **x} in the second arg?

isidentical commented 3 years ago

You can create a new AST node ast.Dict(keys=[None, None], values=[right, left]);

    def visit_assertDictContainsSubset(self, node):
        left, right, *args = node.args
        func = "assertEqual"
        args = [ast.Dict(keys=[None, None], values=[right, left]), *args]
        return Rewrite(node, func, args)

and this should automatically work. Since it is on another branch, I'm merging this as is. Feel free to create a PR for that as well, if you have time to do so.

isidentical commented 3 years ago

Thanks a lot for this PR!

hugovk commented 3 years ago

Thanks! Please see PR https://github.com/isidentical/teyit/pull/5.