Title: Fix `__init__` type of `typing.NamedTuple` · Issue #6079 · python/typeshed · GitHub
Open Graph Title: Fix `__init__` type of `typing.NamedTuple` · Issue #6079 · python/typeshed
X Title: Fix `__init__` type of `typing.NamedTuple` · Issue #6079 · python/typeshed
Description: While working on python/mypy#11047 I've noticed that there's an issue with typing.NamedTuple Right now __init__ of typing.NamedTuple has type: def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., **kwargs: Any) -> N...
Open Graph Description: While working on python/mypy#11047 I've noticed that there's an issue with typing.NamedTuple Right now __init__ of typing.NamedTuple has type: def __init__(self, typename: str, fields: Iterable[Tup...
X Description: While working on python/mypy#11047 I've noticed that there's an issue with typing.NamedTuple Right now __init__ of typing.NamedTuple has type: def __init__(self, typename: str, fields: Iter...
Opengraph URL: https://github.com/python/typeshed/issues/6079
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fix `__init__` type of `typing.NamedTuple`","articleBody":"While working on https://github.com/python/mypy/issues/11047 I've noticed that there's an issue with `typing.NamedTuple`\r\n\r\nRight now `__init__` of `typing.NamedTuple` has type: `def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., **kwargs: Any) -> None: ...` which is not correct.\r\n\r\nWhy? Let's try to add both `fields` and `kwargs`:\r\n\r\n```python\r\n>>> from typing import NamedTuple\r\n>>> NamedTuple('a', (), f=int)\r\nTraceback (most recent call last):\r\n File \"\u003cstdin>\", line 1, in \u003cmodule>\r\n File \"/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py\", line 1864, in NamedTuple\r\n raise TypeError(\"Either list of fields or keywords\"\r\nTypeError: Either list of fields or keywords can be provided to NamedTuple, not both\r\n```\r\n\r\nSource: https://github.com/python/cpython/blob/f56268a2cd38b3fe2be1e4361d3d8b581e73559b/Lib/typing.py#L2290-L2294\r\n\r\nSo, it should be an overload instead:\r\n\r\n```python\r\n @overload\r\n def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ...) -> None: ...\r\n @overload\r\n def __init__(self, typename: str, **kwargs: Any) -> None: ...\r\n```\r\n\r\nNotice, that `fields=None` is still allowed:\r\n\r\n```python\r\n>>> NamedTuple('a', fields=None, f=int)\r\n```\r\n\r\nBut, I don't think that we should have `def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...` instead.\r\n\r\nOne more thing: is it `Tuple[str, Any]` or `Tuple[str, type]`? It is supposed to be types. But, it is not enforced anywhere (except `mypy`) as far as I know.\r\n\r\nI will send a PR with the fix.","author":{"url":"https://github.com/sobolevn","@type":"Person","name":"sobolevn"},"datePublished":"2021-09-26T18:24:39Z","url":"https://github.com/python/typeshed/issues/6079"}
| 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:361c8bbc-60a4-a03a-1bc6-cd4861c948d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9AA8:B438C:11A33A5:1731E64:6B000B3C |
| html-safe-nonce | 595874b32c250ff1be3930550b14f53e30e5a9cabb812801db509720650bdb29 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUE4OkI0MzhDOjExQTMzQTU6MTczMUU2NDo2QjAwMEIzQyIsInZpc2l0b3JfaWQiOiI3MDE0MTkzNjc2NTIzOTMyNDc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7de7d45ec920ee342c42d4f511bef1dab7f62b339900b5afade8a6a7ad8196d3 |
| hovercard-subject-tag | issue:1007465549 |
| 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/typeshed/6079/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f10f4df1c0130dd2958ef5d3a1812cb7a6e5c2468bae53e2e4235843827b982d/python/typeshed/issues/6079 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f10f4df1c0130dd2958ef5d3a1812cb7a6e5c2468bae53e2e4235843827b982d/python/typeshed/issues/6079 |
| og:image:alt | While working on python/mypy#11047 I've noticed that there's an issue with typing.NamedTuple Right now __init__ of typing.NamedTuple has type: def __init__(self, typename: str, fields: Iterable[Tup... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sobolevn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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