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
Domain: github.com
{"@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-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:a0c48747-2025-9814-a52d-0e30a0dbe3b7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C16:C2FBA:26935:358A8:6B006048 |
| html-safe-nonce | 166e3ec1e8d458d1ca7c2cc5bebb5ff924d26eece97e466373605c304306a68f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzE2OkMyRkJBOjI2OTM1OjM1OEE4OjZCMDA2MDQ4IiwidmlzaXRvcl9pZCI6IjQ1MTQ0NjA2MDkzMTk4MjEzODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f3886731c8490444d30041a48c2a904b4fa7b585390491eda3b2fe3307492c4d |
| hovercard-subject-tag | issue:160863176 |
| 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/typing/236/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c808aec48fd8b31f90fd847e09c81e79e5c03623940ac439d215ea6c8fe078fb/python/typing/issues/236 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c808aec48fd8b31f90fd847e09c81e79e5c03623940ac439d215ea6c8fe078fb/python/typing/issues/236 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width