views:

388

answers:

2

Is there something similar to Pylint, that will look at a Python script (or run it), and determine which version of Python each line (or function) requires?

For example, theoretical usage:

$ magic_tool <EOF
with something:
    pass
EOF
1: 'with' statement requires Python 2.6 or greater

$ magic_tool <EOF
class Something:
    @classmethod
    def blah(cls):
        pass
EOF
2: classmethod requires Python 2.2 or greater
$ magic_tool <EOF
print """Test
"""
EOF
1: Triple-quote requires Python 1.5 of later

Is such a thing possible? I suppose the simplest way would be to have all Python versions on disc, run the script with each one and see what errors occur..

+6  A: 

Not an actual useful answer but here it goes anyway. I think this should be doable to make (though probably quite an exercise), for example you could make sure you have all the official grammars for the versions you want to check, like this one .

Then parse the bit of code starting with the first grammar version. Next you need a similar map of all the built-in module namespaces and parse the code again starting with the earliest version, though it might be tricky to differentiate between built-in modules and modules that are external or something in between like ElementTree.

The result should be an overview of versions that support the syntax of the code and an overview of the modules and which version (if at all) is needed to use it. With that result you could calculate the best lowest and highest version.

Martin P. Hellwig
+1 For the college try at a task like this, and for your remarks on the difficulties in accomplishing this using the example of ElementTree. This question pretty much hints at static analysis, which is very hard to achieve for dynamic languages as you point out. With Python's occasional use of "from __future__ import blah", it gets even harder. And then throw in duck typing :-) Yours is a good answer that introduces all of these worms in the can.
Jarret Hardie
Have a look at my new answer to this question, I put together a script that tries to identify the minimum Python version required.
Greg Hewgill
+12  A: 

Inspired by this excellent question, I recently put together a script that tries to do this. You can find it on github at pyqver.

It's reasonably complete but there are some aspects that are not yet handled (as mentioned in the README file). Feel free to fork and improve it!

Greg Hewgill