Title: Incorrent "Missing return statement" in presence of bad combination of decorated function call, definition order and NoReturn · Issue #8129 · python/mypy · GitHub
Open Graph Title: Incorrent "Missing return statement" in presence of bad combination of decorated function call, definition order and NoReturn · Issue #8129 · python/mypy
X Title: Incorrent "Missing return statement" in presence of bad combination of decorated function call, definition order and NoReturn · Issue #8129 · python/mypy
Description: Code to reproduce: from typing import Callable, NoReturn, TypeVar def no_return() -> NoReturn: pass def use_decorated_before_defined() -> int: decorated() if bool(): return 0 else: no_return() T = TypeVar('T') def wrapper(function: T) ->...
Open Graph Description: Code to reproduce: from typing import Callable, NoReturn, TypeVar def no_return() -> NoReturn: pass def use_decorated_before_defined() -> int: decorated() if bool(): return 0 else: no_return() T = ...
X Description: Code to reproduce: from typing import Callable, NoReturn, TypeVar def no_return() -> NoReturn: pass def use_decorated_before_defined() -> int: decorated() if bool(): return 0 else: no_return(...
Opengraph URL: https://github.com/python/mypy/issues/8129
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrent \"Missing return statement\" in presence of bad combination of decorated function call, definition order and NoReturn","articleBody":"Code to reproduce:\r\n\r\n```python\r\nfrom typing import Callable, NoReturn, TypeVar\r\n\r\n\r\ndef no_return() -> NoReturn:\r\n pass\r\n\r\n\r\ndef use_decorated_before_defined() -> int:\r\n decorated()\r\n if bool():\r\n return 0\r\n else:\r\n no_return()\r\n\r\n\r\nT = TypeVar('T')\r\n\r\n\r\ndef wrapper(function: T) -> T:\r\n return function\r\n\r\n\r\ndef decorator() -> Callable[[T], T]:\r\n return wrapper\r\n\r\n\r\n@decorator()\r\ndef decorated() -> None: pass\r\n\r\n\r\ndef use_decorated_after_defined() -> int:\r\n decorated()\r\n if bool():\r\n return 0\r\n else:\r\n no_return()\r\n\r\n\r\ndef use_wrapped_before_defined() -> int:\r\n wrapped()\r\n if bool():\r\n return 0\r\n else:\r\n no_return()\r\n\r\n\r\n@wrapper\r\ndef wrapped() -> None: pass\r\n```\r\n\r\nI tested this both with mypy 0.750 and one from the current master branch (526a2187bbd2da35862899e7529394a74a6f6ced), running on CPython 3.7.5. The command line and the output:\r\n\r\n```\r\n% python -m mypy repro.py\r\nrepro.py:8: error: Missing return statement\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nI'd expect no errors here. After some digging in mypy source code I believe `use_decorated_before_defined` can't be fully processed when it's type-checked (at least the first time it's entered) because of a) the call to `decorated` being looked at before `decorated` is defined and b) `decorated` being decorated (pardon my repetition) with a result of another function call which I suspect creates a difficult environment for mypy to deduce things and it defers further processing of `use_decorated_before_defined` (or at least part of it).\r\n\r\nThen, when the call to `no_return` is encountered its type is detected as `Any` because of the processing being deferred per above, hence the error.\r\n\r\nI left two similar variants of the function (`use_decorated_after_defined` and `use_wrapped_before_defined`) to help narrow things down as those don't trigger the error.","author":{"url":"https://github.com/jstasiak","@type":"Person","name":"jstasiak"},"datePublished":"2019-12-10T21:44:31Z","url":"https://github.com/python/mypy/issues/8129"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6f9c10de-fd0e-0a01-635b-d57f817a756e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A758:18020:22B4ED:2ECB1F:6B0022CB |
| html-safe-nonce | e4081820dca89bc15f6c61ae42c5b0ff7a380d5614aac143365b8c5843223315 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzU4OjE4MDIwOjIyQjRFRDoyRUNCMUY6NkIwMDIyQ0IiLCJ2aXNpdG9yX2lkIjoiNzU4NjgyMzc4NjE1MDc2NTI2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2fcb26c92b4bd91400d1342937f0207bd3c87fc25ca0f4f50d4ef650ab29c474 |
| hovercard-subject-tag | issue:536011841 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/8129/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b65bd4ebbd214126db9435e1617fff9b11089a9cb7ac59143deddec51977231b/python/mypy/issues/8129 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b65bd4ebbd214126db9435e1617fff9b11089a9cb7ac59143deddec51977231b/python/mypy/issues/8129 |
| og:image:alt | Code to reproduce: from typing import Callable, NoReturn, TypeVar def no_return() -> NoReturn: pass def use_decorated_before_defined() -> int: decorated() if bool(): return 0 else: no_return() T = ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jstasiak |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width