{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/7913","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/7913/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/7913/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/7913/events","html_url":"https://github.com/numpy/numpy/issues/7913","id":169767318,"number":7913,"title":"np.bool_ causes insidious behavior; some suggestions","user":{"login":"yingzong","id":11035556,"avatar_url":"https://avatars.githubusercontent.com/u/11035556?v=3","gravatar_id":"","url":"https://api.github.com/users/yingzong","html_url":"https://github.com/yingzong","followers_url":"https://api.github.com/users/yingzong/followers","following_url":"https://api.github.com/users/yingzong/following{/other_user}","gists_url":"https://api.github.com/users/yingzong/gists{/gist_id}","starred_url":"https://api.github.com/users/yingzong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yingzong/subscriptions","organizations_url":"https://api.github.com/users/yingzong/orgs","repos_url":"https://api.github.com/users/yingzong/repos","events_url":"https://api.github.com/users/yingzong/events{/privacy}","received_events_url":"https://api.github.com/users/yingzong/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-06T21:27:46Z","updated_at":"2016-08-06T23:07:05Z","closed_at":null,"body":"Example:\r\n\r\n```\r\nIn [1]: import numpy as np\r\n\r\nIn [2]: a = np.int64(1)\r\n\r\nIn [3]: (a > 0) is True\r\nOut[3]: False\r\n```\r\n\r\nObviously I understand the reason is because `is` is doing type comparison and the result of `(a > 0)` is still type `numpy.bool_`. However, for scalar bools, this is really annoying bordering on mistake-inviting due to the fact that `is` and `==` are almost idiomatically interchangeable for `bool`s in native Python (again, I understand the difference).\r\n\r\nThere are also odd promotion rules, to wit:\r\n\r\n```\r\nIn [4]: b = np.bool_(True)\r\n\r\nIn [5]: type(b and True)\r\nOut[5]: bool\r\n\r\nIn [6]: type(True and b)\r\nOut[6]: numpy.bool_\r\n\r\nIn [7]: type(b or True)\r\nOut[7]: numpy.bool_\r\n\r\nIn [8]: type(True or b)\r\nOut[8]: bool\r\n\r\nIn [9]: type(not b)\r\nOut[9]: bool\r\n\r\nIn [10]: type(~b)\r\nOut[10]: numpy.bool_\r\n```\r\n\r\nIt appears `__and__` is doing some kind of inverted `__or__`. Once more, this is understandable, but it introduces many insidious bugs in existing code that already depends on the assumption that `bool`s in Python work a certain way with operators.\r\n\r\nNow, suggestion. One possibility, short of simply converting scalar `numpy.bool_` to Python `bool` is at least for operators like `__and__`, `__or__` whose other operand is a Python `bool` to return always a Python `bool`. That would at least isolate the \"problem\" of unexpected behavior to segments of the code where only numpy code is, and therefore visible to the writer, instead of a case where a library writer may return a `numpy.bool_` and a library user may use only the return value and Python `bool`s, neither knowing there is some hidden interaction. May I also suggest, so long as they are different, that `__repr__` for `numpy.bool_` return something like `true` and `false` rather than masquerading as internal `True` and `False`?"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/238053630","html_url":"https://github.com/numpy/numpy/issues/7913#issuecomment-238053630","issue_url":"https://api.github.com/repos/numpy/numpy/issues/7913","id":238053630,"user":{"login":"pv","id":35046,"avatar_url":"https://avatars.githubusercontent.com/u/35046?v=3","gravatar_id":"","url":"https://api.github.com/users/pv","html_url":"https://github.com/pv","followers_url":"https://api.github.com/users/pv/followers","following_url":"https://api.github.com/users/pv/following{/other_user}","gists_url":"https://api.github.com/users/pv/gists{/gist_id}","starred_url":"https://api.github.com/users/pv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pv/subscriptions","organizations_url":"https://api.github.com/users/pv/orgs","repos_url":"https://api.github.com/users/pv/repos","events_url":"https://api.github.com/users/pv/events{/privacy}","received_events_url":"https://api.github.com/users/pv/received_events","type":"User","site_admin":false},"created_at":"2016-08-06T23:07:05Z","updated_at":"2016-08-06T23:07:05Z","body":"What you suggest is not possible in Python.\nNote also that `foo is True` is discouraged by PEP 8.\n"}}
{"actor":{"display_login":"pv"}}