Title: how to correctly create a dict subclass with typing · python/typing · Discussion #1033 · GitHub
Open Graph Title: how to correctly create a dict subclass with typing · python/typing · Discussion #1033
X Title: how to correctly create a dict subclass with typing · python/typing · Discussion #1033
Description: how to correctly create a dict subclass with typing
Open Graph Description: we have a lot of classes that subclass dict for the purpose of storing keys and values in different ways, providing different ways of returning defaults, etc. I'm not able to create a custom subcla...
X Description: we have a lot of classes that subclass dict for the purpose of storing keys and values in different ways, providing different ways of returning defaults, etc. I'm not able to create a custom su...
Opengraph URL: https://github.com/python/typing/discussions/1033
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"how to correctly create a dict subclass with typing","text":"\u003cp dir=\"auto\"\u003ewe have a lot of classes that subclass \u003ccode class=\"notranslate\"\u003edict\u003c/code\u003e for the purpose of storing keys and values in different ways, providing different ways of returning defaults, etc.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eI'm not able to create a custom subclass of \u003ccode class=\"notranslate\"\u003etyping.Dict\u003c/code\u003e where all methods are accepted by mypy.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eCurrently, trying to figure out the \u003ccode class=\"notranslate\"\u003e.values()\u003c/code\u003e method. Mypy insists that the return type here be the \u003ccode class=\"notranslate\"\u003edict_values\u003c/code\u003e type. However this is a private Python type; while it seems to be somewhat part of \"builtins\", it's not actually there and you can't import it without typing and runtime errors.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eWhat's the correct return type for \u003ccode class=\"notranslate\"\u003edict.values()\u003c/code\u003e, or is it no longer good practice to subclass Dict and I need to instead create a wrapping object with a real dict on the inside (this would be very unfortunate and surprising?) ? Or am I supposed to build a custom protocol that matches dict_values somehow? typing.ValuesView is not accepted if you are subclassing \u003ccode class=\"notranslate\"\u003eDict\u003c/code\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eexample below\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"# Module \u0026quot;builtins\u0026quot; has no attribute \u0026quot;dict_values\u0026quot;\nfrom builtins import dict_values # type: ignore\n\nimport time\nfrom typing import Any\nfrom typing import cast\nfrom typing import Dict, List\nfrom typing import Tuple\nfrom typing import TypeVar\nfrom typing import ValuesView\n\n\n_KT = TypeVar(\u0026quot;_KT\u0026quot;, bound=Any)\n_VT = TypeVar(\u0026quot;_VT\u0026quot;, bound=Any)\n\n\n\nclass MyDict(Dict[_KT, _VT]):\n \u0026quot;\u0026quot;\u0026quot;a dictionary that stores values as a two-tuple with a timestamp.\n\n the exposed values however are those that were originally passed in.\n\n \u0026quot;\u0026quot;\u0026quot;\n\n def __getitem__(self, __k: _KT) -\u0026gt; _VT:\n super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\n try:\n value = super_dict.__getitem__(__k)\n except KeyError:\n raise\n else:\n return value[0]\n\n def __setitem__(self, __k: _KT, v: _VT) -\u0026gt; None:\n super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\n return super_dict.__setitem__(__k, (v, time.time()))\n\n # values like a normal person - return a list\n # Return type \u0026quot;List[_VT]\u0026quot; of \u0026quot;values\u0026quot; incompatible with return type \u0026quot;dict_values[_KT, _VT]\u0026quot; in supertype \u0026quot;dict\u0026quot;\n # Return type \u0026quot;List[_VT]\u0026quot; of \u0026quot;values\u0026quot; incompatible with return type \u0026quot;ValuesView[_VT]\u0026quot; in supertype \u0026quot;Mapping\u0026quot;\n\n # def values(self) -\u0026gt; List[_VT]:\n # super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\n # return [v[0] for k, v in super_dict.items()]\n\n # try to use real ValuesView\n # Return type \u0026quot;ValuesView[_VT]\u0026quot; of \u0026quot;values\u0026quot; incompatible with return type \u0026quot;dict_values[_KT, _VT]\u0026quot; in supertype \u0026quot;dict\u0026quot;\n\n # def values(self) -\u0026gt; ValuesView[_VT]:\n # super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\n # return ValuesView({k: v[0] for k, v in super_dict.items()})\n\n # try to use the dict_values mypy is claiming -\n # not possible, because we cannot import private dict_values\n def values(self) -\u0026gt; dict_values[_KT, _VT]:\n super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\n return ValuesView({k: v[0] for k, v in super_dict.items()})\n\nmd: MyDict[str, str] = MyDict()\n\n# fails, revealed type is Any, because we had to ignore type\n# on dict_values\nreveal_type(md.values())\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-c\"\u003e# Module \"builtins\" has no attribute \"dict_values\"\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ebuiltins\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003edict_values\u003c/span\u003e \u003cspan class=\"pl-c\"\u003e# type: ignore\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etime\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eAny\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ecast\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eDict\u003c/span\u003e, \u003cspan class=\"pl-v\"\u003eList\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eTuple\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eTypeVar\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eValuesView\u003c/span\u003e\n\n\n\u003cspan class=\"pl-s1\"\u003e_KT\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eTypeVar\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"_KT\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ebound\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-v\"\u003eAny\u003c/span\u003e)\n\u003cspan class=\"pl-s1\"\u003e_VT\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eTypeVar\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"_VT\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ebound\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-v\"\u003eAny\u003c/span\u003e)\n\n\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eMyDict\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eDict\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_VT\u003c/span\u003e]):\n \u003cspan class=\"pl-s\"\u003e\"\"\"a dictionary that stores values as a two-tuple with a timestamp.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e the exposed values however are those that were originally passed in.\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e\u003c/span\u003e\n\u003cspan class=\"pl-s\"\u003e \"\"\"\u003c/span\u003e\n\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__getitem__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e__k\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003e_KT\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003e_VT\u003c/span\u003e:\n \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ecast\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eDict\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-v\"\u003eTuple\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_VT\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003efloat\u003c/span\u003e]], \u003cspan class=\"pl-en\"\u003esuper\u003c/span\u003e())\n \u003cspan class=\"pl-k\"\u003etry\u003c/span\u003e:\n \u003cspan class=\"pl-s1\"\u003evalue\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003e__getitem__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003e__k\u003c/span\u003e)\n \u003cspan class=\"pl-k\"\u003eexcept\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eKeyError\u003c/span\u003e:\n \u003cspan class=\"pl-k\"\u003eraise\u003c/span\u003e\n \u003cspan class=\"pl-k\"\u003eelse\u003c/span\u003e:\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalue\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e]\n\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__setitem__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e__k\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003e_VT\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eNone\u003c/span\u003e:\n \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ecast\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eDict\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-v\"\u003eTuple\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_VT\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003efloat\u003c/span\u003e]], \u003cspan class=\"pl-en\"\u003esuper\u003c/span\u003e())\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003e__setitem__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003e__k\u003c/span\u003e, (\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etime\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003etime\u003c/span\u003e()))\n\n \u003cspan class=\"pl-c\"\u003e# values like a normal person - return a list\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# Return type \"List[_VT]\" of \"values\" incompatible with return type \"dict_values[_KT, _VT]\" in supertype \"dict\"\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# Return type \"List[_VT]\" of \"values\" incompatible with return type \"ValuesView[_VT]\" in supertype \"Mapping\"\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e# def values(self) -\u0026gt; List[_VT]:\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# return [v[0] for k, v in super_dict.items()]\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e# try to use real ValuesView\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# Return type \"ValuesView[_VT]\" of \"values\" incompatible with return type \"dict_values[_KT, _VT]\" in supertype \"dict\"\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e# def values(self) -\u0026gt; ValuesView[_VT]:\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# super_dict = cast(Dict[_KT, Tuple[_VT, float]], super())\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# return ValuesView({k: v[0] for k, v in super_dict.items()})\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e# try to use the dict_values mypy is claiming -\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# not possible, because we cannot import private dict_values\u003c/span\u003e\n \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003evalues\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003edict_values\u003c/span\u003e[\u003cspan class=\"pl-smi\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-smi\"\u003e_VT\u003c/span\u003e]:\n \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ecast\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eDict\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_KT\u003c/span\u003e, \u003cspan class=\"pl-v\"\u003eTuple\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003e_VT\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003efloat\u003c/span\u003e]], \u003cspan class=\"pl-en\"\u003esuper\u003c/span\u003e())\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eValuesView\u003c/span\u003e({\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e: \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e[\u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e] \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ein\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003esuper_dict\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eitems\u003c/span\u003e()})\n\n\u003cspan class=\"pl-s1\"\u003emd\u003c/span\u003e: \u003cspan class=\"pl-v\"\u003eMyDict\u003c/span\u003e[\u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e, \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e] \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eMyDict\u003c/span\u003e()\n\n\u003cspan class=\"pl-c\"\u003e# fails, revealed type is Any, because we had to ignore type\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e# on dict_values\u003c/span\u003e\n\u003cspan class=\"pl-en\"\u003ereveal_type\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003emd\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003evalues\u003c/span\u003e())\u003c/pre\u003e\u003c/div\u003e","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eYeah this was broken in 3.10, see here: \u003ca href=\"https://bugs.python.org/issue46399\" rel=\"nofollow\"\u003ehttps://bugs.python.org/issue46399\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eWe're trying to figure out some solutions!\u003c/p\u003e","upvoteCount":2,"url":"https://github.com/python/typing/discussions/1033#discussioncomment-1986298"}}}
| 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 | /:user_id/:repository/discussions/:number(.:format) |
| route-controller | discussions |
| route-action | show |
| fetch-nonce | v2:43f4ece1-6325-4601-f0bc-8eac49b3dccf |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | A68E:2A0045:3014279:3F82D40:6B002A4E |
| html-safe-nonce | 312d64ebb7a144a9ca063da4d9733d28a78809d2891ea79e7344ba5ccecf8164 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjhFOjJBMDA0NTozMDE0Mjc5OjNGODJENDA6NkIwMDJBNEUiLCJ2aXNpdG9yX2lkIjoiOTkwMDg2ODIzNjI1NjMyMzM0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1af956115319b30cbc1a7c039cc4892c69dae941a1844c5649e524df35022843 |
| hovercard-subject-tag | discussion:3806199 |
| github-keyboard-shortcuts | repository,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/python/typing/discussions/1033 |
| twitter:image | https://opengraph.githubassets.com/8438a486224eaac7eacaabcc0801e9ccff468c933b1a4f34dcb69d21922ba7c7/python/typing/discussions/1033 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8438a486224eaac7eacaabcc0801e9ccff468c933b1a4f34dcb69d21922ba7c7/python/typing/discussions/1033 |
| og:image:alt | we have a lot of classes that subclass dict for the purpose of storing keys and values in different ways, providing different ways of returning defaults, etc. I'm not able to create a custom subcla... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| 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 |
Links:
Viewport: width=device-width