Title: Decorated function makes type-check fail if done inside of method from the class · Issue #9266 · python/mypy · GitHub
Open Graph Title: Decorated function makes type-check fail if done inside of method from the class · Issue #9266 · python/mypy
X Title: Decorated function makes type-check fail if done inside of method from the class · Issue #9266 · python/mypy
Description: I believe this is a bug (explanation below). I expected to be able to statically check a case by checking whether self matches a given protocol (with the check_implements below). This seems to work, but if the method is decorated (with t...
Open Graph Description: I believe this is a bug (explanation below). I expected to be able to statically check a case by checking whether self matches a given protocol (with the check_implements below). This seems to work...
X Description: I believe this is a bug (explanation below). I expected to be able to statically check a case by checking whether self matches a given protocol (with the check_implements below). This seems to work...
Opengraph URL: https://github.com/python/mypy/issues/9266
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Decorated function makes type-check fail if done inside of method from the class","articleBody":"I believe this is a bug (explanation below).\r\n\r\nI expected to be able to statically check a case by checking whether `self` matches a given protocol (with the `check_implements` below).\r\n\r\nThis seems to work, but if the method is decorated (with the `implements` as shown in the code below) it doesn't validate properly that a method return type is wrong.\r\n\r\ni.e.: in the code below I expected that the `_: IFoo = check_implements(self)` line gives an error because the protocol declaration expects a `bool` return, whereas the method implementation says it returns a `str`.\r\n\r\nI think this is a bug because the same type-check later on says that the implementation doesn't conform to the protocol if done outside of the class -- as `_: IFoo = check_implements(Foo())`, so, it does something on `__init__` and a different thing if out of the `__init__`. Besides, it works if the function is not decorated.\r\n\r\nNote: even if I do `_: IFoo = check_implements(Foo())` in the `__init__` it doesn't validate properly (so, it doesn't seem related to `self` -- and it works as expected if the function is not decorated).\r\n\r\n```\r\nfrom typing import TypeVar, Protocol, Any, Callable\r\nimport functools\r\n\r\nF = TypeVar('F', bound=Callable[..., Any])\r\n\r\ndef implements(method: Any) -> Callable[[F], F]:\r\n \"\"\"\r\n Meant to be used as\r\n\r\n class B:\r\n @implements(IExpectedProtocol.method)\r\n def method(self):\r\n pass\r\n\r\n Useful for knowing whether a name still exists in the interface and document\r\n what's being implemented.\r\n\r\n In runtime validates that the name passed is the same name implemented.\r\n \"\"\"\r\n @functools.wraps(method)\r\n def wrapper(func):\r\n if func.__name__ != method.__name__:\r\n msg = \"Wrong @implements: %r expected, but implementing %r.\"\r\n msg = msg % (func.__name__, method.__name__)\r\n raise AssertionError(msg)\r\n\r\n return func\r\n\r\n return wrapper\r\n\r\nT = TypeVar(\"T\")\r\n\r\ndef check_implements(x: T) -> T:\r\n \"\"\"\r\n This is to be used in the constructor of a class which is expected to\r\n implement some protocol. It can be used as:\r\n\r\n def __init__(self):\r\n _: IExpectedProtocol = check_implements(self)\r\n\r\n Mypy should complain if `self` is not implementing the IExpectedProtocol.\r\n \"\"\"\r\n return x\r\n\r\nclass IFoo(Protocol):\r\n\r\n def some_method(self) -> bool:\r\n pass\r\n\r\nclass Foo(object):\r\n\r\n def __init__(self) -> None: # give type\r\n _: IFoo = check_implements(self) # Issue: Mypy does not report error here\r\n\r\n @implements(IFoo.some_method) # Note: if this implements is removed it reports the error at the __init__!\r\n def some_method(self) -> str: # Note: return value does not match protocol!\r\n pass\r\n\r\n_: IFoo = check_implements(Foo()) # Mypy properly reports error here (with or without the implements decorator).\r\n```","author":{"url":"https://github.com/fabioz","@type":"Person","name":"fabioz"},"datePublished":"2020-08-05T01:19:44Z","url":"https://github.com/python/mypy/issues/9266"}
| 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:a76cd6b8-827d-b2a3-ff01-14c721eaae6c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DEAC:5314B:24F6ABB:30336B2:6AFFF858 |
| html-safe-nonce | 0772d45ad73a717ab9f5b48736cc41cfdcafbb6d04be084dcff9d77780bd0e5f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUFDOjUzMTRCOjI0RjZBQkI6MzAzMzZCMjo2QUZGRjg1OCIsInZpc2l0b3JfaWQiOiIzNDE0ODc2NTI1NDQ1NTExMjU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fadc36d80da06f0f5b72558654fa7d45ddf54cc8b1240e5978c2a99bb7b07112 |
| hovercard-subject-tag | issue:673181920 |
| 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/9266/issue_layout |
| twitter:image | https://opengraph.githubassets.com/044d90fcc3cc820127e99bdc03b30b2c300d8446aed3fcc931e2adc7cc77ec12/python/mypy/issues/9266 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/044d90fcc3cc820127e99bdc03b30b2c300d8446aed3fcc931e2adc7cc77ec12/python/mypy/issues/9266 |
| og:image:alt | I believe this is a bug (explanation below). I expected to be able to statically check a case by checking whether self matches a given protocol (with the check_implements below). This seems to work... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fabioz |
| 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