{"action":"closed","issue":{"url":"https://api.github.com/repos/jab/bidict/issues/21","labels_url":"https://api.github.com/repos/jab/bidict/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/jab/bidict/issues/21/comments","events_url":"https://api.github.com/repos/jab/bidict/issues/21/events","html_url":"https://github.com/jab/bidict/issues/21","id":118664897,"number":21,"title":"Add strictbidict that throws exception on any duplicate value assignment","user":{"login":"lordmauve","id":617224,"avatar_url":"https://avatars.githubusercontent.com/u/617224?v=3","gravatar_id":"","url":"https://api.github.com/users/lordmauve","html_url":"https://github.com/lordmauve","followers_url":"https://api.github.com/users/lordmauve/followers","following_url":"https://api.github.com/users/lordmauve/following{/other_user}","gists_url":"https://api.github.com/users/lordmauve/gists{/gist_id}","starred_url":"https://api.github.com/users/lordmauve/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lordmauve/subscriptions","organizations_url":"https://api.github.com/users/lordmauve/orgs","repos_url":"https://api.github.com/users/lordmauve/repos","events_url":"https://api.github.com/users/lordmauve/events{/privacy}","received_events_url":"https://api.github.com/users/lordmauve/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-11-24T17:37:57Z","updated_at":"2015-12-21T03:16:24Z","closed_at":"2015-12-21T03:16:24Z","body":"I see considerable value preventing people accidentally creating mappings that are not invertible. In our codebase, there are several hundred instances of code like\r\n\r\n    PRODUCT_REMAP = {... hundreds of lines ...}\r\n    REVERSE_PRODUCT_REMAP = dict((v, k) for k, v in PRODUCT_REMAP.iteritems())\r\n\r\nA concern with these is that literals that were accidentally written to include duplicate values would create an incorrect inverse mapping.\r\n\r\nUsing bidict does little to improve matters. In code like that below, one of those keys will \"win\":\r\n\r\n    d = bidict({\r\n         'foo': 1,\r\n         ... hundreds of lines ...\r\n         'bar': 1\r\n    })\r\n\r\nThis has the advantage that the inverse mapping will genuinely reflect the forward mapping, but it carries the disadvantage the mapping for one of the keys is lost. In many cases this is *worse* - for example, when the forward mapping is fundamental and the inverse mapping is just informational. The only reasonable solution in cases like these is to throw an exception for the programmer to deal with.\r\n\r\nI suggest creating a \"strictbidict\" in which ``_put()`` throws a ``CollapseException`` if any value is duplicated, rather than dropping the previous key that maps to that value.\r\n\r\n<bountysource-plugin>\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/28576867-add-strictbidict-that-throws-exception-on-any-duplicate-value-assignment?utm_campaign=plugin&utm_content=tracker%2F6404484&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F6404484&utm_medium=issues&utm_source=github).\r\n</bountysource-plugin>"}}