{"action":"opened","issue":{"url":"https://api.github.com/repos/python/mypy/issues/5851","repository_url":"https://api.github.com/repos/python/mypy","labels_url":"https://api.github.com/repos/python/mypy/issues/5851/labels{/name}","comments_url":"https://api.github.com/repos/python/mypy/issues/5851/comments","events_url":"https://api.github.com/repos/python/mypy/issues/5851/events","html_url":"https://github.com/python/mypy/issues/5851","id":374825417,"node_id":"MDU6SXNzdWUzNzQ4MjU0MTc=","number":5851,"title":"Assignment to an instance backed by a descriptor doesn't discard optional","user":{"login":"gvanrossum","id":2894642,"node_id":"MDQ6VXNlcjI4OTQ2NDI=","avatar_url":"https://avatars3.githubusercontent.com/u/2894642?v=4","gravatar_id":"","url":"https://api.github.com/users/gvanrossum","html_url":"https://github.com/gvanrossum","followers_url":"https://api.github.com/users/gvanrossum/followers","following_url":"https://api.github.com/users/gvanrossum/following{/other_user}","gists_url":"https://api.github.com/users/gvanrossum/gists{/gist_id}","starred_url":"https://api.github.com/users/gvanrossum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gvanrossum/subscriptions","organizations_url":"https://api.github.com/users/gvanrossum/orgs","repos_url":"https://api.github.com/users/gvanrossum/repos","events_url":"https://api.github.com/users/gvanrossum/events{/privacy}","received_events_url":"https://api.github.com/users/gvanrossum/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-29T01:28:56Z","updated_at":"2018-10-29T01:28:56Z","closed_at":null,"author_association":"MEMBER","body":"Universal example:\r\n```py\r\nfrom typing import Type, TypeVar, Optional\r\nT = TypeVar('T')\r\n\r\nclass IntDescr:\r\n  def __get__(self, obj: T, typ: Type[T]) -> Optional[int]: ...\r\n  def __set__(self, obj: T, value: Optional[int]) -> None: ...\r\n\r\nclass C:\r\n  reg = 0\r\n  spec = IntDescr()\r\n\r\n  def meth_reg(self) -> int:\r\n    if self.reg is None:\r\n      self.reg = 0\r\n    return self.reg    # OK\r\n\r\n  def meth_spec(self) -> int:\r\n    if self.spec is None:\r\n      self.spec = 0\r\n    # assert self.spec is not None\r\n    return self.spec    # Error: Incompatible return value type (got \"Optional[int]\", expected \"int\")\r\n```\r\nIf I uncomment the `assert` on the line before the error, the error goes away. I suspect that something is missing for assignment to descriptors. Or perhaps this is intentional, because we can't be *sure* that `__set__` doesn't just throw the value away? Then the assert would be an unfortunate workaround needed 99% of the time."}}
{"actor":{"display_login":"gvanrossum"}}