Title: Callables and `__init__` methods · Issue #15024 · python/mypy · GitHub
Open Graph Title: Callables and `__init__` methods · Issue #15024 · python/mypy
X Title: Callables and `__init__` methods · Issue #15024 · python/mypy
Description: Bug Report I am currently working on Kludex/starlette#1987 where I'm defining a type hint for an argument that can take either a type of a class having a __call__ method, or a callable returning another callable. That is, both definition...
Open Graph Description: Bug Report I am currently working on Kludex/starlette#1987 where I'm defining a type hint for an argument that can take either a type of a class having a __call__ method, or a callable returning an...
X Description: Bug Report I am currently working on Kludex/starlette#1987 where I'm defining a type hint for an argument that can take either a type of a class having a __call__ method, or a callable returnin...
Opengraph URL: https://github.com/python/mypy/issues/15024
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Callables and `__init__` methods","articleBody":"\u003c!--\r\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\r\nPlease consider:\r\n\r\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\r\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\r\n- asking on gitter chat: https://gitter.im/python/typing\r\n-->\r\n\r\n**Bug Report**\r\n\r\nI am currently working on https://github.com/encode/starlette/pull/1987 where I'm defining a type hint for an [argument](https://github.com/encode/starlette/blob/54cc128f924dbca7916dad9afd5243a0d58f8ab4/starlette/middleware/__init__.py#L7) that can take either a type of a class having a `__call__` method, or a callable returning another callable. That is, both definitions work:\r\n\r\n```python\r\nclass ASGIMiddleware:\r\n def __init__(app: ASGIApp, p0: str, p1: int) -> None:\r\n ...\r\n async def __call__(self, scope, receive, send) -> None:\r\n ...\r\n# or\r\ndef asgi_middleware(app, p0: str, p1: int) -> ASGIApp:\r\n async def _asgi(scope, receive, send) -> None:\r\n ...\r\n return _asgi\r\n```\r\n\r\nI was told that the following protocol would support both:\r\n\r\n```python\r\nclass MiddlewareType(Protocol):\r\n def __call__(self, app: ASGIApp, **options: Any) -> ASGIApp:\r\n ...\r\n```\r\nWith the class definition, the `__init__` method is checked against the `__call__` method of `MiddlewareType`, and with the callable definition, well it is checked against the `__call__` method naturally. I was surprised to see that mypy supported the first case, as it was unclear to me what was the correct definition of a `callable` (in fact, definitions differ in docs):\r\n\r\n- https://docs.python.org/3/library/functions.html#callable\r\n> Return [True](https://docs.python.org/3/library/constants.html#True) if the object argument appears callable, [False](https://docs.python.org/3/library/constants.html#False) if not. If this returns `True`, it is still possible that a call fails, but if it is `False`, calling _object_ will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a `__call__()` method.\r\n\r\n- https://docs.python.org/3/library/typing.html#typing.Callable\r\n> Callable type; `Callable[[int], str]` is a function of (int) -> str.\r\n\r\n- https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable\r\n> ABC for classes that provide the `__call__()` method.\r\n\r\nNow if this behaviour is supported by `mypy`, maybe this could be clarified in [this part](https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols) of the docs?\r\n\r\nI've made this issue a bug report as I'm still struggling to have the `MiddlewareType` type working, as `**options: typing.Any` doesn't seem to catch the args/kwargs following `app: ASGIApp`.\r\n\r\nSee this mypy-play example:\r\n\r\nhttps://mypy-play.net/?mypy=0.991&python=3.11&gist=49f042e0c90bdaf202e519bd1467ce3a (works on `0.991`)\r\nhttps://mypy-play.net/?mypy=1.1.1&python=3.11&gist=6c7629c60546af1490ba092821564d63 (doesn't work since `1.0.0`)\r\n\r\nThe last example is also returning a misleading error message:\r\n\r\n```sh\r\nmain.py:24: note: Following member(s) of \"ExampleMiddleware\" have conflicts:\r\nmain.py:24: note: Expected:\r\nmain.py:24: note: def __call__(app: int, **options: Any) -> Callable[[str], None]\r\nmain.py:24: note: Got:\r\nmain.py:24: note: def __call__(self: ExampleMiddleware, el: str) -> None # should be: def __init__(self, app: int, other_arg: Any) -> None:\r\n```\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: 0.991 / > 1.0.0\r\n- Mypy command-line flags: None\r\n- Mypy configuration options from `mypy.ini` (and other config files):\r\n- Python version used: 3.9 / 3.10 / 3.11\r\n\r\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\r\n","author":{"url":"https://github.com/Viicos","@type":"Person","name":"Viicos"},"datePublished":"2023-04-09T09:07:50Z","url":"https://github.com/python/mypy/issues/15024"}
| 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:010106e7-b1ef-6ada-d865-15042500a892 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A16E:DCDA8:30D41F3:406F9DF:6B002A41 |
| html-safe-nonce | 9654a5d760fdcb07ee54ef2b27765b4cb30f0819e544a982e6e771498ae1a472 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTZFOkRDREE4OjMwRDQxRjM6NDA2RjlERjo2QjAwMkE0MSIsInZpc2l0b3JfaWQiOiI2ODIxNDQ1ODk5NDQ1NjExMDczIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7fa4a00be034d85888be6837e625b01748e293ac0740fe37e53d39438a353873 |
| hovercard-subject-tag | issue:1659807147 |
| 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/15024/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f174e4068da0dee8d18bb88eedcc47adfb6851b3409bdb98ee83e3d0bd1064c5/python/mypy/issues/15024 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f174e4068da0dee8d18bb88eedcc47adfb6851b3409bdb98ee83e3d0bd1064c5/python/mypy/issues/15024 |
| og:image:alt | Bug Report I am currently working on Kludex/starlette#1987 where I'm defining a type hint for an argument that can take either a type of a class having a __call__ method, or a callable returning an... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Viicos |
| 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