René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:643d819b-9ae0-747b-6ea1-4efdec237df1
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9058:21219A:3177D7:3FBB3F:6AFF992C
html-safe-nonce4ad6981c9bb630f35d2e288a4821acedd72c9e713cbac58f6d0e1e53074dbf14
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDU4OjIxMjE5QTozMTc3RDc6M0ZCQjNGOjZBRkY5OTJDIiwidmlzaXRvcl9pZCI6Ijc2MjAzMDgzNjkwOTc1OTUxODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac0800fef4c903fec6af4e71c5844fc2ad1ed3978566a85ac80cb33dd13d85be45
hovercard-subject-tagissue:304127754
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/4717/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ee3fbb44cc795eb73d8293388913461f49173e221a1624d169fc92ffdcefb856/python/mypy/issues/4717
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ee3fbb44cc795eb73d8293388913461f49173e221a1624d169fc92ffdcefb856/python/mypy/issues/4717
og:image:altIn #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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameerikwright
hostnamegithub.com
expected-hostnamegithub.com
None2c75d045e8c84b51e2ac680086a6dd1eeaf2bc15b82137d22ee5106835d6256c
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release525b66d22c4f7e6bb8b62934bec198964f7e8b93
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/4717#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F4717
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F4717
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/4717
Reloadhttps://github.com/python/mypy/issues/4717
Reloadhttps://github.com/python/mypy/issues/4717
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests547 (547)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/4717
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/4717
Use case for typing.Type with abstract typeshttps://github.com/python/mypy/issues/4717#top
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
https://github.com/erikwright
erikwrighthttps://github.com/erikwright
on Mar 11, 2018https://github.com/python/mypy/issues/4717#issue-304127754
#2169https://github.com/python/mypy/issues/2169
#1843https://github.com/python/mypy/issues/1843
@gvanrossumhttps://github.com/gvanrossum
#2169 (comment)https://github.com/python/mypy/issues/2169#issuecomment-249001710
Nexthttps://github.com/python/mypy/issues/4717?timeline_page=1
ilevkivskyihttps://github.com/ilevkivskyi
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
priority-2-lowhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-2-low%22
on Mar 17, 2018https://github.com/python/mypy/issues/4717#event-1526895720
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Mar 17, 2018https://github.com/python/mypy/issues/4717#issuecomment-373932080
https://github.com/glyph
glyphhttps://github.com/glyph
on Jan 15, 2019https://github.com/python/mypy/issues/4717#issuecomment-454211819
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jan 15, 2019https://github.com/python/mypy/issues/4717#issuecomment-454444315
@glyphhttps://github.com/glyph
https://github.com/glyph
glyphhttps://github.com/glyph
on Jan 15, 2019https://github.com/python/mypy/issues/4717#issuecomment-454451377
JukkaLhttps://github.com/JukkaL
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%22
priority-2-lowhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-2-low%22
on Jan 15, 2019https://github.com/python/mypy/issues/4717#event-2075137691
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jan 15, 2019https://github.com/python/mypy/issues/4717#issuecomment-454463705
https://github.com/glyph
glyphhttps://github.com/glyph
on Jan 15, 2019https://github.com/python/mypy/issues/4717#issuecomment-454576354
@JukkaLhttps://github.com/JukkaL
https://github.com/glyph
glyphhttps://github.com/glyph
on Jan 16, 2019https://github.com/python/mypy/issues/4717#issuecomment-454609539
@JukkaLhttps://github.com/JukkaL
https://github.com/glyph
glyphhttps://github.com/glyph
on Aug 6, 2019https://github.com/python/mypy/issues/4717#issuecomment-518885346
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Aug 7, 2019https://github.com/python/mypy/issues/4717#issuecomment-519213502
#5374https://github.com/python/mypy/issues/5374
https://github.com/petr-motejlek
petr-motejlekhttps://github.com/petr-motejlek
on Feb 1, 2020https://github.com/python/mypy/issues/4717#issuecomment-581054142
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Feb 4, 2020https://github.com/python/mypy/issues/4717#issuecomment-581938354
bmerryhttps://github.com/bmerry
on Mar 11, 2025https://github.com/python/mypy/issues/4717#event-16692503026
Load band_mask models for gpucbf.antenna-channelised-voltage streamshttps://github.com/ska-sa/katsdpcontroller/commit/fb1b4f93d97fed3cdc96f6743e2edfa7f1f9d230
fb1b4f9https://github.com/ska-sa/katsdpcontroller/commit/fb1b4f93d97fed3cdc96f6743e2edfa7f1f9d230
https://github.com/rilshok
rilshokhttps://github.com/rilshok
on May 19, 2025https://github.com/python/mypy/issues/4717#issuecomment-2891988919
LecrisUThttps://github.com/LecrisUT
on Sep 24, 2025https://github.com/python/mypy/issues/4717#event-19895569764
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/e3ef13f743df648683430a809fdf32f1d6082740
e3ef13fhttps://github.com/LecrisUT/tmt/commit/e3ef13f743df648683430a809fdf32f1d6082740
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/5787e0ddb47bb0fa48b0b6c0f6b7a0176547a12c
5787e0dhttps://github.com/LecrisUT/tmt/commit/5787e0ddb47bb0fa48b0b6c0f6b7a0176547a12c
https://github.com/dpinol
dpinolhttps://github.com/dpinol
on Oct 6, 2025https://github.com/python/mypy/issues/4717#issuecomment-3370842626
def cast(typ: type[_T], val: Any) -> _Thttps://github.com/python/typeshed/blob/3c161811adf94079da9db866f67053a91c11b23c/stdlib/typing.pyi#L971
LecrisUThttps://github.com/LecrisUT
on Oct 16, 2025https://github.com/python/mypy/issues/4717#event-20313808872
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/17d2cfc002e4712819db126d3b9ab68d73264d3e
17d2cfchttps://github.com/LecrisUT/tmt/commit/17d2cfc002e4712819db126d3b9ab68d73264d3e
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/e1ab93dcc0c18baa869141cb85f947818a6ef72c
e1ab93dhttps://github.com/LecrisUT/tmt/commit/e1ab93dcc0c18baa869141cb85f947818a6ef72c
happzhttps://github.com/happz
on Oct 20, 2025https://github.com/python/mypy/issues/4717#event-20378575352
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/026f74288ee6b8fec37cce9436d9b624cde07daf
026f742https://github.com/LecrisUT/tmt/commit/026f74288ee6b8fec37cce9436d9b624cde07daf
Type ignore known mypy limitationshttps://github.com/LecrisUT/tmt/commit/20e66f325cbaf2cc4452cc92df7a8f121205a151
20e66f3https://github.com/LecrisUT/tmt/commit/20e66f325cbaf2cc4452cc92df7a8f121205a151
Add abc all-around (#4096)https://github.com/teemtee/tmt/commit/a409212e4c0cf6db4394c6b70cca6cd813dad934
#4096https://github.com/teemtee/tmt/pull/4096
a409212https://github.com/teemtee/tmt/commit/a409212e4c0cf6db4394c6b70cca6cd813dad934
mkoncekhttps://github.com/mkoncek
on Oct 21, 2025https://github.com/python/mypy/issues/4717#event-20415334341
Add abc all-around (teemtee#4096)https://github.com/mkoncek/tmt/commit/dac8da4c310fd5ec0197f988ce32bbb6b6afafc8
teemtee#4096https://github.com/teemtee/tmt/pull/4096
dac8da4https://github.com/mkoncek/tmt/commit/dac8da4c310fd5ec0197f988ce32bbb6b6afafc8
https://github.com/vallsv
vallsvhttps://github.com/vallsv
on Nov 7, 2025https://github.com/python/mypy/issues/4717#issuecomment-3501885316
def cast(typ: type[_T], val: Any) -> _Thttps://github.com/python/typeshed/blob/3c161811adf94079da9db866f67053a91c11b23c/stdlib/typing.pyi#L971
https://github.com/jacopoabramo
jacopoabramohttps://github.com/jacopoabramo
on Dec 10, 2025https://github.com/python/mypy/issues/4717#issuecomment-3638283059
some runtime type checkershttps://typeguard.readthedocs.io/en/latest/features.html#supported-standard-library-annotations
Python discoursehttps://discuss.python.org/t/mypy-filtering-mapping-of-objects-based-on-typing-protocol-as-a-typevar-input/104876/5
PEP 747https://peps.python.org/pep-0747/
RiyaPrabhakar-githttps://github.com/RiyaPrabhakar-git
GC-Content-Calculatorhttps://github.com/users/RiyaPrabhakar-git/projects/1
on Dec 12, 2025https://github.com/python/mypy/issues/4717#event-21527450662
github-project-automationhttps://github.com/apps/github-project-automation
GC-Content-Calculatorhttps://github.com/users/RiyaPrabhakar-git/projects/1
on Dec 12, 2025https://github.com/python/mypy/issues/4717
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/4717
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/4717
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.