Title: typeshed annotations do not permit instantiation of urllib.error.HTTPError · Issue #1377 · python/typeshed · GitHub
Open Graph Title: typeshed annotations do not permit instantiation of urllib.error.HTTPError · Issue #1377 · python/typeshed
X Title: typeshed annotations do not permit instantiation of urllib.error.HTTPError · Issue #1377 · python/typeshed
Description: Currently in typeshed, urllib.error.HTTPError is (accurately) annotated as inheriting both URLError and urllib.response.addinfourl. The addinfourl class inherits addbase, which is annotated as inheriting typing.BinaryIO. This appears to ...
Open Graph Description: Currently in typeshed, urllib.error.HTTPError is (accurately) annotated as inheriting both URLError and urllib.response.addinfourl. The addinfourl class inherits addbase, which is annotated as inhe...
X Description: Currently in typeshed, urllib.error.HTTPError is (accurately) annotated as inheriting both URLError and urllib.response.addinfourl. The addinfourl class inherits addbase, which is annotated as inhe...
Opengraph URL: https://github.com/python/typeshed/issues/1377
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"typeshed annotations do not permit instantiation of urllib.error.HTTPError","articleBody":"Currently in typeshed, [urllib.error.HTTPError is (accurately) annotated](https://github.com/python/typeshed/blob/master/stdlib/3/urllib/error.pyi#L8) as inheriting both `URLError` and `urllib.response.addinfourl`. The `addinfourl` class inherits `addbase`, which [is annotated](https://github.com/python/typeshed/blob/master/stdlib/3/urllib/response.pyi#L6) as inheriting [typing.BinaryIO](https://github.com/python/typing/blob/master/src/typing.py#L2312). This appears to be an attempt to deal with the fact that `addbase` [actually inherits](https://github.com/python/cpython/blob/master/Lib/urllib/response.py#L14) the (private) `tempfile._TemporaryFileWrapper`, which is supposed to wrap a \"file-like object\", and [proxies all attribute access to the wrapped object](https://github.com/python/cpython/blob/master/Lib/tempfile.py#L473).\r\n\r\nThis makes some sense: `BinaryIO` is supposed to be the typing \"definition\" of what \"file-like object\" means (at least in binary mode). And this works OK for type-checking code that accesses \"file-like\" methods on an `HTTPError` instance. But it makes it impossible to actually instantiate an `HTTPError` without a spurious mypy error, because the methods of `typing.BinaryIO` (and its parent `typing.IO`) [are all decorated as @abstractmethod](https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi#L393), and most of them are not overridden by the typeshed annotation of `addbase` or `addinfourl`.\r\n\r\nSo if you type-check this code (which works fine at runtime):\r\n\r\n```\r\nfrom io import StringIO\r\nfrom urllib.error import HTTPError\r\n\r\nhttp_error = HTTPError(\"\", 500, \"message\", dict(), StringIO(''))\r\n```\r\n\r\nmypy gives you:\r\n\r\n```\r\nerror: Cannot instantiate abstract class 'HTTPError' with abstract attributes '__enter__', '__exit__', ... and 'writelines' (16 methods suppressed)\r\n```\r\n\r\nI'm filing an issue rather than going straight to pull request because the correct fix here isn't obvious to me, and I'm hoping someone with more typeshed experience can offer a preferred solution. If so, I'm happy to implement it. The obvious solution seems to be repeating all the method definitions from `typing.BinaryIO` and `typing.IO` in the typeshed annotation for `addbase`, without `@abstractmethod`, but I'm hoping there might be an option that avoids this duplication.","author":{"url":"https://github.com/carljm","@type":"Person","name":"carljm"},"datePublished":"2017-06-01T04:10:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/1377/typeshed/issues/1377"}
| 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:3a9b1ec5-c428-05ff-6117-8b4de8746564 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA56:2CFC95:1945F5B:2251778:6A536337 |
| html-safe-nonce | 8f9e921cd252cf86aa4feba0f45fa820139920c5f072a5854fd9cde30f89928f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTU2OjJDRkM5NToxOTQ1RjVCOjIyNTE3Nzg6NkE1MzYzMzciLCJ2aXNpdG9yX2lkIjoiMTAwOTM0MzczODk4MjY1NDc3NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6b9016331513259c8f2b41019aa0107359529575770f051dc7f6d7d0f0e424dc |
| hovercard-subject-tag | issue:232760935 |
| 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/1377/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d5bac1049f216af7e318336d3d0573ff8ec32b542a81bbf6a96a73ccc976b0e3/python/typeshed/issues/1377 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d5bac1049f216af7e318336d3d0573ff8ec32b542a81bbf6a96a73ccc976b0e3/python/typeshed/issues/1377 |
| og:image:alt | Currently in typeshed, urllib.error.HTTPError is (accurately) annotated as inheriting both URLError and urllib.response.addinfourl. The addinfourl class inherits addbase, which is annotated as inhe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | carljm |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width