[Python-Dev] Type hints -- a mediocre programmer's reaction

R. David Murray rdmurray at bitdance.com
Tue Apr 21 18:09:05 CEST 2015


On Wed, 22 Apr 2015 01:09:52 +1000, Chris Angelico <rosuav at gmail.com> wrote:
> def incremental_parser(input: FileLike) -> List[Token]:
>     tokens = []
>     data = ""
>     while True:
>         if not data:
>             data = input.read(64)
>         token = Token(data[0]); data = data[1:]
>         while token.wants_more():
>             token.give_more(data[0]); data = data[1:]
>         tokens.append(token)
>         if token.is_end_of_stream(): break
>     input.seek(-len(data), 1)
>     return tokens
> 
> If you were to exhaustively stipulate the requirements on the
> file-like object, you'd have to say:
> 
> * Provides a read() method which takes an integer and returns up to
> that many bytes
> * Provides a seek() method which takes two integers
> * Is capable of relative seeking by at least 63 bytes backward
> * Is open for reading
> * Etcetera
> 
> That's not the type system's job. Not in Python. Maybe in Haskell, but

Just a note that if I'm reading the high level description right,
this kind of analysis is exactly the kind of thing that Flow does
for javascript.

--David


More information about the Python-Dev mailing list