Title: pymysql: fetch result type · Issue #5567 · python/typeshed · GitHub
Open Graph Title: pymysql: fetch result type · Issue #5567 · python/typeshed
X Title: pymysql: fetch result type · Issue #5567 · python/typeshed
Description: import pymysql connection = pymysql.connect(password='hunter2', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() reveal_type(cursor) # pymysql.cursors.Cursor, but DictCursor at runtime reveal_type(cursor.fetchone()) #...
Open Graph Description: import pymysql connection = pymysql.connect(password='hunter2', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() reveal_type(cursor) # pymysql.cursors.Cursor, but DictCursor at ...
X Description: import pymysql connection = pymysql.connect(password='hunter2', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() reveal_type(cursor) # pymysql.cursors.Cursor, but DictCu...
Opengraph URL: https://github.com/python/typeshed/issues/5567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"pymysql: fetch result type","articleBody":"```python\r\nimport pymysql\r\n\r\nconnection = pymysql.connect(password='hunter2', cursorclass=pymysql.cursors.DictCursor)\r\ncursor = connection.cursor()\r\nreveal_type(cursor) # pymysql.cursors.Cursor, but DictCursor at runtime\r\nreveal_type(cursor.fetchone()) # Union[builtins.tuple[Any], builtins.dict[builtins.str, Any], None]\r\n\r\ncursor.execute(\"select 1 as foo\")\r\nfoo = cursor.fetchone()['foo'] # error: Value of type \"Union[Tuple[Any, ...], Dict[str, Any], None]\" is not indexable\r\n```\r\n\r\nIdeally `cursor` would have type `pymysql.cursors.DictCursor`, so that `cursor.fetchone()` would return `Dict[str, Any]` instead of an annoying union.\r\n\r\nBecause `cursor.fetchone()` can return `None` but often checking for it is annoying, it could instead return `Union[Dict[str, Any], Any]`. See #5528 for a similar situation.\r\n\r\nWe could make `connection` generic and overload `connect()` to figure out what type `connection.cursor()` returns. Something like this (omitting lots of irrelevant stuff):\r\n\r\n```python\r\n_CursorT = TypeVar(\"_CursorT\", bound=pymysql.cursors.Cursor)\r\n\r\nclass Connection(Generic[_CursorT]):\r\n def cursor(self) -> _CursorT: ...\r\n\r\n@overload\r\ndef connect(*, cursorclass: Type[_CursorT]) -> Connection[_CursorT]: ...\r\n@overload\r\ndef connect(*, cursorclass: Type[pymysql.cursors.Cursor] = ...) -> Connection[pymysql.cursors.Cursor]: ...\r\n```\r\n\r\nThe downside is that now all other arguments of `connect()` have to be copy/pasted into two places, and there's many of them.","author":{"url":"https://github.com/Akuli","@type":"Person","name":"Akuli"},"datePublished":"2021-06-03T10:15:53Z","url":"https://github.com/python/typeshed/issues/5567"}
| 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:ab73bb36-b04a-4080-4b4d-cc8cd10f075b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B13A:1F5A:343A18:4CF1E5:6B003920 |
| html-safe-nonce | 814fbb181355e8c1e6256dc8d5517e5497cf7188a9714ca3e7fe5c03f475b653 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMTNBOjFGNUE6MzQzQTE4OjRDRjFFNTo2QjAwMzkyMCIsInZpc2l0b3JfaWQiOiI2ODUyOTM4NjEzOTk5NTQ4NzA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9b9896a33c0c4e710dd5f5238a6b0a728b52e7aef0370847ca253bd86dc58496 |
| hovercard-subject-tag | issue:910343432 |
| 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/5567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff839a4861c2e88efaa9026faedc508c831f98e74d7b7158ffa0002cadfcc056/python/typeshed/issues/5567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff839a4861c2e88efaa9026faedc508c831f98e74d7b7158ffa0002cadfcc056/python/typeshed/issues/5567 |
| og:image:alt | import pymysql connection = pymysql.connect(password='hunter2', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() reveal_type(cursor) # pymysql.cursors.Cursor, but DictCursor at ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Akuli |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width