pylint or similar to test version-specific language constructs?

Discussion in 'Python' started by jkn, Jan 9, 2013.

  1. jkn

    jkn Guest

    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
    jkn, Jan 9, 2013
    #1
    1. Advertising

  2. jkn

    Gisle Vanem Guest

    "jkn" <9.co.uk> wrote:

    > 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
    Gisle Vanem, Jan 10, 2013
    #2
    1. Advertising

  3. jkn

    Guest

    On Thursday, January 10, 2013 12:45:32 AM UTC+1, jkn wrote:
    > 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 () if you go that way.

    Regards,

    --
    Sylvain
    , Jan 11, 2013
    #3
  4. jkn

    Terry Reedy Guest

    On 1/11/2013 3:29 AM, The Night Tripper wrote:
    > Gisle Vanem wrote:
    >
    >> "jkn" <9.co.uk> wrote:
    >>
    >>> 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.

    >
    > 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'.

    --
    Terry Jan Reedy
    Terry Reedy, Jan 11, 2013
    #4
  5. jkn

    Dave Angel Guest

    On 01/11/2013 03:29 AM, The Night Tripper wrote:
    > Gisle Vanem wrote:
    >
    >> "jkn" <9.co.uk> wrote:
    >>
    >>> 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.

    > 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.



    --

    DaveA
    Dave Angel, Jan 11, 2013
    #5
  6. On Fri, 11 Jan 2013 10:06:30 -0500, Dave Angel wrote:

    > On 01/11/2013 03:29 AM, The Night Tripper wrote:
    >> Gisle Vanem wrote:
    >>
    >>> "jkn" <9.co.uk> wrote:
    >>>
    >>>> 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.

    >> 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.


    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.


    --
    Steven
    Steven D'Aprano, Jan 11, 2013
    #6
  7. jkn

    Dave Angel Guest

    On 01/11/2013 10:37 AM, Steven D'Aprano wrote:
    > On Fri, 11 Jan 2013 10:06:30 -0500, Dave Angel wrote:
    >
    >> <snip>

    >
    >> 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.
    >


    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.



    --

    DaveA
    Dave Angel, Jan 11, 2013
    #7
  8. jkn

    jkn Guest

    Hi Dave

    On 11 Jan, 15:06, Dave Angel <> wrote:
    >
    > 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
    jkn, Jan 13, 2013
    #8
  9. On Mon, Jan 14, 2013 at 4:56 AM, jkn <9.co.uk> wrote:
    > Hi Dave
    >
    > On 11 Jan, 15:06, Dave Angel <> wrote:
    >>
    >> 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.


    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
    Chris Angelico, Jan 13, 2013
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. V Green
    Replies:
    0
    Views:
    841
    V Green
    Feb 5, 2008
  2. PA Bear [MS MVP]
    Replies:
    0
    Views:
    955
    PA Bear [MS MVP]
    Feb 5, 2008
  3. Tim Frink

    Definition of language constructs

    Tim Frink, Oct 6, 2009, in forum: C Programming
    Replies:
    10
    Views:
    577
    David Thompson
    Oct 20, 2009
  4. Giacomo Boffi
    Replies:
    4
    Views:
    1,658
    Jean-Michel Pichavant
    Apr 19, 2010
  5. Replies:
    4
    Views:
    101
Loading...

Share This Page