Title: Edge case of invalid `TypeVar` inference with `Generic` · Issue #16075 · python/mypy · GitHub
Open Graph Title: Edge case of invalid `TypeVar` inference with `Generic` · Issue #16075 · python/mypy
X Title: Edge case of invalid `TypeVar` inference with `Generic` · Issue #16075 · python/mypy
Description: Bug Report It is not possible to infer the type of a TypeVar declared in a Generic with another TypeVar in __init__ by adding a type hint to self. To Reproduce from typing import Any, Generic, TypeVar, overload DTOPacketT = TypeVar("DTOP...
Open Graph Description: Bug Report It is not possible to infer the type of a TypeVar declared in a Generic with another TypeVar in __init__ by adding a type hint to self. To Reproduce from typing import Any, Generic, Type...
X Description: Bug Report It is not possible to infer the type of a TypeVar declared in a Generic with another TypeVar in __init__ by adding a type hint to self. To Reproduce from typing import Any, Generic, Type...
Opengraph URL: https://github.com/python/mypy/issues/16075
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Edge case of invalid `TypeVar` inference with `Generic`","articleBody":"**Bug Report**\r\n\r\nIt is not possible to infer the type of a `TypeVar` declared in a `Generic` with another `TypeVar` in `__init__` by adding a type hint to `self`.\r\n\r\n**To Reproduce**\r\n\r\n```py\r\nfrom typing import Any, Generic, TypeVar, overload\r\n\r\nDTOPacketT = TypeVar(\"DTOPacketT\")\r\n\r\nSentPacketT = TypeVar(\"SentPacketT\")\r\nReceivedPacketT = TypeVar(\"ReceivedPacketT\")\r\n\r\nclass Serializer(Generic[DTOPacketT]):\r\n ...\r\n \r\n\r\nclass Converter(Generic[SentPacketT, ReceivedPacketT, DTOPacketT]):\r\n ...\r\n \r\n\r\nclass Protocol(Generic[SentPacketT, ReceivedPacketT]):\r\n @overload\r\n def __init__(\r\n self: Protocol[DTOPacketT, DTOPacketT],\r\n serializer: Serializer[DTOPacketT],\r\n converter: None = ...,\r\n ) -> None:\r\n ...\r\n\r\n @overload\r\n def __init__(\r\n self,\r\n serializer: Serializer[DTOPacketT],\r\n converter: Converter[SentPacketT, ReceivedPacketT, DTOPacketT],\r\n ) -> None:\r\n ...\r\n\r\n def __init__(\r\n self,\r\n serializer: Serializer[Any],\r\n converter: Converter[SentPacketT, ReceivedPacketT, Any] | None = None,\r\n ) -> None:\r\n ...\r\n\r\n# This case works\r\nserializer: Serializer[str] = Serializer()\r\nconverter: Converter[dict[str, Any], dict[str, Any], str] = Converter()\r\n\r\nprotocol_with_converter = Protocol(serializer, converter=converter)\r\nreveal_type(protocol_with_converter) # Revealed type is \"__main__.Protocol[builtins.dict[builtins.str, Any], builtins.dict[builtins.str, Any]]\"\r\n\r\n# This case fails\r\nprotocol_without_converter = Protocol(serializer, converter=None)\r\nreveal_type(protocol_without_converter)\r\n```\r\n\r\nThere is a mypy-play available here:\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.11&gist=282865693449e3786dde94d2f45bfb30\r\n\r\n**Expected Behavior**\r\n`protocol_without_converter` should be inferred as `Protocol[str, str]`\r\n\r\n**Actual Behavior**\r\n```\r\nmain.py:49: error: Argument 1 to \"Protocol\" has incompatible type \"Serializer[str]\"; expected \"Serializer[\u003cnothing>]\" [arg-type]\r\nmain.py:50: note: Revealed type is \"__main__.Protocol[DTOPacketT`-1, DTOPacketT`-1]\"\r\n```\r\n\r\nEDIT: A workaround to get this to work is to do the following:\r\n\r\n```py\r\n @overload\r\n def __init__(\r\n self,\r\n serializer: Serializer[SentPacketT | ReceivedPacketT],\r\n converter: None = ...,\r\n ) -> None:\r\n ...\r\n```\r\n\r\nMypy playground: https://mypy-play.net/?mypy=latest&python=3.11&gist=77ee74bfd5e80a91e3c0ff654a502d1f\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.5.1\r\n- Mypy command-line flags: (none)\r\n- Mypy configuration options from `mypy.ini` (and other config files): (none)\r\n- Python version used: 3.11.2\r\n","author":{"url":"https://github.com/francis-clairicia","@type":"Person","name":"francis-clairicia"},"datePublished":"2023-09-09T08:35:21Z","url":"https://github.com/python/mypy/issues/16075"}
| 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:89089de8-12aa-3814-4e13-7831bf1f5579 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C5F4:2DB972:25205C0:307DED8:6AFFF853 |
| html-safe-nonce | 40f25206f31273ec1d878372b2c058bf7047aeabc3387fdf710e20afab52161a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNUY0OjJEQjk3MjoyNTIwNUMwOjMwN0RFRDg6NkFGRkY4NTMiLCJ2aXNpdG9yX2lkIjoiNDA4MzA5NzgwNjYyMzk5NTk4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b8a813233ec3ca1508250a929a16ecf7589ff89b10f32f9dffbf3b39cb5fa685 |
| hovercard-subject-tag | issue:1888646035 |
| 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/16075/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8891301663c219c0d65ecc32a8064e15e1271282197127519935c918fd242ace/python/mypy/issues/16075 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8891301663c219c0d65ecc32a8064e15e1271282197127519935c918fd242ace/python/mypy/issues/16075 |
| og:image:alt | Bug Report It is not possible to infer the type of a TypeVar declared in a Generic with another TypeVar in __init__ by adding a type hint to self. To Reproduce from typing import Any, Generic, Type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | francis-clairicia |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width