Title: Order of clause in one-line if/else statement yields different mypy behaviors · Issue #8519 · python/mypy · GitHub
Open Graph Title: Order of clause in one-line if/else statement yields different mypy behaviors · Issue #8519 · python/mypy
X Title: Order of clause in one-line if/else statement yields different mypy behaviors · Issue #8519 · python/mypy
Description: I may have encountered a bug in Mypy's inference of types when using a one-line if/else statement: The issue appears when returning a lambda function or a standard built-in (in that case print), depending on a bool. import typing as t de...
Open Graph Description: I may have encountered a bug in Mypy's inference of types when using a one-line if/else statement: The issue appears when returning a lambda function or a standard built-in (in that case print), de...
X Description: I may have encountered a bug in Mypy's inference of types when using a one-line if/else statement: The issue appears when returning a lambda function or a standard built-in (in that case print)...
Opengraph URL: https://github.com/python/mypy/issues/8519
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Order of clause in one-line if/else statement yields different mypy behaviors","articleBody":"I may have encountered a bug in Mypy's inference of types when using a one-line if/else statement:\r\n\r\nThe issue appears when returning a lambda function or a standard built-in (in that case `print`), depending on a `bool`.\r\n\r\n```\r\nimport typing as t\r\n\r\ndef get_print_fn(quiet: bool) -> t.Callable[..., None]:\r\n \"\"\"Return the standard print function if quiet is False, else an empty function.\"\"\"\r\n return (lambda *args, **kwargs: None) if quiet else print\r\n```\r\n\r\n^^^ The above raises the following Mypy error:\r\n`Incompatible return value type (got \"function\", expected \"Callable[..., None]\")`\r\n\r\nHowever, reversing the condition like below does not show any error.\r\n\r\n```\r\nimport typing as t\r\n\r\ndef get_print_fn(quiet: bool) -> t.Callable[..., None]:\r\n \"\"\"Return the standard print function if quiet is False, else an empty function.\"\"\"\r\n return print if not quiet else lambda *args, **kwargs: None\r\n```\r\n\r\nThis somehow suggests that mypy is using the type of the `if` block to infer the return type? I'm not sure whether this is intended or indeed a bug.\r\n\r\n\r\n\r\n**Meta information**\r\n\r\n - mypy version: `0.761`\r\n - `mypy.ini`\r\n\r\n```\r\n[mypy]\r\npython_version = 3.7\r\nwarn_return_any = True\r\nwarn_unused_ignores = True\r\nwarn_unreachable = True\r\nnamespace_packages = True\r\nshow_column_numbers = True\r\nstrict_equality = True\r\nno_implicit_optional = True\r\ndisallow_untyped_defs = True\r\ndisallow_any_generics = True\r\ndisallow_untyped_calls = True\r\n```","author":{"url":"https://github.com/fgmehlin","@type":"Person","name":"fgmehlin"},"datePublished":"2020-03-09T14:02:10Z","url":"https://github.com/python/mypy/issues/8519"}
| 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:53517f44-946e-8990-ce98-da25645f211f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E344:3E1398:395F9A:4D3CE7:6B00296D |
| html-safe-nonce | c76c4cdeec6a3063e420552077172df3274fd431cead7318a7c2b5cd0ff0f5d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzQ0OjNFMTM5ODozOTVGOUE6NEQzQ0U3OjZCMDAyOTZEIiwidmlzaXRvcl9pZCI6Ijg1NzE1NTk0NTIzOTI1NjUxMDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 69678fa4075f2c7754ea08bbdb52784e1b6b448855bbc3a410544c5d096709a4 |
| hovercard-subject-tag | issue:577931369 |
| 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/8519/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d090a7448ff337355654b771b001c251b67ce10373f3005585b6245e3b4d9a3/python/mypy/issues/8519 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d090a7448ff337355654b771b001c251b67ce10373f3005585b6245e3b4d9a3/python/mypy/issues/8519 |
| og:image:alt | I may have encountered a bug in Mypy's inference of types when using a one-line if/else statement: The issue appears when returning a lambda function or a standard built-in (in that case print), de... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fgmehlin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width