Python switch for syntax checking

Discussion in 'Python' started by Jeff Duffy, Nov 19, 2004.

  1. Jeff Duffy

    Jeff Duffy Guest

    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.compile(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
    Jeff Duffy, Nov 19, 2004
    #1
    1. Advertising

  2. Jeff Duffy wrote:
    > 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.compile(r'MyApp.py')"
    > ...

    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 David Daniels, Nov 19, 2004
    #2
    1. Advertising

  3. Jeff Duffy

    Craig Ringer Guest

    On Sat, 2004-11-20 at 02:46, Jeff Duffy wrote:
    > 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.compile(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.


    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
    Craig Ringer, Nov 19, 2004
    #3
  4. Jeff Duffy

    Ian Bicking Guest

    Jeff Duffy wrote:
    > 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.compile(r'MyApp.py')"


    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 / / http://blog.ianbicking.org
    Ian Bicking, Nov 19, 2004
    #4
  5. On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <> wrote:

    >Jeff Duffy wrote:
    >> 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.compile(r'MyApp.py')"
    >> ...

    >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

    ??? What does "not a main program mean"? I'm not sure what you mean, e.g.,

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

    [14:00] C:\pywk\clp>python
    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.
    >>> import notmain

    notmain.py executing
    >>> ^Z



    [14:00] C:\pywk\clp>py24
    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.
    >>> import notmain

    notmain.py executing

    Oops, forgot to show you what notmain.py was:

    >>> print '%s\n%s%s' % ('-'*40, open('notmain.py').read(), '-'*40)

    ----------------------------------------
    print 'notmain.py executing'

    ----------------------------------------

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


    Regards,
    Bengt Richter
    Bengt Richter, Nov 19, 2004
    #5
  6. Jeff Duffy

    Jeff Shannon Guest

    Bengt Richter wrote:

    >On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <> wrote:
    >
    >
    >
    >>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
    >>
    >>

    >??? What does "not a main program mean"? I'm not sure what you mean, e.g.,
    >
    >


    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
    Jeff Shannon, Nov 19, 2004
    #6
  7. Jeff Duffy originally wrote:
    > I've been wondering why python itself doesn't provide a switch to
    > check a file for valid syntax....


    > Jeff Shannon wrote:
    > Bengt Richter wrote:
    >> On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels
    >> <> wrote:
    >>> 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

    >>
    >> ??? What does "not a main program mean"? I'm not sure what you mean,

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

    I do mean something like that. Essentially, I mean "if the file is
    meant to be imported, rather than run."

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


    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 David Daniels, Nov 19, 2004
    #7
    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. Christopher Benson-Manica

    switch syntax and style

    Christopher Benson-Manica, Jul 20, 2004, in forum: C Programming
    Replies:
    6
    Views:
    347
    Christopher Benson-Manica
    Jul 20, 2004
  2. Michael Mair
    Replies:
    1
    Views:
    410
    S.Tobias
    Jun 14, 2005
  3. Guybrush Threepwood

    syntax error in switch statement

    Guybrush Threepwood, Jun 8, 2007, in forum: C Programming
    Replies:
    8
    Views:
    387
    Christopher Benson-Manica
    Jun 8, 2007
  4. mahantesh varavattekar
    Replies:
    1
    Views:
    202
    Laurent Claessens
    Jun 23, 2011
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    106
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page