pylint or similar to test version-specific language constructs?

J

jkn

Hi all
I have to write python code which must run on an old version of
python (v2.4) as well as a newer (v2.7). I am using pylint and would
like to check if is possible to check with pylint the use of operators
etc. which are not present in 2.4; the ternary operator springs to
mind.

I haven't found anything in pylint which indicates it can do this sort
of check; am I missing anything? Other suggestions for this kind of
checking welcome.

Thanks
Jon N
 
G

Gisle Vanem

jkn said:
I have to write python code which must run on an old version of
python (v2.4) as well as a newer (v2.7). I am using pylint and would
like to check if is possible to check with pylint the use of operators
etc. which are not present in 2.4; the ternary operator springs to
mind.

No idea about PyLint. Why not install Python 2.4 and test
with that? Sounds safer IMHO.

-gv
 
T

thenault

Hi all

I have to write python code which must run on an old version of

python (v2.4) as well as a newer (v2.7). I am using pylint and would

like to check if is possible to check with pylint the use of operators

etc. which are not present in 2.4; the ternary operator springs to

mind.



I haven't found anything in pylint which indicates it can do this sort

of check; am I missing anything? Other suggestions for this kind of

checking welcome.


Hi,

there is no such checker in pylint yet, though it should be fairly easy to build one. The longer part being to identify all constructs that should be detected and their representation in the ast. Then pylint checkers are simple visitors. You'll get some help from the project mailing list ([email protected]) if you go that way.

Regards,
 
T

Terry Reedy

I do have Python 2.4 installed; but I would like a checker that warned me
beforehand about trying to use constructs (like the ternary operator,
decorators) which are version-specific.

Search each chapter of the reference manual (about 7) for 'version changed'.
 
D

Dave Angel

I do have Python 2.4 installed; but I would like a checker that warned me
beforehand about trying to use constructs (like the ternary operator,
decorators) which are version-specific.

J^n

Not sure what you mean by beforehand. Don't you run all your unit tests
before putting each revision of your code into production? So run those
tests twice, once on 2.7, and once on 2.4. A unit test that's testing
code with a ternary operator will fail, without any need for a separate
test.

if it doesn't, then you've got some coverage gaps in your unit tests.
 
S

Steven D'Aprano

Decorators work fine in Python 2.4.

Not sure what you mean by beforehand. Don't you run all your unit tests
before putting each revision of your code into production? So run those
tests twice, once on 2.7, and once on 2.4. A unit test that's testing
code with a ternary operator will fail, without any need for a separate
test.

You don't even need tests for the code that includes the ternary
operator. The module simply won't compile in Python 2.4, you get a
SyntaxError when you try to import it or run it.

You don't need PyLint to check for *illegal syntax*. Python already does
that.
 
D

Dave Angel

You don't even need tests for the code that includes the ternary
operator. The module simply won't compile in Python 2.4, you get a
SyntaxError when you try to import it or run it.

You don't need PyLint to check for *illegal syntax*. Python already does
that.

You're right of course. But I can weasel out of it by saying that
Python will only check it if the particular module is imported. I've
seen equivalent bugs in real projects, where a release went out with one
of its dynamic libraries not even present.

Another thing that can stop Python from checking is if some naive
programmer uses a bare except in production code.

Both of these would show up in the simplest of unit testing code.
 
J

jkn

Hi Dave

Not sure what you mean by beforehand.  Don't you run all your unit tests
before putting each revision of your code into production?  So run those
tests twice, once on 2.7, and once on 2.4.  A unit test that's testing
code with a ternary operator will fail, without any need for a separate
test.

if it doesn't, then you've got some coverage gaps in your unit tests.

By 'beforehand' I meant 'before testing on my target 2.4 system;
perhaps I should have been clearer in that I am running 2.7 on my
'development' platform, and 2.4 on my target. It would be painful to
put 2.4 on my target system (although I continue to wonder about
that...). So I was looking to catch such errors before migrating to
the target.

[Steven D'Aprano]
Decorators work fine in Python 2.4.

Yes, I was just coming up with another example of a language construct
which didn't exist at one point.
You don't even need tests for the code that includes the ternary
operator. The module simply won't compile in Python 2.4, you get a
SyntaxError when you try to import it or run it.

In fact I had a misapprehension about this; for some reason (I thought
I'd tried it) I thought such an error only got caught at runtime, not
'compile-time'. I now see that this is not the case, which means the
athe problem is less of a concern than I thought.

Thanks for the comments.

Jon N
 
C

Chris Angelico

Hi Dave



By 'beforehand' I meant 'before testing on my target 2.4 system;
perhaps I should have been clearer in that I am running 2.7 on my
'development' platform, and 2.4 on my target. It would be painful to
put 2.4 on my target system (although I continue to wonder about
that...). So I was looking to catch such errors before migrating to
the target.

Painful to put 2.4 on your dev, you mean? I've never done it, but I
would expect that the old sources will compile against newer libraries
with no problems.

That's likely to be the easiest option. It's the language-level
equivalent of watching for a thrown exception rather than asking
forgiveness beforehand :)

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top