Search code examples
pythonunicodepattern-matchingspecial-characterspython-2to3

Problem with literal arguments in the PATTERN string for a python 2to3 fixer


I'm writing a fixer for the 2to3 tool in python.

In my pattern string, I have a section where I'd like to match an empty string as an argument, or an empty unicode string. The relevant chunk of my pattern looks like:

(args='""' | args='u""')

My issue is the second option never matches. Even if it's alone, it won't match. However, if I simply say args=any and then output args, I can catch cases where args is exactly equal to the second option.

Is there some weird unicode handling thing going on? Why won't the second literal option ever match?


Solution

  • Because 2to3 pattern matching is designed to match tokens not literals, there is no way to do this directly.

    Instead you could match (args=STRING) and then determine the value of the string argument inside the transformation function and handle it appropriately.