Title: Handling mixins that reference fields that they do not contain · Issue #5837 · python/mypy · GitHub
Open Graph Title: Handling mixins that reference fields that they do not contain · Issue #5837 · python/mypy
X Title: Handling mixins that reference fields that they do not contain · Issue #5837 · python/mypy
Description: This is more of a question or a feature request if there's not a good way to do it. I'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating the code to Python 3. I run into code that looks l...
Open Graph Description: This is more of a question or a feature request if there's not a good way to do it. I'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating the code t...
X Description: This is more of a question or a feature request if there's not a good way to do it. I'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating th...
Opengraph URL: https://github.com/python/mypy/issues/5837
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Handling mixins that reference fields that they do not contain","articleBody":"This is more of a question or a feature request if there's not a good way to do it.\r\n\r\nI'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating the code to Python 3.\r\n\r\nI run into code that looks like this:\r\n```python\r\n#! /usr/bin/env python\r\n\r\nclass A(object):\r\n\tdef __init__(self, var1, var2):\r\n\t\t# type: (str, str) -> None\r\n\t\tself.var1 = var1\r\n\t\tself.var2 = var2\r\n\r\nclass Mixin(object):\r\n\tdef funcA(self):\r\n\t\t# type: () -> str\r\n\t\treturn self.var1\r\n\r\nclass B(A, Mixin):\r\n\tpass\r\n\r\ndef main():\r\n\t# type: () -> None\r\n\r\n\ta = B('value1', 'value2')\r\n\tprint(a.funcA())\r\n\r\nif __name__ == '__main__':\r\n\tmain()\r\n```\r\n\r\nThis is valid Python code, i.e.:\r\n```\r\n$ ./test.py\r\nvalue1\r\n```\r\n\r\nBut mypy is complaining:\r\n\r\n```\r\n$ ../py37/bin/mypy --python-executable ../py27/bin/python test.py\r\ntest.py:12: error: \"Mixin\" has no attribute \"var1\"\r\n```\r\n\r\nI understand the issue, there's nothing in `Mixin` class that states that `var1` will be available.\r\n\r\nMy question is is there a way to tell mypy somehow that this attribute will be available?\r\n\r\nI did not have much luck with using abstract classes and `Protocol`.\r\nI also had a suggestion to use stub file, but that doesn't look like be perfect solution either, since I might need to define types outside of the module and keep them in sync. And this pattern is used in multiple places not just one.\r\n\r\nIs there a way how this could be expressed in mypy? If the code needs to be slightly refactored without changing how it works I suppose that might also be acceptable.\r\n","author":{"url":"https://github.com/takeda","@type":"Person","name":"takeda"},"datePublished":"2018-10-25T18:03:21Z","url":"https://github.com/python/mypy/issues/5837"}
| 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:cbf21b78-8bde-3e33-d6c6-1acf9b650b4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E366:137B3:C4CC67:FF4B9C:6AFFF16A |
| html-safe-nonce | 06daa05701a1803766d45180a1c84fa1ea05af3142b0fc46f4b02544c9705335 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzY2OjEzN0IzOkM0Q0M2NzpGRjRCOUM6NkFGRkYxNkEiLCJ2aXNpdG9yX2lkIjoiOTg0NDQ5MDA5MDM0NzE5NTk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1778eef46f6c012a78ab4bec1f862d74fa77405592078a311ddc989b96241b8d |
| hovercard-subject-tag | issue:374065037 |
| 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/5837/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e43f5d5b6fcc6d2853a8dbba0ec721817daac7ee699cbe40341873f441f889c/python/mypy/issues/5837 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e43f5d5b6fcc6d2853a8dbba0ec721817daac7ee699cbe40341873f441f889c/python/mypy/issues/5837 |
| og:image:alt | This is more of a question or a feature request if there's not a good way to do it. I'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating the code t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | takeda |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width