Title: `is_dataclass` should be a type guard and `asdict` should only work on objects with `__dataclass_fields__` · Issue #9345 · python/typeshed · GitHub
Open Graph Title: `is_dataclass` should be a type guard and `asdict` should only work on objects with `__dataclass_fields__` · Issue #9345 · python/typeshed
X Title: `is_dataclass` should be a type guard and `asdict` should only work on objects with `__dataclass_fields__` · Issue #9345 · python/typeshed
Description: @AlexWaygood proposed the following stubs in this comment: python/mypy#14263 (comment) from typing import Any, ClassVar, Protocol from typing_extensions import TypeGuard class _DataclassInstance(Protocol): __dataclass_fields__: ClassVar[...
Open Graph Description: @AlexWaygood proposed the following stubs in this comment: python/mypy#14263 (comment) from typing import Any, ClassVar, Protocol from typing_extensions import TypeGuard class _DataclassInstance(Pr...
X Description: @AlexWaygood proposed the following stubs in this comment: python/mypy#14263 (comment) from typing import Any, ClassVar, Protocol from typing_extensions import TypeGuard class _DataclassInstance(Pr...
Opengraph URL: https://github.com/python/typeshed/issues/9345
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`is_dataclass` should be a type guard and `asdict` should only work on objects with `__dataclass_fields__`","articleBody":"@AlexWaygood proposed the following stubs in this comment: https://github.com/python/mypy/pull/14263#issuecomment-1343337541\r\n\r\n```python\r\nfrom typing import Any, ClassVar, Protocol\r\nfrom typing_extensions import TypeGuard\r\n\r\nclass _DataclassInstance(Protocol):\r\n __dataclass_fields__: ClassVar[dict[str, Any]]\r\n\r\ndef asdict(obj: _DataclassInstance) -> dict[str, Any]: ...\r\ndef is_dataclass(obj: object) -> TypeGuard[_DataclassInstance]: ...\r\n```\r\n\r\nand this works as expected with dataclasses on mypy and pyright, but it doesn't currently work on pyre because pyre doesn't know yet that dataclasses have the `__dataclass_fields__` classvar. (I haven't checked other type checkers.)\r\n\r\nAt runtime, `is_dataclass` really only checks for the `__dataclass_fields__` attribute:\r\n\r\nhttps://github.com/python/cpython/blob/7a0f3c1d92ef0768e082ace19d970b0ef12e7346/Lib/dataclasses.py#L1263-L1267\r\n\r\nHowever, it also works with types in addition to instances, so an overload would be needed.\r\n\r\nSee also this issue for context: https://github.com/python/mypy/issues/14215","author":{"url":"https://github.com/tmke8","@type":"Person","name":"tmke8"},"datePublished":"2022-12-09T23:03:10Z","url":"https://github.com/python/typeshed/issues/9345"}
| 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:b842fe85-f29a-2a37-ec70-d61eda335562 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1BC:3BD6D2:6C307:96A06:6B007EE1 |
| html-safe-nonce | 6dfd016cbace641ac9716df34e931d30fd8326790f8980889f9395ac0d11672e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUJDOjNCRDZEMjo2QzMwNzo5NkEwNjo2QjAwN0VFMSIsInZpc2l0b3JfaWQiOiI1MjMwNTU3MTU2NTA3NDE4MzM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fe5d80df22dc968e6095cd8ff6b188bb829a388a684d237c68eabdd621643831 |
| hovercard-subject-tag | issue:1487592529 |
| 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/9345/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f83b16f5171324bf2c58b19b12876e76282b552053f0dc02c86d561db6eaf8ea/python/typeshed/issues/9345 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f83b16f5171324bf2c58b19b12876e76282b552053f0dc02c86d561db6eaf8ea/python/typeshed/issues/9345 |
| og:image:alt | @AlexWaygood proposed the following stubs in this comment: python/mypy#14263 (comment) from typing import Any, ClassVar, Protocol from typing_extensions import TypeGuard class _DataclassInstance(Pr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tmke8 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 82c186f13e104004ab7bdb779f3195e59dd38bfde531aaa74b76470fc2462b83 |
| 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 | d1bc2aa1939001689f5ce935982c10a6138f745c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width