Title: Dealing with error: Signature of "func" incompatible with supertype "Base" · Issue #9174 · python/mypy · GitHub
Open Graph Title: Dealing with error: Signature of "func" incompatible with supertype "Base" · Issue #9174 · python/mypy
X Title: Dealing with error: Signature of "func" incompatible with supertype "Base" · Issue #9174 · python/mypy
Description: Hello, this is a user question. Software versions are: Python 3.6.9 mypy 0.782 mypy-0.790+dev.28829fbb684d7535680934b401c05698db5b4d85 (from GitHub) I have a question related to mypy checks of overridden methods, as documented here. From...
Open Graph Description: Hello, this is a user question. Software versions are: Python 3.6.9 mypy 0.782 mypy-0.790+dev.28829fbb684d7535680934b401c05698db5b4d85 (from GitHub) I have a question related to mypy checks of over...
X Description: Hello, this is a user question. Software versions are: Python 3.6.9 mypy 0.782 mypy-0.790+dev.28829fbb684d7535680934b401c05698db5b4d85 (from GitHub) I have a question related to mypy checks of over...
Opengraph URL: https://github.com/python/mypy/issues/9174
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Dealing with error: Signature of \"func\" incompatible with supertype \"Base\"","articleBody":"Hello,\r\n\r\nthis is a user question.\r\n\r\nSoftware versions are:\r\n\r\n* Python 3.6.9\r\n* mypy 0.782\r\n* mypy-0.790+dev.28829fbb684d7535680934b401c05698db5b4d85 (from GitHub)\r\n\r\nI have a question related to mypy checks of overridden methods, as [documented here](https://mypy.readthedocs.io/en/latest/error_code_list.html#check-validity-of-overrides-override).\r\n\r\nFrom ticket #1237 (this [comment](https://github.com/python/mypy/issues/1237#issuecomment-189490903) in particular), I understand that this is to be expected and that there are reasons behind the behaviour below.\r\n\r\nPlease consider the classes here.\r\n\r\n\r\n```python\r\n# ######################################################################\r\n\r\nclass Base:\r\n def __init__(self):\r\n self.a = 0\r\n self.zxc = 'qwerty'\r\n\r\n def func(self, a:int):\r\n self.a = a\r\n\r\nclass ChildOne(Base):\r\n def __init__(self):\r\n self.b = 0\r\n\r\n def func(self, a:int, b:int):\r\n self.b = b\r\n super().func(a)\r\n\r\nclass ChildTwo(Base):\r\n def __init__(self):\r\n self.b = 0\r\n self.c = 0\r\n\r\n def func(self, a:int, b:int, c:int):\r\n self.b = b\r\n self.c = c\r\n super().func(a)\r\n\r\n# ######################################################################\r\n\r\nclass Base2:\r\n def do_something(self, *args, **kwargs):\r\n raise NotImplementedError('Should be overridden by subclasses')\r\n\r\nclass ChildOne2(Base2):\r\n def do_something(self, arg1:int, arg2:int):\r\n pass\r\n\r\nclass ChildTwo2(Base2):\r\n def do_something(self, arg1:int, arg2:str, arg3:list, arg4:dict):\r\n pass\r\n\r\n# ######################################################################\r\n```\r\n\r\nWhat mypy reports:\r\n\r\n```bash\r\n$ mypy mypy1.py\r\nmypy1.py:14: error: Signature of \"func\" incompatible with supertype \"Base\"\r\nmypy1.py:23: error: Signature of \"func\" incompatible with supertype \"Base\"\r\nmypy1.py:35: error: Signature of \"do_something\" incompatible with supertype \"Base2\"\r\nmypy1.py:39: error: Signature of \"do_something\" incompatible with supertype \"Base2\"\r\nFound 4 errors in 1 file (checked 1 source file)\r\n$\r\n```\r\n\r\n* The first case are sub-classes that have functionality on top of what the base class offers. Having performed what they are supposed to do, they forward the common parameter(s) to a parent class implementing some kind of a common functionality.\r\n\r\n In addition to the common functionality, it is often desirable to treat instances of ChildOne and ChildTwo as though they were instances of Base because I am only interested in the fact that the 'zxc' attribute is available, no matter what particular subclass of Base it comes from.\r\n\r\n* The other situation is considered the same type of an error by mypy in the sense that it is a case of differing signatures, which is why I am providing it here in the same ticket but conceptually it is somewhat different - I find myself writing this kind of code with plugins-like architectures where there is a set of methods to implement but sub-classes know better what their actual arguments and return types are.\r\n\r\nIn either case, this is not really about the child classes arguments that are wider or narrower in the mypy's type checking sense which I recognise is yet another situation.\r\n\r\nI find mypy a great tool and I am very thankful for your effort. However, I am just not sure how to tackle the two scenarios above, how to specify the types here.\r\n\r\nIn the latter scenario, I think it would suffice if I could just skip type checking in Base2's ```do_something``` method, after all it is not doing anything with its input, but using ```# type: ignore``` still leads to the same error.\r\n\r\n```python\r\n def do_something(self, *args, **kwargs):\r\n # type: ignore\r\n raise NotImplementedError('Should be overridden by subclasses')\r\n```\r\n\r\nIn the former case, I would truly like for the right types to be checked. In such a simple case, I could possibly simply rename func to base_func and call that in sub-classes but with a hierarchy that was deeper it could become difficult to know which base method to call thus being able to do simply ```super().func(a)``` and have it bubble up wherever the receiver is, that would be most convenient.\r\n\r\nJust to confirm it, I realise why mypy behaves in that way by default as this code looks like; I am just looking for a way to configure it to type check it anyway somehow because it seems a reasonable use-case, especially because I read comments in #1237 as confirming my belief to a degree (the idea of @covariant_args).\r\n\r\nThank you for your assistance.","author":{"url":"https://github.com/dsuch","@type":"Person","name":"dsuch"},"datePublished":"2020-07-19T23:43:56Z","url":"https://github.com/python/mypy/issues/9174"}
| 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:444a169e-65aa-4293-a492-0d22e1c8da5b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F8A:1A1C04:18ECAF8:21B738F:6AFFF693 |
| html-safe-nonce | fcd212ca3777d50e2250c725f3169e3b0251006c03c89a97699de61ee9ad8e70 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjhBOjFBMUMwNDoxOEVDQUY4OjIxQjczOEY6NkFGRkY2OTMiLCJ2aXNpdG9yX2lkIjoiMTEwNzAwMTYzOTE5NTA5MTM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 93d420373da167869973d1b43ad397e423c18d526ceb070750fde2418c49cc5b |
| hovercard-subject-tag | issue:661170406 |
| 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/9174/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fdfbc95be1bb404a1eb470ef4b541c5f2ce965d53c0271a184a36a33af9f6dcd/python/mypy/issues/9174 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fdfbc95be1bb404a1eb470ef4b541c5f2ce965d53c0271a184a36a33af9f6dcd/python/mypy/issues/9174 |
| og:image:alt | Hello, this is a user question. Software versions are: Python 3.6.9 mypy 0.782 mypy-0.790+dev.28829fbb684d7535680934b401c05698db5b4d85 (from GitHub) I have a question related to mypy checks of over... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dsuch |
| 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