Title: Suggestion: Add more examples for “Type Hinting Asynchronous Objects” section in chapter 21 · Issue #44 · fluentpython/example-code-2e · GitHub
Open Graph Title: Suggestion: Add more examples for “Type Hinting Asynchronous Objects” section in chapter 21 · Issue #44 · fluentpython/example-code-2e
X Title: Suggestion: Add more examples for “Type Hinting Asynchronous Objects” section in chapter 21 · Issue #44 · fluentpython/example-code-2e
Description: Dear Ramalho, I hope you are well. I'm writing to suggest an improvement for the “Type Hinting Asynchronous Objects” section in Chapter 21 for the next edition. Enhancing Clarity with Examples I believe that including additional examples...
Open Graph Description: Dear Ramalho, I hope you are well. I'm writing to suggest an improvement for the “Type Hinting Asynchronous Objects” section in Chapter 21 for the next edition. Enhancing Clarity with Examples I be...
X Description: Dear Ramalho, I hope you are well. I'm writing to suggest an improvement for the “Type Hinting Asynchronous Objects” section in Chapter 21 for the next edition. Enhancing Clarity with Examples ...
Opengraph URL: https://github.com/fluentpython/example-code-2e/issues/44
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Add more examples for “Type Hinting Asynchronous Objects” section in chapter 21 ","articleBody":"Dear Ramalho,\r\n\r\nI hope you are well. I'm writing to suggest an improvement for the “Type Hinting Asynchronous Objects” section in Chapter 21 for the next edition.\r\n\r\n**Enhancing Clarity with Examples**\r\n\r\nI believe that including additional examples in this section would significantly enhance understanding of how to properly use type hints for asynchronous objects. Here are some specific suggestions:\r\n\r\n* **Coroutines Example:** A beneficial addition would be an example demonstrating type hints for coroutines. The following code showcases how to achieve this:\r\n\r\n```python\r\nimport asyncio\r\nimport socket\r\n\r\nfrom collections.abc import AsyncGenerator, Generator, Coroutine, Callable\r\n\r\n\r\ndef get_ipv4_by_hostname(hostname: str) -\u003e list[str]:\r\n return list(\r\n i[4][0]\r\n for i in \r\n socket.getaddrinfo(hostname, 0)\r\n if i[0] is socket.AddressFamily.AF_INET\r\n and i[1] is socket.SocketKind.SOCK_RAW \r\n )\r\n\r\n\r\nasync def get_ipv4_by_hostname_async(hostname: str) -\u003e list[str]:\r\n return await asyncio.to_thread(get_ipv4_by_hostname, hostname)\r\n\r\n\r\nasync def print_domanin_ip(\r\n domain: str,\r\n get_ip: Callable[[str], Coroutine[None, None, list[str]]]) -\u003e None:\r\n ips = await get_ip(domain)\r\n if ips:\r\n for ip in ips:\r\n print(f'{domain}: {ip}')\r\n else:\r\n print(f'{domain}: No IP found')\r\n print('_' * 20)\r\n\r\n\r\nasync def main() -\u003e None:\r\n await print_domanin_ip('docker.com', get_ipv4_by_hostname_async)\r\n```\r\n\r\n* **AsyncGenerators Example:** The official typing documentation provides an excellent and clear example for AsyncGenerators. Including a similar example in the book would be valuable:\r\n\r\n```python\r\n\r\nasync def echo_round() -\u003e AsyncGenerator[int, float]:\r\n sent = yield 0\r\n while sent \u003e= 0.0:\r\n rounded = await round(sent)\r\n sent = yield rounded\r\n```\r\n\r\nI also guess that there might be a typo on the page 825, the image is provided below:\r\n\r\n\r\nI think “to last” phrase should be removed because `Coroutine` is actually covariant on the third parameter.\r\n","author":{"url":"https://github.com/kamalfarahani","@type":"Person","name":"kamalfarahani"},"datePublished":"2024-05-05T17:29:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/44/example-code-2e/issues/44"}
| 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:a4e2b907-2440-5da1-eb87-58bf1a890fe2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD70:2ECD35:651140:8A0B79:696FF144 |
| html-safe-nonce | d73f8f3142517407c5345540b8b1386cf5687d9dc8be2b810f13561377aaefe1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDcwOjJFQ0QzNTo2NTExNDA6OEEwQjc5OjY5NkZGMTQ0IiwidmlzaXRvcl9pZCI6Ijg0NzIzMDE5MzM1MjIwNTU0OTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 86b8d79b13bff6df3ecc6be50c9701ddf84030d73e939232d3ec4d4c8ac0858f |
| hovercard-subject-tag | issue:2279641027 |
| 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/fluentpython/example-code-2e/44/issue_layout |
| twitter:image | https://opengraph.githubassets.com/01b38700c8c730e1c529481de70a4eabf4f18fc66da674b8233a977153188fe3/fluentpython/example-code-2e/issues/44 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/01b38700c8c730e1c529481de70a4eabf4f18fc66da674b8233a977153188fe3/fluentpython/example-code-2e/issues/44 |
| og:image:alt | Dear Ramalho, I hope you are well. I'm writing to suggest an improvement for the “Type Hinting Asynchronous Objects” section in Chapter 21 for the next edition. Enhancing Clarity with Examples I be... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kamalfarahani |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2b218dbdee134592a2dbfabd454a1070986f1fbedb8334bf06b8f2ccc3449130 |
| turbo-cache-control | no-preview |
| go-import | github.com/fluentpython/example-code-2e git https://github.com/fluentpython/example-code-2e.git |
| octolytics-dimension-user_id | 9216311 |
| octolytics-dimension-user_login | fluentpython |
| octolytics-dimension-repository_id | 176997413 |
| octolytics-dimension-repository_nwo | fluentpython/example-code-2e |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 176997413 |
| octolytics-dimension-repository_network_root_nwo | fluentpython/example-code-2e |
| 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 | bcaac379a58a3ed99a1b0502e2a8f5cfd3a7b54b |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width