Hmm, this can be done with Python code too. And there are already tools doing so.
Can it? That's what I would like to know, how to know without executing the code with the argument 4 that there's an error in this Python code:
import sys;
def f( x ):
if x == 4:
x += q
print( x )
f( int( sys.argv[1] ) )
That will "work" until 4 is passed at the runtime: $ ex-undefined.py 4
Traceback (most recent call last):
File "/tmp/ex-undefined.py", line 8, in <module>
f( int( sys.argv[1] ) )
File "/tmp/ex-undefined.py", line 5, in f
x += q
NameError: name 'q' is not defined
With this shell code #!/bin/sh
f() {
x=$1
if [ "$x" = 4 ] ; then
x=$((x+q))
fi
echo $x
}
f "$1"
and shellcheck I get: Line 5:
x=$((x+q))
^-- SC2154: q is referenced but not assigned.
even if I have never executed the code. $ flake8 foo.py
foo.py:6:14: F821 undefined name 'q'
$ mypy --check-untyped-defs foo.py
foo.py:6: error: Name 'q' is not defined
(They also have some other complaints about your code that you'd either have to disable or adjust to.) Success: no issues found in 1 source file
and additionally produced a .mypy_cache folder of 2 MB at the place where the script was.So the style of "unreasonable" defaults of Python alone (not detecting the error, speaking from the point of view of a user of Perl) propagates to the "unreasonable" defaults of the checkers.
Still thanks Ded7xSEoPKYNsDd, I really wasn't aware of these! Yet, even if I haven't formally specified that, I was looking for the way to do it with the Python as the language and its default interpreter alone, as for Perl nothing additional has to be installed:
use 5.010;
use strict;
sub f {
my $x = shift;
$x += $q if ( $x == 4 );
say $x;
}
f( $ARGV[ 0 ] );
Gets me: Global symbol "$q" requires explicit package name (did you forget to declare "my $q"?) at ex-undefined.pl line 6.
Execution of ex-undefined.pl aborted due to compilation errors.
I am aware that for shell I'd need an additional shellcheck but Python is many, many times bigger than the shell binary alone (or even the sum of the shell and shellcheck binaries), and actively changed, whereas the shell semantics is standardized and effectively frozen in time, and from the shell interpreter alone a very low startup overhead is expected.