Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98939

PEP 484 stubs with generic types

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject PEP 484 stubs with generic types
Date Tue, 17 Nov 2015 10:49:00 -0700
Lines 32
Message-ID <mailman.400.1447782587.16136.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de ziyEV2tY3w+IcvTkXOrILQiwYKRPzeZpkgNwtlAY4xLg==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'static': 0.03; 'repository': 0.05; 'defines': 0.07; 'subject:PEP': 0.07; 'type:': 0.09; 'typeerror:': 0.09; 'url:github': 0.09; 'python': 0.10; 'def': 0.13; '"should': 0.16; 'invalid.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'runtime.': 0.16; 'stub': 0.16; 'creates': 0.18; 'meant': 0.22; 'code.': 0.23; 'bit': 0.23; 'errors': 0.23; 'import': 0.24; 'module': 0.25; "doesn't": 0.26; 'skip:" 20': 0.26; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'fine': 0.28; 'pep': 0.29; "can't": 0.32; 'problem': 0.33; 'url:python': 0.33; 'avoiding': 0.33; 'reference,': 0.33; 'running': 0.34; 'advice': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'generic': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'still': 0.40; 'future': 0.60; 'url:3': 0.60; 'real': 0.62; 'forward': 0.66; 'url:4': 0.70; 'evaluate': 0.72; 'case?': 0.84; 'fortunately,': 0.84; 'to:name:python': 0.84; 'url:master': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=4LM0vgRT2KsEhKJSXJF8zX4fZcpj+VW3CscGGfQw/Ow=; b=n+IZAYEWQbvo7X+PlqW7nBPbi/dzOE+Ku5hpHKXXIp3Tf1bn98W9IGjk2sUybtNywi 3x12EqUnKVNEqn557g23zL/LTSKN7NYNII+P0vdjkAYOOQUWrYaFYGZ5MtRrNbX+8aS0 Be3bpqX3plzMDlaRW09CZ0qkuV00qAPrVBNRmDY+Vs2rz0R5cZsuJl/eaIj7mpjUdLl6 8t0ii6b01KbsSH/4rUYz5q0J98EwnsBckzoJeeTRDAohEnqpZyHE+sqoeRa1VrT0WR2u Js/IUN8n/dD3zBWQnS0BY8mnYxlNMIBIxihF608GcE4uQRlKUMQvUOEu/0Z9fgrCo/Br l6+A==
X-Received by 10.107.19.12 with SMTP id b12mr45164949ioj.11.1447782579467; Tue, 17 Nov 2015 09:49:39 -0800 (PST)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98939

Show key headers only | View raw


Playing around a bit with PEP 484, I annotated a function that returns
an asyncio.Future:

import asyncio

def get_future() -> asyncio.Future[int]:
    future = asyncio.Future()
    future.set_result(42)
    return future

The problem with this is that in Python 3.5, asyncio.Future can't be
used as a generic type. Fortunately, the typeshed repository provides
a stub that defines it as a generic type:

https://github.com/python/typeshed/blob/master/stdlib/3.4/asyncio/futures.pyi

This is fine for running a static type checker, but it still creates a
problem when actually running the code. The real asyncio.Future
doesn't support indexing, and so the annotation causes a TypeError at
runtime.

I could write the annotation as a forward reference, avoiding the TypeError:

def get_future() -> "asyncio.Future[int]":
    ...

But PEP 484 stipulates that a forward reference "should evaluate
without errors once the module has been fully loaded", so this is
invalid.

Is there any advice for this case? How are generic types meant to be
used with stub files?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

PEP 484 stubs with generic types Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-17 10:49 -0700

csiph-web