Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: __all__ attribute: bug and proposal Date: Tue, 28 Jun 2016 08:41:16 +1000 Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de us8b+9jK1Ul1LyaNEfUPGgvXpDE+1z9l0eBSwGkLnX9Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:bug': 0.05; 'objects,': 0.07; 'cc:addr:python-list': 0.09; '__all__': 0.09; 'integers': 0.09; 'pavel': 0.09; 'types:': 0.09; 'x),': 0.09; 'def': 0.13; 'importing': 0.15; '2016': 0.16; 'enough.': 0.16; 'example?': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sure.': 0.16; 'vary.': 0.16; 'wrote:': 0.16; 'attribute': 0.18; "shouldn't": 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '"",': 0.22; 'names.': 0.22; 'am,': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'message- id:@mail.gmail.com': 0.27; 'traceback': 0.33; "i'll": 0.33; "skip:' 20": 0.34; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'skip:c 30': 0.35; 'text': 0.35; 'unit': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'skip:{ 10': 0.36; 'subject:: ': 0.37; 'names': 0.38; 'skip:o 20': 0.38; 'test': 0.39; 'provide': 0.61; 'matter': 0.63; 'wall': 0.63; 'results': 0.66; '(your': 0.84; 'chrisa': 0.84; 'to:none': 0.91; 'canonical': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=PiB6PyYWNCO8fVKX6bZUVP5h4uBpT0uvLhoa4FFEMR0=; b=Wp7iK6lctoCqBDfPEH/LMVWdKNTWRwAwxqiBVkOGCB+dZB+vu76uOh1ajYnjSvk6ZQ qY5NpIClQ0KK2XTFwhW00zkRCKsakv133G3BneM3Sn+m2EdhkY49Bj7a8VfpVRg+3gBR +FKhRpjrU/ECCDkHU8ADXQXOoRfX4Hm9WgJT9S/06gbQDhFwwKsWPNpMVhI3gSFcbHmh ja3vlEifpBHWcymy2r6mTQkVMh3RDJ+6hb+mglBqnLRivUKCTpCCodlpw6qbfUw0G8UP ye0O3uj4RkI3541Azvj0iFVJ7BGejhAUJWt03zbnFzp00V2f9TgT8A54linRd6vfMqab EBnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=PiB6PyYWNCO8fVKX6bZUVP5h4uBpT0uvLhoa4FFEMR0=; b=hKXmNAokWClvqHpT4FhPa6z3g5WoeXxeohTOOPzYADkIbIoOZGA6e4+4X5l6kxqbvd LKz29IpMEwAq43baekXDlUWmtMJDU7uMYmhQRhZ/JQGQW5IdEuinuWUuCRZyCf8/u68M qDhPWr3IaWGZFvaz0IiR2Debz+61sQNaLD6w6EOyOwPd3uVQ0P7T3YaNDGWuM8WlZfsR s5Ec7ARfctO+ZmQpOk9Y3k6xwR4qxSy/FjuHFoPdeZUB94EDDPDQm9K2HY86rjdj97ZO KIDAZZXQjqi8+bRcas/wBmON7OpPjorH4hLx1GpjurqQJaOmYnGsrpVbCUxQhRp9/mY1 VX4g== X-Gm-Message-State: ALyK8tLduaEmF1b3VNc8aUC1C9+KIZ+FQu1yHQqC1dNdep3Wl+/hPEUzGPq1uDAwtM/1WrD0HWB3ahBXC53zhQ== X-Received: by 10.194.70.41 with SMTP id j9mr2895801wju.30.1467067277307; Mon, 27 Jun 2016 15:41:17 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:110614 On Tue, Jun 28, 2016 at 8:29 AM, Pavel S wrote: >> but what about integers or strings? > > Can you provide example? Sure. I'll spare you the wall of text that is os.__all__, but here's the types: >>> import os >>> {type(getattr(os, x)).__name__ for x in os.__all__} {'bool', 'module', 'function', 'str', '_Environ', 'NoneType', 'type', 'int', 'dict', 'builtin_function_or_method'} >>> collections.Counter(hasattr(getattr(os, x), "__name__") for x in os.__all__) Counter({True: 184, False: 122}) So, roughly 60% of them even have canonical names. And you don't always want the canonical name, anyway: >>> os.path.__name__ 'posixpath' (Your results may vary. Specifically. os.path.__name__ will be 'ntpath' if you're on Windows.) > No matter if __all__ uses names or objects, I think it should be validated not only when importing '*', but always. > > Frankly, do you always unit-test if __all__ works? Frankly, I don't unit test enough. But if you have __all__, it shouldn't be too hard, nor too unobvious, to test it. >>> def test_all(): ... exec("from os import *") ... >>> test_all() >>> os.__all__.append("asdf") >>> test_all() Traceback (most recent call last): File "", line 1, in File "", line 2, in test_all File "", line 1, in AttributeError: module 'os' has no attribute 'asdf' ChrisA