Title: Use case for typing.Type with abstract types · Issue #4717 · python/mypy · GitHub
Open Graph Title: Use case for typing.Type with abstract types · Issue #4717 · python/mypy
X Title: Use case for typing.Type with abstract types · Issue #4717 · python/mypy
Description: In #2169 and #1843 there was discussion about using Type[some_abc] and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of some_abc. There is an implicit assumption that th...
Open Graph Description: In #2169 and #1843 there was discussion about using Type[some_abc] and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of some_abc....
X Description: In #2169 and #1843 there was discussion about using Type[some_abc] and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of some_abc....
Opengraph URL: https://github.com/python/mypy/issues/4717
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use case for typing.Type with abstract types","articleBody":"In https://github.com/python/mypy/issues/2169 and https://github.com/python/mypy/issues/1843 there was discussion about using `Type[some_abc]` and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of `some_abc`. There is an implicit assumption that the objective of a function taking such a type is, ultimately, to instantiate the type.\r\n\r\n@gvanrossum said, in https://github.com/python/mypy/issues/2169#issuecomment-249001710:\r\n\r\n> But maybe we need a check that you call this thing with a concrete subclass;\r\n> and for that we would need some additional notation (unless maybe we could\r\n> just say that whenever there's an argument of Type[A] where A is abstract,\r\n> that the argument must be a concrete subclass. But for that we'd need some\r\n> experiment to see if there's much real-world code that passes abstract\r\n> classes around. (If there is, we'd need to have a way to indicate the need\r\n> in the signature.)\r\n\r\nI have such a use case.\r\n\r\nI have a sequence of observers supplied by clients of my library, to which I want to dispatch events according to the abstract base class(es) that each implements. I tried to do this as follows:\r\n\r\n```python\r\nimport abc\r\nimport typing\r\n\r\nclass FooObserver(metaclass=abc.ABCMeta):\r\n \"\"\"Receives Foo events.\"\"\"\r\n\r\n @abc.abstractmethod\r\n def on_foo(self, count: int) -> None:\r\n raise NotImplementedError()\r\n\r\nclass BarObserver(metaclass=abc.ABCMeta):\r\n \"\"\"Receives Bar events.\"\"\"\r\n\r\n @abc.abstractmethod\r\n def on_bar(self, status: str) -> None:\r\n raise NotImplementedError()\r\n\r\nclass Engine:\r\n def __init__(self, observers: typing.Sequence[typing.Any]) -> None:\r\n self.__all_observers = observers\r\n\r\n def do_bar(self, succeed: bool) -> None:\r\n status = 'ok' if succeed else 'problem'\r\n for bar_observer in self.__observers(BarObserver):\r\n bar_observer.on_bar(status)\r\n\r\n def do_foo(self, elements: typing.Sequence[typing.Any]) -> None:\r\n count = len(elements)\r\n for foo_observer in self.__observers(FooObserver):\r\n foo_observer.on_foo(count)\r\n\r\n __OBSERVER_TYPE = typing.TypeVar('__OBSERVER_TYPE')\r\n\r\n def __observers(\r\n self,\r\n observer_type: typing.Type['__OBSERVER_TYPE']\r\n ) -> typing.Sequence['__OBSERVER_TYPE']:\r\n return [observer for observer in self.__all_observers\r\n if isinstance(observer, observer_type)]\r\n```\r\n\r\nUnfortunately, MyPy complains about this as follows:\r\n\r\n```\r\n/Users/erikwright/abc_typing.py:24: error: Only concrete class can be given where \"Type[BarObserver]\" is expected\r\n/Users/erikwright/abc_typing.py:29: error: Only concrete class can be given where \"Type[FooObserver]\" is expected\r\n```\r\n\r\nGiven that (AFAICT) the decision was made to not attempt to verify that the runtime type supports any specific constructor signature I'm wondering why there is nonetheless an expectation that the runtime type is constructable at all. In my case, the entire purpose of typing here is:\r\n\r\n1. Require you to actually pass _a type_, which means I can use it in `isinstance`\r\n2. Allow me to specify the return type of the method in terms of the supplied type.","author":{"url":"https://github.com/erikwright","@type":"Person","name":"erikwright"},"datePublished":"2018-03-11T03:46:59Z","url":"https://github.com/python/mypy/issues/4717"}
| 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:643d819b-9ae0-747b-6ea1-4efdec237df1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9058:21219A:3177D7:3FBB3F:6AFF992C |
| html-safe-nonce | 4ad6981c9bb630f35d2e288a4821acedd72c9e713cbac58f6d0e1e53074dbf14 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDU4OjIxMjE5QTozMTc3RDc6M0ZCQjNGOjZBRkY5OTJDIiwidmlzaXRvcl9pZCI6Ijc2MjAzMDgzNjkwOTc1OTUxODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0800fef4c903fec6af4e71c5844fc2ad1ed3978566a85ac80cb33dd13d85be45 |
| hovercard-subject-tag | issue:304127754 |
| 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/4717/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ee3fbb44cc795eb73d8293388913461f49173e221a1624d169fc92ffdcefb856/python/mypy/issues/4717 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ee3fbb44cc795eb73d8293388913461f49173e221a1624d169fc92ffdcefb856/python/mypy/issues/4717 |
| og:image:alt | In #2169 and #1843 there was discussion about using Type[some_abc] and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of some_abc.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erikwright |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2c75d045e8c84b51e2ac680086a6dd1eeaf2bc15b82137d22ee5106835d6256c |
| 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 | 525b66d22c4f7e6bb8b62934bec198964f7e8b93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width