Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'heavily': 0.04; 'source,': 0.04; 'static': 0.04; 'api.': 0.05; 'subject:Python': 0.06; '-*-': 0.07; 'diff': 0.07; 'modify': 0.07; 'suppose': 0.07; 'utf-8': 0.07; '"if': 0.09; 'arguments': 0.09; 'coding:': 0.09; 'feature,': 0.09; 'fixed,': 0.09; 'try:': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; '3.0)': 0.16; 'arg2,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'ides': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'param': 0.16; 'received:192.168.1.4': 0.16; 'skip:{ 30': 0.16; 'str,': 0.16; 'supplied,': 0.16; 'types,': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'thu,': 0.19; 'meant': 0.20; 'example': 0.22; 'import': 0.22; 'putting': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; "aren't": 0.24; 'skip:i 40': 0.24; '(or': 0.24; "i've": 0.25; 'script': 0.25; 'changes,': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'generally': 0.29; 'thus': 0.29; 'code': 0.31; 'lines': 0.31; 'assert': 0.31; 'inspect': 0.31; 'int,': 0.31; 'keyerror:': 0.31; 'allows': 0.31; 'probably': 0.32; 'run': 0.32; 'worked': 0.33; 'running': 0.33; 'cases': 0.33; 'implemented': 0.33; 'could': 0.34; 'info': 0.35; 'except': 0.35; 'but': 0.35; 'done': 0.36; 'expected': 0.38; 'generic': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'most': 0.60; 'skip:t 30': 0.61; 'providing': 0.61; 'simple': 0.61; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'hints': 0.68; 'union': 0.69; 'subject:! ': 0.74; 'manner.': 0.74; '2015': 0.84; 'imagine': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=A/HiPsmG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=Vhvw94NMJWsA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=5ANokUeuAAAA:8 a=Lpd3P4sOn0P_6ZRDd6sA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Fri, 30 Jan 2015 02:11:40 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python is DOOMED! Again! References: <54c07d04$0$13012$c3e8da3$5496439d@news.astraweb.com> <5Vyyw.472566$me1.406185@fx23.am4> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422583904 news.xs4all.nl 2837 [2001:888:2000:d::a6]:56439 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84860 On 2015-01-29 23:25, Chris Kaynor wrote: > On Thu, Jan 29, 2015 at 2:57 PM, BartC wrote: [snip] >> Putting in hints, (as as I implemented them using primitive types), >> meant that functions and code no longer worked in a generic (or >> polymorphic) manner. Code also changes, but the type hints aren't >> maintained. I understand the Python proposal allows type hints to >> be a union of expected types, but that sounds complicated. > > Regarding the maintenance of type-hints, for people who heavily use > them, I would imagine they will have a static checker setup which > will regularly run and generally produce an error if the hints are > not updated. Most other people will likely only lightly use the > type-hints and may not use static checkers, and thus they probably > will get out of sync. With such a feature, my main use case would be > to aid IDEs in providing auto-complete, which I've done in the past > by adding lines like "if 0: assert isinstance(variable, type)". Most > of the functions I write do not have any such "hints" but instead > I've only generally used it in cases where the type is pretty much > fixed, but is a custom type with a more complicated API. > I suppose you could check what types the arguments are by running the code and outputting the types supplied, and then run a script that uses the info to modify the source, and then you can diff the result. Here's a simple example I've come up with: #! python3.4 # -*- coding: utf-8 -*- import inspect def check_hints(func): def wrapper(*args, **kwargs): sig = inspect.signature(func) print('file :', inspect.getfile(func)) print('function :', func.__name__) print('line :', inspect.getsourcelines(func)[1] + 1) args_given = list(args) kwargs_given = dict(kwargs) for name in sig.parameters: param = sig.parameters[name] if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD: annotation = param.annotation if args_given: print('parameter : {} : {}'.format(name, type(args_given.pop(0)).__name__)) if annotation != inspect.Parameter.empty: print('annotation: {} : {}'.format(name, annotation.__name__)) else: try: print('parameter : {} : {}'.format(name, type(kwargs_given.pop(name)).__name__)) if annotation != inspect.Parameter.empty: print('annotation: {} : {}'.format(name, annotation.__name__)) except KeyError: pass result = func(*args, **kwargs) print('return : {}'.format(type(result).__name__)) annotation = sig.return_annotation if annotation != inspect.Parameter.empty: print('annotation: {}'.format(annotation.__name__)) print() return result return wrapper @check_hints def foo(arg1: int, arg2: str, arg3: float=2.0) -> int: pass @check_hints def bar(arg1, arg2, arg3=2.0): pass foo(1, "bar") foo("baz", 2, 3.0) bar(1, "bar") bar("baz", 2, 3.0)