Python switch for syntax checking

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Duffy

    Python switch for syntax checking

    Hi all.

    I've been wondering why python itself doesn't provide a switch to
    check a file for valid syntax. I know that you can currently call

    python -c "import py_compile; py_compile.comp ile(r'MyApp.py' )"

    but in order to manage this effectively you have to add a shell alias,
    write a script, mess about with your editor, or what have you. This
    becomes yet another small annoyance I'd like to get rid of. I'm also
    aware of PyChecker and other lint like tools, but these run the code and
    often I don't want that.

    Being able to do

    python -s MyApp.py

    and see any syntax errors would be a very useful thing. Is there a
    specific reason that this doesn't exist (or perhaps it does and I'm
    uninformed)? I don't see any relevant PEPs on the list, either.

    Jeff Duffy
  • Scott David Daniels

    #2
    Re: Python switch for syntax checking

    Jeff Duffy wrote:[color=blue]
    > I've been wondering why python itself doesn't provide a switch to check
    > a file for valid syntax. I know that you can currently call
    > python -c "import py_compile; py_compile.comp ile(r'MyApp.py' )"
    > ...[/color]
    I suspect the reason is threefold.

    First, "python MyApp.py" does a syntax check anyway. If MyApp is
    not a main program, all you get is the syntax check. As for main
    programs, larger applications are often split into smaller files.
    I seldom have large main program files to check.

    Second, lots of extreme programmer (XP) practitioners hang out here,
    and even more who have adopted at leasrt some of the XP techniques.
    Those people tend to run unit tests rather than syntax checks.

    Third, pychecker provides better analysis if you want static analysis.

    I pretty much try to go the unit test way myself. While I would
    suggest you try it out, the three reasons above are meant to explain
    why nobody has put effort into a syntax check switch.

    --Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • Craig Ringer

      #3
      Re: Python switch for syntax checking

      On Sat, 2004-11-20 at 02:46, Jeff Duffy wrote:[color=blue]
      > Hi all.
      >
      > I've been wondering why python itself doesn't provide a switch to
      > check a file for valid syntax. I know that you can currently call
      >
      > python -c "import py_compile; py_compile.comp ile(r'MyApp.py' )"
      >
      > but in order to manage this effectively you have to add a shell alias,
      > write a script, mess about with your editor, or what have you. This
      > becomes yet another small annoyance I'd like to get rid of. I'm also
      > aware of PyChecker and other lint like tools, but these run the code and
      > often I don't want that.[/color]

      The talk of a new switch to import a module would do the job nicely. If
      you just import your script as a module, you know it loads and the main
      program runs. Just add the usual

      if __name__ == '__main__':
      main(sys.argv)

      so your code loads as a module without executing its normal functions,
      and you have your syntax check plus a bit of other sanity checking (all
      modules import, etc).

      In the mean time, it's fairly trivial to:

      python -c "import myscript"

      and not significantly longer / more complex than some sort of check
      flag.

      That said, I'm all in favour of unit tests to make sure your code
      actually runs. Especially in Python, where things like late binding of
      names means that you can't check at load time whether names exit, unit
      tests are IMO the way to go.

      pylint / pychecker may also be worth looking into.

      --
      Craig Ringer

      Comment

      • Ian Bicking

        #4
        Re: Python switch for syntax checking

        Jeff Duffy wrote:[color=blue]
        > Hi all.
        >
        > I've been wondering why python itself doesn't provide a switch to check
        > a file for valid syntax. I know that you can currently call
        >
        > python -c "import py_compile; py_compile.comp ile(r'MyApp.py' )"[/color]

        If you look at the py_compile module, if it's run as a script it will
        compile any filenames you pass to it, so you should be able to do:

        python /path/to/py_compile.py MyApp.py

        And with Python 2.4 I think it will be:

        python -m py_compile MyApp.py

        --
        Ian Bicking / ianb@colorstudy .com / http://blog.ianbicking.org

        Comment

        • Bengt Richter

          #5
          Re: Python switch for syntax checking

          On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Scott.Daniels@ Acm.Org> wrote:
          [color=blue]
          >Jeff Duffy wrote:[color=green]
          >> I've been wondering why python itself doesn't provide a switch to check
          >> a file for valid syntax. I know that you can currently call
          >> python -c "import py_compile; py_compile.comp ile(r'MyApp.py' )"
          >> ...[/color]
          >I suspect the reason is threefold.
          >
          >First, "python MyApp.py" does a syntax check anyway. If MyApp is
          >not a main program, all you get is the syntax check. As for main[/color]
          ??? What does "not a main program mean"? I'm not sure what you mean, e.g.,

          [14:00] C:\pywk\clp>pyt hon notmain.py
          notmain.py executing

          [14:00] C:\pywk\clp>pyt hon
          Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> import notmain[/color][/color][/color]
          notmain.py executing[color=blue][color=green][color=darkred]
          >>> ^Z[/color][/color][/color]


          [14:00] C:\pywk\clp>py2 4
          Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
          [GCC 3.2.3 (mingw special 20030504-1)] on win32
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> import notmain[/color][/color][/color]
          notmain.py executing

          Oops, forgot to show you what notmain.py was:
          [color=blue][color=green][color=darkred]
          >>> print '%s\n%s%s' % ('-'*40, open('notmain.p y').read(), '-'*40)[/color][/color][/color]
          ----------------------------------------
          print 'notmain.py executing'

          ----------------------------------------
          [color=blue]
          >programs, larger applications are often split into smaller files.
          >I seldom have large main program files to check.
          >
          >Second, lots of extreme programmer (XP) practitioners hang out here,
          >and even more who have adopted at leasrt some of the XP techniques.
          >Those people tend to run unit tests rather than syntax checks.
          >
          >Third, pychecker provides better analysis if you want static analysis.
          >
          >I pretty much try to go the unit test way myself. While I would
          >suggest you try it out, the three reasons above are meant to explain
          >why nobody has put effort into a syntax check switch.
          >
          >--Scott David Daniels
          >Scott.Daniels@ Acm.Org
          >[/color]

          Regards,
          Bengt Richter

          Comment

          • Jeff Shannon

            #6
            Re: Python switch for syntax checking

            Bengt Richter wrote:
            [color=blue]
            >On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Scott.Daniels@ Acm.Org> wrote:
            >
            >
            >[color=green]
            >>First, "python MyApp.py" does a syntax check anyway. If MyApp is
            >>not a main program, all you get is the syntax check. As for main
            >>
            >>[/color]
            >??? What does "not a main program mean"? I'm not sure what you mean, e.g.,
            >
            >[/color]

            I believe that Scott is referring to the difference between a file run
            from the commandline, in which __name__ is set to "__main__", and a file
            that's imported as a module, in which __name__ is set to the name of the
            module, which is normally the filename minus the .py[c|o|d] extension.

            Of course, this would also imply that Scott is presuming that one is
            following the good programming practice of not putting any significant
            code (other than function, class, and global variable definitions) at
            module level, except where protected by an 'if __name__ == "__main__": '
            statement. That *is* good practice, and most people do it, but it's not
            required so perhaps not a safe presumption...

            Jeff Shannon
            Technician/Programmer
            Credit International


            Comment

            • Scott David Daniels

              #7
              Re: Python switch for syntax checking

              Jeff Duffy originally wrote:[color=blue]
              > I've been wondering why python itself doesn't provide a switch to
              > check a file for valid syntax....[/color]
              [color=blue]
              > Jeff Shannon wrote:
              > Bengt Richter wrote:[color=green]
              >> On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels
              >> <Scott.Daniels@ Acm.Org> wrote:[color=darkred]
              >>> First, "python MyApp.py" does a syntax check anyway. If MyApp is
              >>> not a main program, all you get is the syntax check. As for main[/color]
              >>
              >> ??? What does "not a main program mean"? I'm not sure what you mean,[/color]
              >
              > I believe that Scott is referring to the difference between a file run
              > from the commandline, in which __name__ is set to "__main__", and a file
              > that's imported as a module, in which __name__ is set to the name of the
              > module, which is normally the filename minus the .py[c|o|d] extension.
              >[/color]
              I do mean something like that. Essentially, I mean "if the file is
              meant to be imported, rather than run."
              [color=blue]
              > Of course, this would also imply that Scott is presuming that one is
              > following the good programming practice of not putting any significant
              > code (other than function, class, and global variable definitions) at
              > module level, except where protected by an 'if __name__ == "__main__": '
              > statement. That *is* good practice, and most people do it, but it's not
              > required so perhaps not a safe presumption...[/color]

              Remember, I am explaining why we normally don't have a syntax-check-only
              option, and my thesis is that there is no perceived need on the part of
              the core implementers.

              --Scott David Daniels
              Scott.Daniels@A cm.Org

              Comment

              Working...