469,289 Members | 2,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

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.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
Jul 18 '05 #1
6 24704
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
Sc***********@Acm.Org

Jul 18 '05 #2
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

Jul 18 '05 #3
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 / ia**@colorstudy.com / http://blog.ianbicking.org
Jul 18 '05 #4
On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Sc***********@Acm.Org> 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
Sc***********@Acm.Org


Regards,
Bengt Richter
Jul 18 '05 #5
Bengt Richter wrote:
On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Sc***********@Acm.Org> 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
Jul 18 '05 #6
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
<Sc***********@Acm.Org> 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
Sc***********@Acm.Org
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.