Finding x is 1, and x is 'foo' comparisons in a code base

Discussion in 'Python' started by Alex Willmer, Jan 17, 2012.

  1. Alex Willmer

    Alex Willmer Guest

    Hello,

    I'm looking for a way to find the occurrences of x is y comparisons in
    an existing code base. Except for a few special cases (e.g. x is [not]
    None) they're a usually mistakes, the correct test being x == y.
    However they happen to work most of the time on CPython (e.g. when y
    is a small integer or string) so they slip into production code
    unnoticed.

    PyLint and PyFlakes don't check this AFAICT. Any suggestions for such
    a tool, or a pointer how to add the check to an existing tool would be
    most welcome.

    Regards, Alex
     
    Alex Willmer, Jan 17, 2012
    #1
    1. Advertising

  2. Alex Willmer

    MRAB Guest

    On 17/01/2012 17:10, Alex Willmer wrote:
    > Hello,
    >
    > I'm looking for a way to find the occurrences of x is y comparisons in
    > an existing code base. Except for a few special cases (e.g. x is [not]
    > None) they're a usually mistakes, the correct test being x == y.
    > However they happen to work most of the time on CPython (e.g. when y
    > is a small integer or string) so they slip into production code
    > unnoticed.
    >
    > PyLint and PyFlakes don't check this AFAICT. Any suggestions for such
    > a tool, or a pointer how to add the check to an existing tool would be
    > most welcome.
    >

    I would search using a regex such as:

    \bis\b(?! +None| +not +None)

    in either a text editor or using a Python script.
     
    MRAB, Jan 17, 2012
    #2
    1. Advertising

  3. Am 17.01.2012 18:10, schrieb Alex Willmer:
    > Hello,
    >
    > I'm looking for a way to find the occurrences of x is y comparisons in
    > an existing code base. Except for a few special cases (e.g. x is [not]
    > None) they're a usually mistakes, the correct test being x == y.
    > However they happen to work most of the time on CPython (e.g. when y
    > is a small integer or string) so they slip into production code
    > unnoticed.
    >
    > PyLint and PyFlakes don't check this AFAICT. Any suggestions for such
    > a tool, or a pointer how to add the check to an existing tool would be
    > most welcome.


    Pychecker contains a check for this mistake.
     
    Thomas Heller, Jan 17, 2012
    #3
    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. Alex Vinokur
    Replies:
    7
    Views:
    457
    Jerry Coffin
    Sep 21, 2003
  2. Alex Vinokur
    Replies:
    4
    Views:
    493
    Ron Natalie
    Sep 24, 2003
  3. Replies:
    3
    Views:
    406
    John Roth
    Jul 29, 2005
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    419
    JaredGrubb
    Sep 24, 2008
  5. Replies:
    4
    Views:
    171
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page