pytest-dev/nose2pytest

"from nose import tools" not supported

Closed this issue · 3 comments

I see the syntax "from nose import tools" and then tools.assert_foo(..) quite a bit.

Examples etcd, kitchen, param and xhtml2pdf

Same issue here. We use import nose.tools as nt and then nt.assert_foo(...) as an idiom in our code. I took a quick look at the code here but it isn't immediately obvious to me how you could add a prefix(s) to search/replace for.

After a bit of poking it looks like the lib2to3 pattern matchers would need to be updated to allow for a prefix to these methods. I've never worked with this code before, so I'm just stabbing a bit in the dark, but I got it working in one example for my imported prefix of nt. Below is the change I had to make to get things working for the 1 or 2 arg case

PATTERN_1_OR_2_ARGS = """
    power< 'nt' trailer< '.' '{}' > trailer< '('
        ( not(arglist | argument<any '=' any>) test=any
        | arglist< test=any ',' msg=any > )
    ')' > >
    """

I pieced together just enough info to be dangerous from this webpage: http://python3porting.com/fixers.html

Armed with this fix, I could see making this production in one of two ways:

  1. Add a command line argument for users to pass the name they have imported, e.g. --prefix 'nt'. This would need some further massaging to work for nested modules (i.e. if you did import nose.tools), but it should be easy to dynamically generate this pattern.
  2. As an extension to the above, rather than pass the prefix, it should be possible to look for the import of nose.tools and then determine what the prefix and pattern should be. I have no idea how to do this, and it also looks like the structure of this code as-is isn't factored to accommodate patterns that change on a per-file basis like this. This feels like the value it would add doesn't justify the effort of a large refactor to accommodate it.

I unfortunately don't have more time to dig on this or implement anything, but I wanted to drop what info I did find in my 30 minutes of digging in case that helps anyone else pick up the torch on this one.

It is probably not worth implement, will add complexity for something that can be fixed manually: search/replace all prefixes by empty (eg nose.tools. by "", or nt. by ""). The test suite code is not executed, it is only parsed, therefore only the names matter, not where they come from.

I'm closing this.