Title: Type of conditional expression is object when using Protocol · Issue #11722 · python/mypy · GitHub
Open Graph Title: Type of conditional expression is object when using Protocol · Issue #11722 · python/mypy
X Title: Type of conditional expression is object when using Protocol · Issue #11722 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type of conditional expression is object when using Protocol","articleBody":"Mypy does not infer that a conditional expression returns a Protocol. Here is the code:\r\n\r\n from typing import Protocol\r\n\r\n\r\n class Shape(Protocol):\r\n def area(self) -> str:\r\n ...\r\n\r\n\r\n class Circle:\r\n def area(self) -> str:\r\n return 'π * r²'\r\n\r\n\r\n class Square:\r\n def area(self) -> str:\r\n return 'w * l'\r\n\r\n\r\n class ShapeFactory:\r\n def create(self, shape_type: str) -> Shape:\r\n return Circle() if shape_type == 'circle' else Square() # Here\r\n\r\nError message:\r\n> error: Incompatible return value type (got \"object\", expected \"Shape\")\r\n\r\nBut if I change it to an conditional statement it works perfectly:\r\n\r\n # ... code omitted\r\n\r\n class ShapeFactory:\r\n def create(self, shape_type: str) -> Shape:\r\n if shape_type == 'circle':\r\n return Circle()\r\n else:\r\n return Square()\r\n\r\n","author":{"url":"https://github.com/carlosporta","@type":"Person","name":"carlosporta"},"datePublished":"2021-12-12T20:29:27Z","url":"https://github.com/python/mypy/issues/11722"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:a050a8de-6a72-a44c-aabf-1e193b96f582 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DF2:9E1B2:2D441F1:3BBC20B:6B001D87 |
| html-safe-nonce | 2e76a6fe303b233fa38afcc78ce9759f3b6a2b0b73b557f3155690a3a16a9977 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REYyOjlFMUIyOjJENDQxRjE6M0JCQzIwQjo2QjAwMUQ4NyIsInZpc2l0b3JfaWQiOiI0OTU5Njk1OTI0NDUzNDQ5MDk1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 21a3bcb43d1d110db3bb6fdd41d8eaf117329a76a0b641a8ccbf3e1f0590911a |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/11722 |
| twitter:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-cache |
| 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