Title: Consider loosening restrictions of `Final` · Issue #920 · python/typing · GitHub
Open Graph Title: Consider loosening restrictions of `Final` · Issue #920 · python/typing
X Title: Consider loosening restrictions of `Final` · Issue #920 · python/typing
Description: I think that Final is a very based idea, but I feel that it's current restrictions make using it painful. I think that split assignments should be allowed: def foo(cond: bool) -> None: if cond: a: Final = Funny("spam") else: a: Final = F...
Open Graph Description: I think that Final is a very based idea, but I feel that it's current restrictions make using it painful. I think that split assignments should be allowed: def foo(cond: bool) -> None: if cond: a: ...
X Description: I think that Final is a very based idea, but I feel that it's current restrictions make using it painful. I think that split assignments should be allowed: def foo(cond: bool) -> None: if co...
Opengraph URL: https://github.com/python/typing/issues/920
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Consider loosening restrictions of `Final`","articleBody":"I think that `Final` is a very based idea, but I feel that it's current restrictions make using it painful.\r\n\r\nI think that split assignments should be allowed:\r\n```py\r\ndef foo(cond: bool) -> None:\r\n if cond:\r\n a: Final = Funny(\"spam\")\r\n else:\r\n a: Final = Funny(\"eggs\") # this is currently an error\r\n```\r\n\r\nMost languages allow this pattern to initialize constant variables:\r\n```py\r\ndef foo(cond: bool) -> None:\r\n a: Final[Funny]\r\n if cond:\r\n a = Funny(\"spam\")\r\n else:\r\n a = Funny(\"eggs\")\r\n```\r\nI think these are convenient and enables `Final` to be used in more scenarios, but this is currently an error.\r\nBut this is allowed:\r\n```py\r\ndef foo(cond: bool) -> None:\r\n a: Final = Funny(\"spam\") if cond else Funny(\"eggs\")\r\n```\r\n\u003cbr/>\r\n\r\nAnother issue I've found is the prohibition of `Final` declarations within loops:\r\n```py\r\ndef foo() -> None:\r\n for i in [1,2,3]:\r\n a: Final = i * 2 # currently an error\r\n a = 10 # error\r\n print(a)\r\n print(a)\r\n a = 10 # error\r\n```\r\nThis is defined in the pep: \r\n> Note that a type checker need not allow Final declarations inside loops since the runtime will see multiple assignments to the same variable in subsequent iterations.\r\n\r\nI understand that at runtime the same variable is reused for each iteration of the loop, but if you ignore that fact and just look at the semantic meaning of the code, it matches exactly to this Kotlin example:\r\n```kt\r\nfun foo() {\r\n val a = listOf(1, 2, 3).map { i ->\r\n val a = i\r\n a = 10 // error\r\n println(a)\r\n a\r\n }.last()\r\n println(a)\r\n a = 10 // error\r\n}\r\n```\r\nDisallowing `Final` in loops doesn't address any of the motivations listed in the pep, any I can't see any reason why it should be disallowed.\r\n\u003cbr/>\r\n\r\nAlso, why are `Final` annotations not allowed on functional arguments?\r\n> `Final` may only be used as the outermost type in assignments or variable annotations. Using it in any other position is an error. In particular, `Final` can't be used in annotations for function arguments:\r\n\r\nWhy? This is commonly seen in other languages. In Kotlin, function parameters can __only__ be `val`(Kotlin's form of `Final`), in Java a function parameter may be marked with `final`.","author":{"url":"https://github.com/DetachHead","@type":"Person","name":"DetachHead"},"datePublished":"2021-11-04T12:52:17Z","url":"https://github.com/python/typing/issues/920"}
| 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:2d2739e6-d2a4-39af-3238-b15f9ad64a2d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 98D4:2E9DBB:2368E6D:305A1A2:6B00321B |
| html-safe-nonce | 987414f8ee465bd0a576371accc2afd24cd5873c4a20fbaf22daf77dd6c9706f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEQ0OjJFOURCQjoyMzY4RTZEOjMwNUExQTI6NkIwMDMyMUIiLCJ2aXNpdG9yX2lkIjoiNjU3MTY3ODY1NjIzNjYzMDU1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d9c323a0802e930ae366e57ccc42eda5b6dc92642998b1b99e89c555088ef6ea |
| hovercard-subject-tag | issue:1044730788 |
| 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/920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/371e7b8317f36eceaaaa058017918ccf6dfdd327a154c82b78ad270743518fd8/python/typing/issues/920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/371e7b8317f36eceaaaa058017918ccf6dfdd327a154c82b78ad270743518fd8/python/typing/issues/920 |
| og:image:alt | I think that Final is a very based idea, but I feel that it's current restrictions make using it painful. I think that split assignments should be allowed: def foo(cond: bool) -> None: if cond: a: ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DetachHead |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width