{"action":"created","issue":{"url":"https://api.github.com/repos/python/mypy/issues/2343","repository_url":"https://api.github.com/repos/python/mypy","labels_url":"https://api.github.com/repos/python/mypy/issues/2343/labels{/name}","comments_url":"https://api.github.com/repos/python/mypy/issues/2343/comments","events_url":"https://api.github.com/repos/python/mypy/issues/2343/events","html_url":"https://github.com/python/mypy/issues/2343","id":185585636,"number":2343,"title":"Variable assignment type doesn't support Union","user":{"login":"GPHemsley","id":376064,"avatar_url":"https://avatars.githubusercontent.com/u/376064?v=3","gravatar_id":"","url":"https://api.github.com/users/GPHemsley","html_url":"https://github.com/GPHemsley","followers_url":"https://api.github.com/users/GPHemsley/followers","following_url":"https://api.github.com/users/GPHemsley/following{/other_user}","gists_url":"https://api.github.com/users/GPHemsley/gists{/gist_id}","starred_url":"https://api.github.com/users/GPHemsley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GPHemsley/subscriptions","organizations_url":"https://api.github.com/users/GPHemsley/orgs","repos_url":"https://api.github.com/users/GPHemsley/repos","events_url":"https://api.github.com/users/GPHemsley/events{/privacy}","received_events_url":"https://api.github.com/users/GPHemsley/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-27T06:43:02Z","updated_at":"2016-10-27T09:12:08Z","closed_at":null,"body":"```python\r\n#!/usr/bin/env python3\r\n\r\nimport decimal\r\nimport typing\r\n\r\ndef foo(x: decimal.Decimal) -> typing.Union[decimal.Decimal, float]:\r\n\tif x == 0:\r\n\t\treturn -0.0\r\n\r\n\treturn decimal.Decimal(x)\r\n\r\ndef bar(y: bool) -> None:\r\n    if y:\r\n        z = decimal.Decimal()\r\n    else:\r\n        z = foo(decimal.Decimal())\r\n```\r\n\r\nThis results in the error:\r\n> Incompatible types in assignment (expression has type \"Union[Decimal, float]\", variable has type \"Decimal\")\r\n"},"comment":{"url":"https://api.github.com/repos/python/mypy/issues/comments/256588948","html_url":"https://github.com/python/mypy/issues/2343#issuecomment-256588948","issue_url":"https://api.github.com/repos/python/mypy/issues/2343","id":256588948,"user":{"login":"JukkaL","id":1107911,"avatar_url":"https://avatars.githubusercontent.com/u/1107911?v=3","gravatar_id":"","url":"https://api.github.com/users/JukkaL","html_url":"https://github.com/JukkaL","followers_url":"https://api.github.com/users/JukkaL/followers","following_url":"https://api.github.com/users/JukkaL/following{/other_user}","gists_url":"https://api.github.com/users/JukkaL/gists{/gist_id}","starred_url":"https://api.github.com/users/JukkaL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JukkaL/subscriptions","organizations_url":"https://api.github.com/users/JukkaL/orgs","repos_url":"https://api.github.com/users/JukkaL/repos","events_url":"https://api.github.com/users/JukkaL/events{/privacy}","received_events_url":"https://api.github.com/users/JukkaL/received_events","type":"User","site_admin":false},"created_at":"2016-10-27T09:12:08Z","updated_at":"2016-10-27T09:12:08Z","body":"Mypy infers the type of a variable from the first assignment (except for a few special cases). You can work this around by adding a type annotation:\r\n\r\n```\r\n    if y:\r\n        z = decimal.Decimal()  # type: typing.Union[decimal.Decimal, float]\r\n    else:\r\n        z = foo(decimal.Decimal())\r\n```\r\n\r\nAlternatively, you can negate the condition of the if statement, and mypy picks the variable type from the union-type initializer:\r\n\r\n```\r\n    if not y:\r\n        z = foo(decimal.Decimal())\r\n    else:\r\n        z = decimal.Decimal()\r\n```\r\n"}}
{"actor":{"display_login":"JukkaL"}}