Title: Pygments `Formatter.__init__` overloads do not work as intended · Issue #7436 · python/typeshed · GitHub
Open Graph Title: Pygments `Formatter.__init__` overloads do not work as intended · Issue #7436 · python/typeshed
X Title: Pygments `Formatter.__init__` overloads do not work as intended · Issue #7436 · python/typeshed
Description: In #6819 pygments.formatter.Formatter was made generic to improve the type safety: _T = TypeVar("_T", str, bytes) class Formatter(Generic[_T]): @overload def __init__(self: Formatter[str], *, encoding: None = ..., outencoding: None = ......
Open Graph Description: In #6819 pygments.formatter.Formatter was made generic to improve the type safety: _T = TypeVar("_T", str, bytes) class Formatter(Generic[_T]): @overload def __init__(self: Formatter[str], *, encod...
X Description: In #6819 pygments.formatter.Formatter was made generic to improve the type safety: _T = TypeVar("_T", str, bytes) class Formatter(Generic[_T]): @overload def __init__(self: Formatter[str]...
Opengraph URL: https://github.com/python/typeshed/issues/7436
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pygments `Formatter.__init__` overloads do not work as intended","articleBody":"In #6819 `pygments.formatter.Formatter` was made generic to improve the type safety:\r\n\r\n```python\r\n_T = TypeVar(\"_T\", str, bytes)\r\n\r\nclass Formatter(Generic[_T]):\r\n @overload\r\n def __init__(self: Formatter[str], *, encoding: None = ..., outencoding: None = ..., **options) -> None: ...\r\n @overload\r\n def __init__(self: Formatter[bytes], *, encoding: str, outencoding: None = ..., **options) -> None: ...\r\n @overload\r\n def __init__(self: Formatter[bytes], *, encoding: None = ..., outencoding: str, **options) -> None: ...\r\n```\r\n\r\nApparently this was never tested, since it does not actually work:\r\n\r\n```python\r\nfrom pygments.formatters.html import HtmlFormatter\r\n\r\nreveal_type(HtmlFormatter())\r\nreveal_type(HtmlFormatter(encoding='utf-8'))\r\n```\r\n\r\n```\r\n$ mypy foo.py\r\nfoo.py:3: note: Revealed type is \"pygments.formatters.html.HtmlFormatter[builtins.str*]\"\r\nfoo.py:4: note: Revealed type is \"pygments.formatters.html.HtmlFormatter[builtins.str*]\"\r\n$ pyright foo.py\r\n /tmp/foo.py:3:13 - information: Type of \"HtmlFormatter(encoding='utf-8')\" is \"HtmlFormatter[Unknown]\"\r\n /tmp/foo.py:4:13 - information: Type of \"HtmlFormatter()\" is \"HtmlFormatter[Unknown]\"\r\n```\r\n\r\nThe intention of the overloads is that `HtmlFormatter(encoding='utf-8')` should be of type `HtmlFormatter[builtins.bytes]`.\r\n\r\nI am not sure if this is a mypy bug or if such overload inference should even work in the first place (given that pyright also infers the type variable to be `Unknown`: https://github.com/microsoft/pyright/issues/3146).","author":{"url":"https://github.com/not-my-profile","@type":"Person","name":"not-my-profile"},"datePublished":"2022-03-04T11:36:30Z","url":"https://github.com/python/typeshed/issues/7436"}
| 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:84214755-b300-42dc-45cf-a60ae335fdff |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE0A:22FAD4:215B1:2CE41:6B0019DB |
| html-safe-nonce | 9016c86091b955398a6d71832eb0cc33e9f8ac1c7ac32403c6cc7c828a242092 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTBBOjIyRkFENDoyMTVCMToyQ0U0MTo2QjAwMTlEQiIsInZpc2l0b3JfaWQiOiI0ODk3ODA4NzAxMzAzMjk4NTI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a720c585c82d9df4de79a6707f0bc7e6776a7a43ce902684338508f2c5a90d61 |
| hovercard-subject-tag | issue:1159550810 |
| 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/7436/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5e109a5836f2b019884196bf09b6ff904329913a080109a9ad99621dbe13f985/python/typeshed/issues/7436 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5e109a5836f2b019884196bf09b6ff904329913a080109a9ad99621dbe13f985/python/typeshed/issues/7436 |
| og:image:alt | In #6819 pygments.formatter.Formatter was made generic to improve the type safety: _T = TypeVar("_T", str, bytes) class Formatter(Generic[_T]): @overload def __init__(self: Formatter[str], *, encod... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | not-my-profile |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width