[Python-ideas] TypeHinting: From variable name to type
Anders Hovmöller
boxed at killingar.net
Fri Oct 19 03:29:38 EDT 2018
> On 19 Oct 2018, at 09:18, Thomas Güttler <guettliml at thomas-guettler.de> wrote:
>
> Imaging you are developing in the django context.
>
> Everytime you use a variable named "request" or "response" your human brains
> knows that this is a subclass of django.http.HttpRequest and django.http.HttpResponse.
>
> How to give the IDE this knowledge?
>
> One solution is the do typehinting everywhere the veriable gets used.
>
> But why does the human brain not need this?
>
> Because it is intelligent?
>
> I would not call this intelligence. There is a simple
> dictionary in the brain of the developer, which maps:
>
> variable-name --> Type
>
> And this mapping dict exists once per library.
>
> If you are developing in the requests http lib, then
> there is a different mapping. Then "response" means
> type requests.Response.
>
>
> Now my idea: Per module and/or per file type hinting from variable name.
>
> Maybe a magic docstring in the __init__.py file:
>
> """
> variable-name-mapping:
> {
> request: django.http.HttpRequest,
> ...
> }
> """
>
> This makes the type mapping available for all files in this directory (or sub-directories).
>
> What do you think?
I tried to implement this in mypy quite recently actually, but gave up for various reasons.
I am very much +1 on this. This would be a huge boon to preexisting code bases.
/ Anders
More information about the Python-ideas
mailing list