|
|
Log in / Subscribe / Register

Opposition to Python type hints

Opposition to Python type hints

Posted May 7, 2015 13:28 UTC (Thu) by ibukanov (subscriber, #3942)
Parent article: Opposition to Python type hints

My experience with scripting languages is that the most problematic feature is not lack of types but rather lack of declarations for names to check them before running the code. I made much less type errors than simple typos in names. It is so annoying to wait for results only to discover that I mistyped the name of variable to put the results of a long-running code.


to post comments

Opposition to Python type hints

Posted May 7, 2015 14:15 UTC (Thu) by niner (guest, #26151) [Link] (5 responses)

That's not a problem of scripting languages in general. Perl has had use strict; for ages. This has been copied to Javascript as "use strict;" (yes, with the quotes, to provide backwards compatability). Even PHP has optional warnings about undeclared variables.

Python is the user unfriendly hold out here.

Opposition to Python type hints

Posted May 7, 2015 16:08 UTC (Thu) by ibukanov (subscriber, #3942) [Link] (4 responses)

Both Perl and JavaScript even in the strict modes check names for existence only at runtime when the name is accessed or assigned. What I want is to check variable names and ideally property/methods for existence before the execution. At least modern Perl with -w warns about name used only once, but that is not helpful when after refactoring a wrong name is left in several places on rarely accessed branches.

Note that to check for property/method existence one does not need a full-fledged static typesystem. With JavaScript tools like jslint or similar that checks property/methods against a predefined list are enough to capture most of my typos.

Opposition to Python type hints

Posted May 7, 2015 16:51 UTC (Thu) by niner (guest, #26151) [Link] (2 responses)

Perl does check variable names at compile time in strict mode. The only exception is fully qualified package globals like $Foo::Bar::global_variable. But those are very rare.

Perl 6 throws errors for invalid method names if possible at compile time. I.e. when it knows about the types involved and no fallbacks are present. It's also very helpfully complaining about invalid arguments to methods and functions. And its type system allows very fine grained specification of types and signatures. E.g. it allows one to declare a function that takes a hash (dict in Python) as argument and requires that this hash contains certain keys. So in essence, it makes a combination of duck typing and static types possible and useful.

Opposition to Python type hints

Posted May 7, 2015 17:42 UTC (Thu) by ibukanov (subscriber, #3942) [Link] (1 responses)

Hm, with perl 5.18.4:

~/s> cat x.pl
use strict;

my $a = 10;

if ($a) {
print "OK\n";
} else {
print $b . $b . "\n";
}
~/s> perl -w x.pl
OK

No warnings or errors about undeclared $b. What did I miss?

Opposition to Python type hints

Posted May 7, 2015 17:49 UTC (Thu) by niner (guest, #26151) [Link]

That $a and $b are predefined package globals in Perl 5. They should be used for sort blocks:

my @sorted = sort { $a <=> $b } @unsorted;

Try your example with $x and $y and you'll get:

~> perl x.pl
Global symbol "$y" requires explicit package name at x.pl line 6.
Global symbol "$y" requires explicit package name at x.pl line 6.
Execution of x.pl aborted due to compilation errors.

Yes, that's surprising behavior and it's one of the reasons for Perl 6 being a breaking change. Of course, $a and $b are not exactly good variable names anyway :)

Opposition to Python type hints

Posted May 12, 2015 13:36 UTC (Tue) by ehiggs (subscriber, #90713) [Link]

Python also has linters available. If you use vim with the syntastic extension then it calls out to any of a number of linters:

https://github.com/scrooloose/syntastic/tree/master/synta...

Then your editor will check the file every time you ask it to or on na hook (e.g. when saving the file). Then it puts red and/or yellow `>>` symbols in the gutter pointing to issues like in this image:

http://dancallahan.info/journal/python-flake8/syntastic-f...

Opposition to Python type hints

Posted May 7, 2015 15:11 UTC (Thu) by kjp (guest, #39639) [Link]

I never type an existing variable or function name directly in python source; I always use ctrl-p/ctrl-n in vim to autocomplete it. I load the other file containing the original name in a new tab if I have to, just so autocomplete can match on it. That and pyflakes help a lot. It also helps to use _lengthy_ function and variable names that are unique. However, refactoring modules and functions in a large codebase is still agony.

Opposition to Python type hints

Posted May 10, 2015 12:38 UTC (Sun) by mgedmin (guest, #34497) [Link]

flake8 helps a lot. Especially when integrated with your text editor (vim + Syntastic for me).


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds