Add 'use warnings' directive, like in Perl
Hello group, my name is Eduard. I came into the Python community with an idea to add (implicitly or explicitly) 'use warnings' directive into the python's programs. I think that everyone who worked with Perl understand what I am talking about. For the rest I will explain the idea. Actually, this is my first experience for writing into the community like this, so excuse me if you found some mistakes or oddities. Also I do not know whether you are already talk about this topic..in any case - sorry. So, imagine that you have a program: #!/usr/bin/python *word* = raw_input("Enter line : ") if *word* == "hello": print ("You wrote \'hello\'") else: if *world* == "buy": #Error! should be word not world print "Buy" else: *iamnotfunction* #Also error This script contains two errors. And in both cases we will know about it at runtime. And the most worst thing is that you will not know about these errors until someone enters anything other than the "hello" word.. Try and except blocks do not solve this problem. Within this approach we also receive problem at runtime. What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this: #!/usr/bin/python *use warnings* *word* = raw_input("Enter line : ") if *word* == "hello": print ("You wrote \'hello\'") else: if *world* == "buy": #Error! should be word not world print "Buy" else: *iamnotfunction* #Also error Output: Use of uninitialized value world in eq (==) at test.py line .. Useless use of a constant (iamnotfunction) in void context at test.py line .. The user will see the output like this and the program will not start. To my mind the good idea is to explicitly set this directive. If developer does not want spend time for this checking, he can omit 'use warning' directive. Also it will not corrupt the existing programs. And developers will have a chance to add this line gradually. Many thanks! - Eduard
Hi,
* Eduard Bondarenko
What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this:
It probably doesn't make sense to add something like this to CPython itself - but there are many third-party tools which do such kind of checks (and which I can recommend to use!), static analyzers/linters: https://pypi.python.org/pypi/pyflakes https://pypi.python.org/pypi/pep8 https://pypi.python.org/pypi/flake8 (a wrapper around pyflakes/pep8) http://www.pylint.org/ Florian -- http://www.the-compiler.org | me@the-compiler.org (Mail/XMPP) GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc I love long mails! | http://email.is-not-s.ms/
Linters are actually quite good at finding this kind of mistake, and are the accepted way to deal with such checks. Flake8 is a popular one you may wish to check out: https://pypi.python.org/pypi/flake8 https://pypi.python.org/pypi/flake8 Ryan
On Feb 10, 2015, at 10:04 AM, Eduard Bondarenko
wrote: Hello group,
my name is Eduard. I came into the Python community with an idea to add (implicitly or explicitly) 'use warnings' directive into the python's programs.
I think that everyone who worked with Perl understand what I am talking about. For the rest I will explain the idea.
Actually, this is my first experience for writing into the community like this, so excuse me if you found some mistakes or oddities. Also I do not know whether you are already talk about this topic..in any case - sorry.
So, imagine that you have a program:
#!/usr/bin/python
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
This script contains two errors. And in both cases we will know about it at runtime. And the most worst thing is that you will not know about these errors until someone enters anything other than the "hello" word..
Try and except blocks do not solve this problem. Within this approach we also receive problem at runtime.
What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this:
#!/usr/bin/python
use warnings
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
Output: Use of uninitialized value world in eq (==) at test.py line .. Useless use of a constant (iamnotfunction) in void context at test.py line ..
The user will see the output like this and the program will not start.
To my mind the good idea is to explicitly set this directive. If developer does not want spend time for this checking, he can omit 'use warning' directive. Also it will not corrupt the existing programs. And developers will have a chance to add this line gradually.
Many thanks!
- Eduard _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Hi Eduard,
Welcome to Python!
On Tue, Feb 10, 2015 at 10:12 AM, Florian Bruhin
Hi,
* Eduard Bondarenko
[2015-02-10 18:04:47 +0200]: What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this:
It probably doesn't make sense to add something like this to CPython itself - but there are many third-party tools which do such kind of checks (and which I can recommend to use!), static analyzers/linters:
https://pypi.python.org/pypi/pyflakes https://pypi.python.org/pypi/pep8 https://pypi.python.org/pypi/flake8 (a wrapper around pyflakes/pep8) http://www.pylint.org/
So as a maintainer/contributor to three of those I agree. But they're external dependencies which I sense is something you don't want. The reality is that you can compile this code with some standard library modules and they'll give you the feedback you want. If you were to run this code you'd get a NameErrors for the first line where you expect a warning and you would get a SyntaxError for the second else statement after the first. pyflakes and flake8 turn those into error codes for you. So really, python already comes with the warnings on because doing $ python test.py Would first result in a SyntaxError (because of your else following the first else) and then once that's been fixed, you'd get a NameError. Disclaimer: I didn't actually run the code so I may have the order of errors wrong, but I think this is roughly correct. Cheers, Ian
On Tue, Feb 10, 2015 at 11:26 AM, Ian Cordasco
$ python test.py
Would first result in a SyntaxError (because of your else following the first else) and then once that's been fixed, you'd get a NameError.
Disclaimer: I didn't actually run the code so I may have the order of errors wrong, but I think this is roughly correct.
The syntax error can be corrected with elif, but the OP is correct that the NameError will only happen when that branch is actually run, not at compile-time. Of course, this is probably a better argument for a test suite that covers all of your branches and doing static analysis than it is for adding Perl-style pragmas to the language. Perl needed use warnings and use strict because the language allows some horrible things and didn't want to make good programming mandatory at the expense of breaking existing code, instead making it "suggested" to use pragmas that enforce some good habits.
On 2/10/2015 11:04 AM, Eduard Bondarenko wrote:
Actually, this is my first experience for writing into the community like this, so excuse me if you found some mistakes or oddities.
Code should be plain plain-text, without markup such as *s around identifiers.
#!/usr/bin/python
*word* = raw_input("Enter line : ")
if *word* == "hello": print ("You wrote \'hello\'") else: if *world* == "buy": #Error! should be word not world print "Buy"
elif world == "buy": #Error! should be word not world
else: *iamnotfunction* #Also error
This script contains two errors. And in both cases we will know about it at runtime. And the most worst thing is that you will not know about these errors until someone enters anything other than the "hello" word..
The test suite should test each branch each way. https://pypi.python.org/pypi/coverage/3.7.1 makes this easy. -- Terry Jan Reedy
Hi Eduard, What you describe is not at all equivalent to 'use warnings' in perl. 'use warnings' is a directive to enable/disable all or certain warnings perl code may emit. The equivalent of this exists in python: https://docs.python.org/3.1/library/warnings.html On di, 2015-02-10 at 18:04 +0200, Eduard Bondarenko wrote:
Hello group,
my name is Eduard. I came into the Python community with an idea to add (implicitly or explicitly) 'use warnings' directive into the python's programs.
I think that everyone who worked with Perl understand what I am talking about. For the rest I will explain the idea.
Actually, this is my first experience for writing into the community like this, so excuse me if you found some mistakes or oddities. Also I do not know whether you are already talk about this topic..in any case - sorry.
So, imagine that you have a program:
#!/usr/bin/python
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
This script contains two errors. And in both cases we will know about it at runtime. And the most worst thing is that you will not know about these errors until someone enters anything other than the "hello" word..
Try and except blocks do not solve this problem. Within this approach we also receive problem at runtime.
What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this:
#!/usr/bin/python
use warnings
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
Output: Use of uninitialized value world in eq (==) at test.py line .. Useless use of a constant (iamnotfunction) in void context at test.py line ..
The user will see the output like this and the program will not start.
To my mind the good idea is to explicitly set this directive. If developer does not want spend time for this checking, he can omit 'use warning' directive. Also it will not corrupt the existing programs. And developers will have a chance to add this line gradually.
Many thanks!
- Eduard
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Dennis Kaarsemaker http://www.kaarsemaker.net
Hello Dennis,
I know that, actually I mean the simple version of Perl's 'use warnings'
directive. I do not mean to show warnings like " "isn't numeric" " in the
code below or something like this.
my $a = "2:" + 3;
And roughly speaking, I mentioned Perl's directive to more clearly show
what I mean.
About your reference.
If I correctly understood provided material this is runtime warnings and
absolutely not the same what I mean.
2015-02-11 11:03 GMT+02:00 Dennis Kaarsemaker
Hi Eduard,
What you describe is not at all equivalent to 'use warnings' in perl. 'use warnings' is a directive to enable/disable all or certain warnings perl code may emit. The equivalent of this exists in python:
https://docs.python.org/3.1/library/warnings.html
On di, 2015-02-10 at 18:04 +0200, Eduard Bondarenko wrote:
Hello group,
my name is Eduard. I came into the Python community with an idea to add (implicitly or explicitly) 'use warnings' directive into the python's programs.
I think that everyone who worked with Perl understand what I am talking about. For the rest I will explain the idea.
Actually, this is my first experience for writing into the community like this, so excuse me if you found some mistakes or oddities. Also I do not know whether you are already talk about this topic..in any case - sorry.
So, imagine that you have a program:
#!/usr/bin/python
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
This script contains two errors. And in both cases we will know about it at runtime. And the most worst thing is that you will not know about these errors until someone enters anything other than the "hello" word..
Try and except blocks do not solve this problem. Within this approach we also receive problem at runtime.
What I propose ? I propose to add 'use warnings' directive. This directive will provide deeply verification. Like this:
#!/usr/bin/python
use warnings
word = raw_input("Enter line : ")
if word == "hello": print ("You wrote \'hello\'") else: if world == "buy": #Error! should be word not world print "Buy" else: iamnotfunction #Also error
Output: Use of uninitialized value world in eq (==) at test.py line .. Useless use of a constant (iamnotfunction) in void context at test.py line ..
The user will see the output like this and the program will not start.
To my mind the good idea is to explicitly set this directive. If developer does not want spend time for this checking, he can omit 'use warning' directive. Also it will not corrupt the existing programs. And developers will have a chance to add this line gradually.
Many thanks!
- Eduard
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Dennis Kaarsemaker http://www.kaarsemaker.net
participants (7)
-
Dennis Kaarsemaker
-
Eduard Bondarenko
-
Florian Bruhin
-
Geoffrey Spear
-
Ian Cordasco
-
Ryan Hiebert
-
Terry Reedy