{"action":"created","issue":{"url":"https://api.github.com/repos/python/mypy/issues/1472","repository_url":"https://api.github.com/repos/python/mypy","labels_url":"https://api.github.com/repos/python/mypy/issues/1472/labels{/name}","comments_url":"https://api.github.com/repos/python/mypy/issues/1472/comments","events_url":"https://api.github.com/repos/python/mypy/issues/1472/events","html_url":"https://github.com/python/mypy/issues/1472","id":152854361,"number":1472,"title":"dict.get won't accept Union type","user":{"login":"ddfisher","id":245905,"avatar_url":"https://avatars.githubusercontent.com/u/245905?v=3","gravatar_id":"","url":"https://api.github.com/users/ddfisher","html_url":"https://github.com/ddfisher","followers_url":"https://api.github.com/users/ddfisher/followers","following_url":"https://api.github.com/users/ddfisher/following{/other_user}","gists_url":"https://api.github.com/users/ddfisher/gists{/gist_id}","starred_url":"https://api.github.com/users/ddfisher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ddfisher/subscriptions","organizations_url":"https://api.github.com/users/ddfisher/orgs","repos_url":"https://api.github.com/users/ddfisher/repos","events_url":"https://api.github.com/users/ddfisher/events{/privacy}","received_events_url":"https://api.github.com/users/ddfisher/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/python/mypy/labels/needs%20discussion","name":"needs discussion","color":"bfdadc"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-05-03T19:23:58Z","updated_at":"2016-05-09T03:27:56Z","closed_at":null,"body":"Passing an overly-general Union to dict.get, like this:\r\n```python\r\nfrom typing import Union\r\n\r\nx = 5  # type: Union[int, str]\r\n{ 1: 1 }.get(x, 0)\r\n```\r\nresults in this error:\r\n```\r\ntest.py:4: error: Argument 1 to \"get\" of \"dict\" has incompatible type \"Union[int, str]\"; expected \"int\"\r\n```\r\n\r\nArguably, this should be allowed, because `get` provides a default if the item is not found (unlike `__getitem__`).  But by that logic, potentially *any* type should be a valid argument to `get`, which doesn't seem right.\r\n@gvanrossum: further thoughts?"},"comment":{"url":"https://api.github.com/repos/python/mypy/issues/comments/217769084","html_url":"https://github.com/python/mypy/issues/1472#issuecomment-217769084","issue_url":"https://api.github.com/repos/python/mypy/issues/1472","id":217769084,"user":{"login":"rwbarton","id":22628,"avatar_url":"https://avatars.githubusercontent.com/u/22628?v=3","gravatar_id":"","url":"https://api.github.com/users/rwbarton","html_url":"https://github.com/rwbarton","followers_url":"https://api.github.com/users/rwbarton/followers","following_url":"https://api.github.com/users/rwbarton/following{/other_user}","gists_url":"https://api.github.com/users/rwbarton/gists{/gist_id}","starred_url":"https://api.github.com/users/rwbarton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwbarton/subscriptions","organizations_url":"https://api.github.com/users/rwbarton/orgs","repos_url":"https://api.github.com/users/rwbarton/repos","events_url":"https://api.github.com/users/rwbarton/events{/privacy}","received_events_url":"https://api.github.com/users/rwbarton/received_events","type":"User","site_admin":false},"created_at":"2016-05-09T03:27:56Z","updated_at":"2016-05-09T03:27:56Z","body":"That's not entirely a coincidence, and may reflect my biases more than anything else. It's really just the constraints part though, and not type classes themselves (class constraints are one possible form of constraint, but there are others such as type equality or subtyping constraints). I'm not proposing to allow the user to define their own type classes or instances.\r\n\r\nWe could also reverse my last comment, and turn the constraint into an attribute of one of the type variables involved, which fits better with existing mechanisms.\r\n```\r\n_KTe = TypeVar('_KTe', equatable=_KT)\r\nclass dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):\r\n    def get(self, k: _KTe, default: _VT = None) -> _VT: ...\r\n```\r\nThe intended meaning of `equatable` is: `_KTe` can only take on values that are \"equatable\" with `_KT` (which is expected to be in scope at use site of `_KTe`). \"Equatable\" could mean anything we like, but let's say it means that the types `_KT` and `_KTe` overlap. This is similar to `bound`, although for this use case we need to allow the argument to be another type variable, which `bound` does not support."}}