Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'pycon': 0.01; 'static': 0.04; 'subject:Python': 0.06; 'pypy': 0.07; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '24,': 0.16; 'ast': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'int):': 0.16; 'types,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'module,': 0.24; 'parse': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'clever': 0.31; "d'aprano": 0.31; 'really,': 0.31; 'steven': 0.31; 'types.': 0.31; 'themselves': 0.32; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'too': 0.37; 'clear': 0.37; 'checks': 0.38; 'requiring': 0.38; 'pm,': 0.38; 'skip:b 40': 0.39; 'skip:o 30': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'worth': 0.66; 'analysis': 0.75; '2015': 0.84; '4:29': 0.84; 'subject:Solution': 0.84; 'subject:Proposal': 0.91; 'this;': 0.91; 'watched': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=J0Alm3rUAJRHmqGDeDJa0PDqNUtCdIxVtPusSHJODqc=; b=HrRl3THin71czPnJx20mUS5mCMMO2nlXmbPqfEH6eGKmuYf2pj0txudP3lXy7ohcYG jgF/qxuoilRNdeOguYXBiQ/3F/MwV7zQlv+wPvEehR4lcBbDNuqRCCkbMuRwEuuucXGc 4dVOT68KfhRYnBBaX7IusPXdIMj7Bign0bHBrdgiAAet5n/sY/qqf5uoy3l7ABkxAQJD +WDhtnwnt3YOUMAdNZJGrfSy67ZKNbYIhEIM26zfDQqoo/RsF6BMKpal7P7ZRr7v8TEz 0gfrDqGPGmr+k7sPAuoqPtOQH2xhPKh6cBDHUMqWHIEKaIN9/NHZWEsoQbgOIdkRNNeC cflg== MIME-Version: 1.0 X-Received: by 10.224.128.196 with SMTP id l4mr21921493qas.100.1422077929013; Fri, 23 Jan 2015 21:38:49 -0800 (PST) In-Reply-To: <54c32dae$0$13000$c3e8da3$5496439d@news.astraweb.com> References: <6da1eb58-a0bb-4d37-8293-0a8cafe6a89c@googlegroups.com> <5afad59b-5e8c-4821-85cf-9e971c8c7be6@googlegroups.com> <4b3b498a-c9b0-443d-8514-87ccd8e98f43@googlegroups.com> <87sif1dpgy.fsf@elektro.pacujo.net> <54c32dae$0$13000$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 24 Jan 2015 16:38:48 +1100 Subject: Re: Python Sanity Proposal: Type Hinting Solution From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422077931 news.xs4all.nl 2921 [2001:888:2000:d::a6]:59553 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84437 On Sat, Jan 24, 2015 at 4:29 PM, Steven D'Aprano wrote: > Sufficiently clever type-checkers may use assertions to infer types, but > requiring this is a non-starter. Or they could use the isinstance checks themselves to infer types. def func(arg): if not isinstance(arg, int): raise ValueError("Expected an integer") It's fairly straight-forward to do an AST parse on a module, iterate over it for functions, and then look for a specific pattern like the above: If(test=UnaryOp(op=Not(), operand=Call(func=Name(id='isinstance', ctx=Load()), args=[Name(id='arg', ctx=Load()), Name(id='int', ctx=Load())], keywords=[], starargs=None, kwargs=None)), body=[Raise(exc=Call(func=Name(id='ValueError', ctx=Load()), args=[Str(s='Expected an integer')], keywords=[], starargs=None, kwargs=None), cause=None)], orelse=[]) ... okay, that's not exactly pretty, but it's still a straight-forward structure. But having just watched a PyCon talk about PyPy and RPython and type inference, it's pretty clear that static analysis can do a *lot* more than this; so really, looking for isinstance checks is both too hard and too simplistic to be really worth doing. Type hinting is a different beast. ChrisA