René's URL Explorer Experiment


Title: Types for singleton objects · Issue #236 · python/typing · GitHub

Open Graph Title: Types for singleton objects · Issue #236 · python/typing

X Title: Types for singleton objects · Issue #236 · python/typing

Description: I repeatedly encounter code that uses a singleton instance that is only used to mark some special condition. Here is an example: _empty = object() def f(x=_empty): if x is _empty: # default argument value return 0 elif x is None: # argum...

Open Graph Description: I repeatedly encounter code that uses a singleton instance that is only used to mark some special condition. Here is an example: _empty = object() def f(x=_empty): if x is _empty: # default argumen...

X Description: I repeatedly encounter code that uses a singleton instance that is only used to mark some special condition. Here is an example: _empty = object() def f(x=_empty): if x is _empty: # default argumen...

Mail addresses
notifications@github.com
notifications@github.com

Opengraph URL: https://github.com/python/typing/issues/236

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Types for singleton objects","articleBody":"I repeatedly encounter code that uses a singleton instance that is only used to mark some special condition. Here is an example:\n\n```\n_empty = object()\n\ndef f(x=_empty):\n    if x is _empty:\n        # default argument value\n        return 0\n    elif x is None:\n        # argument was provided and it's None\n        return 1\n    else:\n        # non-None argument provided\n       return x * 2\n```\n\nCurrently PEP 484 doesn't provide primitives for type checking such code precisely. Workarounds such as casts or declaring the type of the special object as `Any` are possible. We can't use the type `object` in a union type, since that would cover all possible values, as `object` is the common base class of everything. \n\nWe could refactor the code to use a separate class for the special object and use `isinstance` to check for the special value, but this may require changing the code in non-trivial ways for larger examples and carries some runtime overhead as `isinstance` checks are slower than `is` operations. Example:\n\n```\nfrom typing import Union\n\nclass Empty: pass\n\n_empty = Empty()\n\ndef f(x: Union[int, None, Empty] = _empty) -> int:\n    # use isinstance so that type checkers can recognize that we are decomposing a union\n    if isinstance(x, Empty):\n        # default argument value\n        return 0\n    elif x is None:\n        # argument was provided and it's None\n        return 1\n    else:\n        # non-None argument provided\n       return x * 2\n```\n\nA potentially better way is to support user-defined types that only include a single instance. This can be seen as a generalization of the `None` type. Here is a potential way to use this to type check the example:\n\n```\nfrom typing import SingletonType, Union\n\n_empty = object()\n\nEmpty = SingletonType('Empty', _empty)\n\ndef f(x: Union[int, None, Empty] = _empty) -> int:\n    if x is _empty:\n        # default argument value\n        return 0\n    elif x is None:\n        # argument was provided and it's None\n        return 1\n    else:\n        # non-None argument provided\n       return x * 2\n```\n\nThe final example is better than second example in a few ways:\n- It's closer to the original code.\n- It's as fast as the original code (modulo one-time overhead due to type annotations).\n","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2016-06-17T10:57:31Z","url":"https://github.com/python/typing/issues/236"}

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:a0c48747-2025-9814-a52d-0e30a0dbe3b7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8C16:C2FBA:26935:358A8:6B006048
html-safe-nonce166e3ec1e8d458d1ca7c2cc5bebb5ff924d26eece97e466373605c304306a68f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzE2OkMyRkJBOjI2OTM1OjM1OEE4OjZCMDA2MDQ4IiwidmlzaXRvcl9pZCI6IjQ1MTQ0NjA2MDkzMTk4MjEzODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacf3886731c8490444d30041a48c2a904b4fa7b585390491eda3b2fe3307492c4d
hovercard-subject-tagissue:160863176
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/typing/236/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c808aec48fd8b31f90fd847e09c81e79e5c03623940ac439d215ea6c8fe078fb/python/typing/issues/236
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c808aec48fd8b31f90fd847e09c81e79e5c03623940ac439d215ea6c8fe078fb/python/typing/issues/236
og:image:altI repeatedly encounter code that uses a singleton instance that is only used to mark some special condition. Here is an example: _empty = object() def f(x=_empty): if x is _empty: # default argumen...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameJukkaL
hostnamegithub.com
expected-hostnamegithub.com
None29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73
turbo-cache-controlno-preview
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
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
releaseeacd927ba6112e3b01ba99c762bd699a4a4f5df7
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typing/issues/236#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F236
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%2Ftyping%2Fissues%2F236
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%2Ftyping
Reloadhttps://github.com/python/typing/issues/236
Reloadhttps://github.com/python/typing/issues/236
Reloadhttps://github.com/python/typing/issues/236
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
https://github.com/python/typing
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/236
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/236
Types for singleton objectshttps://github.com/python/typing/issues/236#top
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 17, 2016https://github.com/python/typing/issues/236#issue-160863176
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jun 18, 2016https://github.com/python/typing/issues/236#issuecomment-226935220
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 18, 2016https://github.com/python/typing/issues/236#issuecomment-226948407
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jun 18, 2016https://github.com/python/typing/issues/236#issuecomment-226966191
@gvanrossumhttps://github.com/gvanrossum
@JukkaLhttps://github.com/JukkaL
https://github.com/markshannon
markshannonhttps://github.com/markshannon
on Jun 19, 2016https://github.com/python/typing/issues/236#issuecomment-226972246
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 20, 2016https://github.com/python/typing/issues/236#issuecomment-227108563
@gvanrossumhttps://github.com/gvanrossum
@ilevkivskyihttps://github.com/ilevkivskyi
@ilevkivskyihttps://github.com/ilevkivskyi
@markshannonhttps://github.com/markshannon
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 20, 2016https://github.com/python/typing/issues/236#issuecomment-227180301
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229317514
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229434614
https://github.com/markshannon
markshannonhttps://github.com/markshannon
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229446242
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229453982
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229455083
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229456447
https://github.com/markshannon
markshannonhttps://github.com/markshannon
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229458912
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229464442
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229488224
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jun 29, 2016https://github.com/python/typing/issues/236#issuecomment-229515556
@gvanrossumhttps://github.com/gvanrossum
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Jun 30, 2016https://github.com/python/typing/issues/236#issuecomment-229610919
@gvanrossumhttps://github.com/gvanrossum
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 30, 2016https://github.com/python/typing/issues/236#issuecomment-229702232
@ilevkivskyihttps://github.com/ilevkivskyi
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jun 30, 2016https://github.com/python/typing/issues/236#issuecomment-229791540
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jul 14, 2016https://github.com/python/typing/issues/236#issuecomment-232554039
#240https://github.com/python/typing/pull/240
python/mypy#1803https://github.com/python/mypy/issues/1803
gvanrossumhttps://github.com/gvanrossum
completedhttps://github.com/python/typing/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jul 14, 2016https://github.com/python/typing/issues/236#event-722491171
niklasfhttps://github.com/niklasf
on Apr 21, 2019https://github.com/python/typing/issues/236#event-2290071165
Make SkipType an enumhttps://github.com/niklasf/python-chess/commit/c564ecf4cca20cbe28a85c50b41498b74331e377
c564ecfhttps://github.com/niklasf/python-chess/commit/c564ecf4cca20cbe28a85c50b41498b74331e377
mthuurnehttps://github.com/mthuurne
on Mar 25, 2023https://github.com/python/typing/issues/236#event-8844868974
Use `Enum` to make mypy understand that `unlimited` is a singletonhttps://github.com/mthuurne/retroasm/commit/b6a26bfb3e34bfbd8be143abf05907d5c69149f1
b6a26bfhttps://github.com/mthuurne/retroasm/commit/b6a26bfb3e34bfbd8be143abf05907d5c69149f1
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/236
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/236
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.