Title: Type variable with NamedTuple as upper bound not working · Issue #3915 · python/mypy · GitHub
Open Graph Title: Type variable with NamedTuple as upper bound not working · Issue #3915 · python/mypy
X Title: Type variable with NamedTuple as upper bound not working · Issue #3915 · python/mypy
Description: I'm trying to type annotate a method that takes a NamedTuple subclass as an argument and returns an instance of that class. Here is a complete example: from typing import Dict from typing import NamedTuple from typing import Type from ty...
Open Graph Description: I'm trying to type annotate a method that takes a NamedTuple subclass as an argument and returns an instance of that class. Here is a complete example: from typing import Dict from typing import Na...
X Description: I'm trying to type annotate a method that takes a NamedTuple subclass as an argument and returns an instance of that class. Here is a complete example: from typing import Dict from typing impor...
Opengraph URL: https://github.com/python/mypy/issues/3915
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type variable with NamedTuple as upper bound not working","articleBody":"I'm trying to type annotate a method that takes a NamedTuple subclass as an argument and returns an instance of that class. Here is a complete example:\r\n\r\n````python\r\nfrom typing import Dict\r\nfrom typing import NamedTuple\r\nfrom typing import Type\r\nfrom typing import TypeVar\r\n\r\nStruct = TypeVar('Struct', bound=NamedTuple)\r\n\r\ndef namedtuple_from_dict(nt_class: Type[Struct], dict_values: Dict) -> Struct:\r\n \"\"\"Create a namedtuple from a dict, using the namedtuple attribute names\r\n to look up values in the dict.\"\"\"\r\n return nt_class._make(dict_values.get(k) for k in nt_class._fields) # type: ignore\r\n\r\nclass Point(NamedTuple):\r\n x: int\r\n y: int\r\n\r\nnamedtuple_from_dict(Point, {'x': 5, 'y': 3})\r\n# error: Type argument 1 of \"namedtuple_from_dict\" has incompatible value \"Point\"\r\n````\r\n\r\nI might be doing something wrong, but I think according to the documentation this should work. I've seen a couple of bugs being open related to NamedTuples, but not one that seemed to fit this case.\r\n\r\nThe ``type: ignore`` for the return statement is needed because otherwise mypy says the function returns a NamedTuple instead of a Struct instance - which seems to be another bug...?","author":{"url":"https://github.com/sjaensch","@type":"Person","name":"sjaensch"},"datePublished":"2017-09-04T20:26:00Z","url":"https://github.com/python/mypy/issues/3915"}
| 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:fc0dc93d-3e1b-1a87-8401-051d00752425 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F24:11BB24:1F85B8E:2AAD451:6B001596 |
| html-safe-nonce | 071321ce7c28fab51c1cbd2f3ecc359cb460813319ccea88e2170154bb2aa1e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjI0OjExQkIyNDoxRjg1QjhFOjJBQUQ0NTE6NkIwMDE1OTYiLCJ2aXNpdG9yX2lkIjoiMjgxMDQwMjgxOTYxNjU0NDE1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 898d05a7440709bc2e73718866050512406a3f6729a74e22d983a105657bad57 |
| hovercard-subject-tag | issue:255112815 |
| 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/3915/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1b29c5471056d773da15f2c5747ce8f28eca64226d8e6878fbd4b367dda7edbf/python/mypy/issues/3915 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1b29c5471056d773da15f2c5747ce8f28eca64226d8e6878fbd4b367dda7edbf/python/mypy/issues/3915 |
| og:image:alt | I'm trying to type annotate a method that takes a NamedTuple subclass as an argument and returns an instance of that class. Here is a complete example: from typing import Dict from typing import Na... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sjaensch |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width