[Python-Dev] Type hints -- a mediocre programmer's reaction
Chris Angelico
rosuav at gmail.com
Tue Apr 21 16:31:48 CEST 2015
On Tue, Apr 21, 2015 at 7:56 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
> If people constantly get told by their editor / IDE that they are calling
> function with the wrong argument types, what are they going to do? They may
> start adopting the same approach as in Java / C++ etc... where interfaces
> must be explicitly defined and the practice of duck typing may become
> forgotten because discouraged by the tools programmers use.
Style guides should generally be recommending the use of concrete
types for return values, but abstract types for parameters - you might
well have a function that returns a list, but most functions that
accept lists are really accepting sequences. Granted, there are some
vague areas - how many functions take a "file-like object", and are
they all the same? - but between MyPy types and the abstract base
types that already exist, there are plenty of ways to formalize duck
typing. And frankly, even with the uncertainties, I'd still rather
have a function declared as taking a "file-like object" than "an
object with a .read() method that takes an integer and returns up to
that many bytes of data, and a .seek() method that blah blah blah
blah". Sometimes, the precision is completely useless. What an editor
would do with the hint that a function takes a file-like object I'm
not sure, but if it issues a complaint because I'm passing it
something that doesn't have a writelines() method, it's the fault of
the editor, not the type hint.
ChrisA
More information about the Python-Dev
mailing list