Title: Proposal to add const-ness type hints · Issue #242 · python/typing · GitHub
Open Graph Title: Proposal to add const-ness type hints · Issue #242 · python/typing
X Title: Proposal to add const-ness type hints · Issue #242 · python/typing
Description: Motivation - Python does not have a concept or notion of object const-ness. The const-ness is normally conveyed via doc strings which explain the usage contracts of the frameworks involved. However, this does not provide the necessary fo...
Open Graph Description: Motivation - Python does not have a concept or notion of object const-ness. The const-ness is normally conveyed via doc strings which explain the usage contracts of the frameworks involved. However...
X Description: Motivation - Python does not have a concept or notion of object const-ness. The const-ness is normally conveyed via doc strings which explain the usage contracts of the frameworks involved. However...
Opengraph URL: https://github.com/python/typing/issues/242
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal to add const-ness type hints","articleBody":"**Motivation** - Python does not have a concept or notion of object const-ness. The const-ness is normally conveyed via doc strings which explain the usage contracts of the frameworks involved. However, this does not provide the necessary formalism required for static analysis tools (like pytype) to flag const-ness contract violations. Hence, we propose to add const-ness type hints to PEP-484 which can be used by static analysis tools to flag const-ness contract violations.\n## The Proposal\n\nWe propose adding two constructs:\n### The `@constmethod` decorator\n\nThis decorator can only be used to decorate instance methods. It describes methods which do not modify the instances on which they are invoked. That is, a method decorated with this decorator a) should not modify the fields on the object, b) should not call other methods which modify the fields of the object, c) and it should not pass non-primitive fields of the object to other callables which modify them.\n\nNormally, this decorator would be used on getters. For example:\n\n```\nfrom typing import constmethod\n\n\nclass MyClass(object):\n ...\n @constmethod\n def get_something(self) -> int: ...\n```\n### The protocol type-hint `Const`\n\nThis type-hint is used to describe function arguments and return values. When used to describe a function argument, it indicates that the function argument will not be modified by the function. It should only be used for non-primitive types. Example:\n\n```\nfrom typing import Const\n\n\ndef my_function(alist: Const[List[SomeClass]]) -> int: ...\n```\n\nThe above indicates that the list argument alist will not be modified by my_function. Note that it does not indicate that the elements of the list are also not modified. If it is desired to indicate that even the elements are not modified, then one should use:\n\n`def my_function(alist: Const[List[Const[SomeClass]]]) -> int: ...`\n\nWhen `Const` is used as a type-hint on return values, it indicates that the value returned should not be modified by the caller. Example:\n\n`def my_function() -> Const[List[SomeClass]]: ...`\n\nThis indicates that the returned list object should not be modified by the caller. As before, it does not indicate that the elements of the returned list should also not be modified. If even element const-ness is desired, then one should use:\n\n`def my_function() -> Const[List[Const[SomeClass]]]: ...`\n","author":{"url":"https://github.com/sivachandra","@type":"Person","name":"sivachandra"},"datePublished":"2016-07-11T17:42:02Z","url":"https://github.com/python/typing/issues/242"}
| 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:eb11c820-a1b4-18bd-ef58-93bd8976c0d0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1D8:29A20E:21BE722:2DD9AF9:6B002348 |
| html-safe-nonce | 549d31cc73f6f41b528682d73c04cde8d549e355451360809a454038da75884e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUQ4OjI5QTIwRToyMUJFNzIyOjJERDlBRjk6NkIwMDIzNDgiLCJ2aXNpdG9yX2lkIjoiMzk1MTMzNDQ4OTIyNDkxMzczNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e88b1908c2a905e45d529dc1844277faed396d15600620d329f35331968c7fab |
| hovercard-subject-tag | issue:164893607 |
| 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/242/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5ed18455dce73d41dce51c82341ff8dfe4d89ab8ffdf9f6b2e1a06d7f7a09eb5/python/typing/issues/242 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5ed18455dce73d41dce51c82341ff8dfe4d89ab8ffdf9f6b2e1a06d7f7a09eb5/python/typing/issues/242 |
| og:image:alt | Motivation - Python does not have a concept or notion of object const-ness. The const-ness is normally conveyed via doc strings which explain the usage contracts of the frameworks involved. However... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sivachandra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width