Title: mypy shows type error with json.dump(s) and kwargs · Issue #8772 · python/mypy · GitHub
Open Graph Title: mypy shows type error with json.dump(s) and kwargs · Issue #8772 · python/mypy
X Title: mypy shows type error with json.dump(s) and kwargs · Issue #8772 · python/mypy
Description: I have noticed that in the seemingly common situation of passing **kwargs to json.dump or json.dumps, mypy fails the type checks unless the kwargs are explicitly annotated as Dict[str, Any]. A simple example: import json json_kwargs = di...
Open Graph Description: I have noticed that in the seemingly common situation of passing **kwargs to json.dump or json.dumps, mypy fails the type checks unless the kwargs are explicitly annotated as Dict[str, Any]. A simp...
X Description: I have noticed that in the seemingly common situation of passing **kwargs to json.dump or json.dumps, mypy fails the type checks unless the kwargs are explicitly annotated as Dict[str, Any]. A simp...
Opengraph URL: https://github.com/python/mypy/issues/8772
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy shows type error with json.dump(s) and kwargs","articleBody":"I have noticed that in the seemingly common situation of passing `**kwargs` to `json.dump` or `json.dumps`, `mypy` fails the type checks unless the kwargs are explicitly annotated as `Dict[str, Any]`. A simple example:\r\n\r\n```python\r\nimport json\r\n\r\njson_kwargs = dict(indent=2)\r\n\r\njson.dumps({}, **json_kwargs)\r\n```\r\n\r\nWhich results in:\r\n\r\n```\r\n$ mypy test.py \r\ntest.py:6: error: Argument 2 to \"dumps\" has incompatible type \"**Dict[str, int]\"; expected \"bool\"\r\ntest.py:6: error: Argument 2 to \"dumps\" has incompatible type \"**Dict[str, int]\"; expected \"Optional[Type[JSONEncoder]]\"\r\ntest.py:6: error: Argument 2 to \"dumps\" has incompatible type \"**Dict[str, int]\"; expected \"Optional[Tuple[str, str]]\"\r\ntest.py:6: error: Argument 2 to \"dumps\" has incompatible type \"**Dict[str, int]\"; expected \"Optional[Callable[[Any], Any]]\"\r\nFound 4 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nIf I change the example to this:\r\n\r\n```python\r\nimport json\r\n\r\njson_kwargs = dict(indent=2, separators=(\",\", \";\"))\r\n\r\njson.dumps({}, **json_kwargs)\r\n```\r\n\r\nThe errors are the same, except the inferred type changes to `**Dict[str, object]`. I can work around it like so:\r\n\r\n```python\r\nimport json\r\nfrom typing import Any, Dict\r\n\r\njson_kwargs : Dict[str, Any] = dict(indent=2, separators=(\",\", \";\"))\r\n\r\njson.dumps({}, **json_kwargs)\r\n```\r\n\r\nUsing Python 3.8 and `mypy == 0.770` on Arch Linux. The error is still present when using the current master (commit 2a3de7b7941f24b6bc53fe0cf8a2b7f8f7fd4e15).\r\n\r\nI have also tested this with `pytype` and `pytype` does not have the same failure.","author":{"url":"https://github.com/pganssle","@type":"Person","name":"pganssle"},"datePublished":"2020-05-04T15:02:28Z","url":"https://github.com/python/mypy/issues/8772"}
| 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:f01a199f-a5b3-ff22-336e-311ddb635e1c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B46:1833B7:1BBA76B:2556F7B:6B00038E |
| html-safe-nonce | d1330e68f06e1f86c8421a85d0584689e3a3ec711d4d02d3c6cee6bf6871749e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjQ2OjE4MzNCNzoxQkJBNzZCOjI1NTZGN0I6NkIwMDAzOEUiLCJ2aXNpdG9yX2lkIjoiODM3NjA2Mjg4NzY1NDg1MTQ3MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 74db96ec37a5b08bf3cee1976a5773996b0bce0732ea81f6cf866b341806321b |
| hovercard-subject-tag | issue:611944405 |
| 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/8772/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5f5c91178ed1decded3c22c10f68a46d48234d5539384dee22467c70b59c4565/python/mypy/issues/8772 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5f5c91178ed1decded3c22c10f68a46d48234d5539384dee22467c70b59c4565/python/mypy/issues/8772 |
| og:image:alt | I have noticed that in the seemingly common situation of passing **kwargs to json.dump or json.dumps, mypy fails the type checks unless the kwargs are explicitly annotated as Dict[str, Any]. A simp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pganssle |
| 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