Title: Clarify aliases vs variables · Issue #3494 · python/mypy · GitHub
Open Graph Title: Clarify aliases vs variables · Issue #3494 · python/mypy
X Title: Clarify aliases vs variables · Issue #3494 · python/mypy
Description: We have both type aliases and variables with types like Type[...], also there are module aliases and variables of type ModuleType. I think we should document the difference in semantics and provide examples. (Also I think there are few m...
Open Graph Description: We have both type aliases and variables with types like Type[...], also there are module aliases and variables of type ModuleType. I think we should document the difference in semantics and provide...
X Description: We have both type aliases and variables with types like Type[...], also there are module aliases and variables of type ModuleType. I think we should document the difference in semantics and provide...
Opengraph URL: https://github.com/python/mypy/issues/3494
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Clarify aliases vs variables","articleBody":"We have both type aliases and variables with types like ``Type[...]``, also there are module aliases and variables of type ``ModuleType``. I think we should document the difference in semantics and provide examples. (Also I think there are few minor things that should be fixed in mypy.) Here how I see things:\r\n\r\n1. Variables with type ``Type[...]`` should be created by assignments with an explicit type annotations:\r\n ```python\r\n class A: ...\r\n tp: Type[A] = A\r\n ```\r\n2. Aliases are created by assignments without an explicit type:\r\n ```python\r\n class A: ...\r\n Alias = A\r\n ```\r\n3. The difference is that aliases are completely known statically and can be used in type context (annotations):\r\n ```python\r\n class A: ...\r\n class B: ...\r\n\r\n if random() > 0.5:\r\n Alias = A\r\n else:\r\n Alias = B # I think this should be prohibited (there are two TODOs in semanal.py)\r\n\r\n tp: Type[object]\r\n if random() > 0.5:\r\n tp = A\r\n else:\r\n tp = B # This is OK\r\n\r\n def fun1(x: Alias) -> None: ... # This is OK\r\n def fun2(x: tp) -> None: ... # Error\r\n ```\r\n4. There is similar distinction between module aliases and variables with type ``types.ModuleType``:\r\n ```python\r\n import math, cmath\r\n from types import ModuleType\r\n\r\n mod: ModuleType = math # variable\r\n m = math # Alias\r\n\r\n mod.sin # Error. ModuleType has no attribute 'sin'\r\n m.sin # OK\r\n ```\r\n and also\r\n ```python\r\n if random() > 0.5:\r\n m = math\r\n else:\r\n m = cmath # Should be prohibited\r\n\r\n mod: ModuleType\r\n if random() > 0.5:\r\n mod = math\r\n else:\r\n mod = cmath # OK, note explicit annotation above\r\n ```\r\n5. For modules, we could add better support later (a particular module is a literal type as in https://github.com/python/mypy/issues/3062 and binder can bind ``mod`` to ``Union[Literal[math], Literal[cmath]]`` after the above code).\r\n\r\n@JukkaL Do we agree on semantics here? If yes, then I will make some minor changes in mypy and add this to documentation (maybe we also need to clarify this in PEP 484).","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2017-06-03T13:03:02Z","url":"https://github.com/python/mypy/issues/3494"}
| 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:11987779-f27c-365a-edc6-ebe74ceea892 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8F6:1A1C04:195A027:224E679:6AFFF90C |
| html-safe-nonce | 2c69ea75c992e81023724c3253daf4690babfa6e772b8057e7ded6417b86a30c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEY2OjFBMUMwNDoxOTVBMDI3OjIyNEU2Nzk6NkFGRkY5MEMiLCJ2aXNpdG9yX2lkIjoiMTA5NjIwODY4ODA1MTM4NjYzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | abc206fecad7719d73e1f5b24fcb549db239c0f8592ae362a0c8b0f248db7903 |
| hovercard-subject-tag | issue:233368173 |
| 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/mypy/3494/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a267d792d1467ec9496e379a7266805559ff2e3de66088d9c0f47b269952e502/python/mypy/issues/3494 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a267d792d1467ec9496e379a7266805559ff2e3de66088d9c0f47b269952e502/python/mypy/issues/3494 |
| og:image:alt | We have both type aliases and variables with types like Type[...], also there are module aliases and variables of type ModuleType. I think we should document the difference in semantics and provide... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilevkivskyi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width