Title: mypy incorrectly complains about incompatible type error · Issue #18267 · python/mypy · GitHub
Open Graph Title: mypy incorrectly complains about incompatible type error · Issue #18267 · python/mypy
X Title: mypy incorrectly complains about incompatible type error · Issue #18267 · python/mypy
Description: Bug Report mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute. In the example below, we are creating a library of fields (StringField, IntegerField, e...
Open Graph Description: Bug Report mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute. In the example below, we are creating a library...
X Description: Bug Report mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute. In the example below, we are creating a library...
Opengraph URL: https://github.com/python/mypy/issues/18267
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy incorrectly complains about incompatible type error","articleBody":"**Bug Report**\r\n\r\n\u003c!--\r\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\r\n\r\nIf the project you encountered the issue in is open source, please provide a link to the project.\r\n-->\r\n\r\nmypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute.\r\n\r\nIn the example below, we are creating a library of fields (`StringField`, `IntegerField`, etc.), to be used in a runtime schema validation library. In addition, we create a `OneOfField` that takes a list of sub-fields via the `with_` function. Creating a `OneOfField` works fine by itself, but when assigned to a class attribute, mypy incorrectly complains about a type error. \r\n\r\n```python\r\nfrom typing import Generic, TypeVar, Union, assert_type, cast, overload, Self\r\n\r\nInputType = TypeVar(\"InputType\")\r\nOutputType = TypeVar(\"OutputType\")\r\n\r\n\r\nclass Field(Generic[InputType, OutputType]):\r\n # We want both InputType and OutputType to be used in functions like the\r\n # ones below, so they can't be marked covariant or contravariant.\r\n def validate(self, input: Any) -> InputType:\r\n return cast(InputType, input)\r\n \r\n def render(self, input: InputType) -> OutputType:\r\n return cast(OutputType, input)\r\n \r\n def serialize(self, output: OutputType) -> str:\r\n return \"\"\r\n\r\n\r\nclass StringField(Field[str, str]):\r\n pass\r\n\r\n\r\nclass IntegerField(Field[int, int]):\r\n pass\r\n\r\n\r\nclass BooleanField(Field[bool, bool]):\r\n pass\r\n\r\n\r\ndef input_type(field: Field[InputType, OutputType]) -> InputType:\r\n return cast(InputType, field)\r\n\r\n\r\nOneOfInputType = TypeVar(\"OneOfInputType\")\r\nOneOfOutputType = TypeVar(\"OneOfOutputType\")\r\n\r\nAInputType = TypeVar(\"AInputType\")\r\nAOutputType = TypeVar(\"AOutputType\")\r\n\r\nBInputType = TypeVar(\"BInputType\")\r\nBOutputType = TypeVar(\"BOutputType\")\r\n\r\nCInputType = TypeVar(\"CInputType\")\r\nCOutputType = TypeVar(\"COutputType\")\r\n\r\n\r\nclass OneOfField(\r\n Generic[OneOfInputType, OneOfOutputType],\r\n Field[OneOfInputType, OneOfOutputType],\r\n):\r\n fields: list[Field]\r\n\r\n def __init__(self) -> None:\r\n super().__init__()\r\n self.fields = []\r\n\r\n @overload\r\n def with_(\r\n self,\r\n a: Field[AInputType, AOutputType],\r\n b: Field[BInputType, BOutputType],\r\n ) -> \"OneOfField[Union[AInputType, BInputType], Union[AOutputType, BOutputType]]\": ...\r\n\r\n @overload\r\n def with_(\r\n self,\r\n a: Field[AInputType, AOutputType],\r\n b: Field[BInputType, BOutputType],\r\n c: Field[CInputType, COutputType],\r\n ) -> \"OneOfField[Union[AInputType, BInputType, CInputType], Union[AOutputType, BOutputType, COutputType]]\": ...\r\n\r\n def with_(\r\n self,\r\n a: Field[AInputType, AOutputType],\r\n b: Field[BInputType, BOutputType],\r\n c: Field[CInputType, COutputType] | None = None,\r\n ) -> \"OneOfField\":\r\n if c is None:\r\n self.fields = [a, b]\r\n else:\r\n self.fields = [a, b, c]\r\n return self\r\n\r\n def return_self(self) -> Self:\r\n return self\r\n\r\n# This works:\r\nfield = OneOfField().with_(StringField(), IntegerField())\r\nassert_type(field, OneOfField[Union[str, int], Union[str, int]])\r\nassert_type(input_type(field), Union[str, int])\r\n\r\n\r\n# This doesn't work:\r\nclass MyModel:\r\n # error: Argument 1 to \"with_\" of \"OneOfField\" has incompatible type \"StringField\"; expected \"Field[str | int, str | int]\" [arg-type]\r\n # error: Argument 2 to \"with_\" of \"OneOfField\" has incompatible type \"IntegerField\"; expected \"Field[str | int, str | int]\" [arg-type]\r\n foo: str | int = input_type(OneOfField().with_(StringField(), IntegerField()))\r\n \r\n# This works?\r\nclass MyModel2:\r\n foo: str | int = input_type(OneOfField().with_(StringField(), IntegerField()).return_self())\r\n\r\n```\r\n\r\n**To Reproduce**\r\n\r\nhttps://gist.github.com/mypy-play/4619ce6d001d7c7c6994d2b6c912424a\r\n\r\nThis does not repro in Pyright: [link](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDiApikSEgMYA0UAKokQGoCGINAqikmCjcwM78yMAPrwERGhQEwaYAG5kANmGYATGgGUiS4ACg9ASRQIArjHoSoAXjoMWIABQAiY2YsNnASj0B5c%2B6WRDZ2Eg4u-jCBnj4GFEoC-FAAYkg6ao7EpOQUANpu5kE0kdESALpeAFx6ULVQAMRQAOrBAO7MKPgARmAwABZQBR5WHWpQJYUMsGBQXcGmQmOoUMCmKBQw3ChJSkgA1sH9RDV1jTxESXMqrTT8M0dwUNIoAOTdwRCsB2MUCqxIHXwuCePBgIGY8n%2BgIAdCdamoiMAoJDdmpmDAiI4hLoaKh3JUoABBFBwLxQAC0AD5BiZJhJqnVGVAQEQYKYQCgnjJHEMiphaTAfIy4VAEUiWSgEU5scBcQKCbyGGSqeMAnSiAymbUWWyOVz%2BDBHBNhpJ%2Be4hXURWKoEJyMxdgAvTEyuRqmAE41BZXUg0gTVanXsznOZwGPTxRJQTRg1BoVLpRzxpRqXK%2B25gir%2BqAIRJhiOCGkYtBkJMZUu5VCyfkwTMinOCPMJAsAITAYCURA6pcTaWTuR67ZoA6UtcZ9f4YetePMYgYjmAvbUBPLiokxTdQQqFOpq41IsDeukBp5Ar5C-SsT8pF8wF3ISC4Wcvmvt9PMSvRBvnqmtgfrAiL7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MvEoATynjPHBWZgCSJtDwmRnQ5ECgUUafmejw4YzUTHOMmjCw6JYSnXyltrapz0EAA)\r\n\r\n**Expected Behavior**\r\n\r\nNo errors.\r\n\r\n**Actual Behavior**\r\n\r\nmypy raises an invalid error saying the argument to `with_` is invalid.\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: 1.13.0\r\n- Mypy command-line flags: N/A\r\n- Mypy configuration options from `mypy.ini` (and other config files): N/A\r\n- Python version used: 3.13\r\n\r\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\r\n","author":{"url":"https://github.com/athyuttamre","@type":"Person","name":"athyuttamre"},"datePublished":"2024-12-08T04:53:04Z","url":"https://github.com/python/mypy/issues/18267"}
| 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:b4946dc0-e209-18f2-376d-a7e4fb547e3d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB60:3204C8:2940931:35FF856:6B000A7A |
| html-safe-nonce | 4c849f2dd509c77cc53be6e9c9235cf7693b7d2e75897f62343c6927e71d850a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjYwOjMyMDRDODoyOTQwOTMxOjM1RkY4NTY6NkIwMDBBN0EiLCJ2aXNpdG9yX2lkIjoiMTA3NDE3MTY1MzIzNzI0NjU4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 62b4d769a6f74a36fca979723b8e09fed4fd1b5db19916cfae7031d74206c212 |
| hovercard-subject-tag | issue:2724953723 |
| 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/18267/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9cb4e331238c5d697ac36cb987ae336134c85fe264b7ba1b19bf40267c3c3a57/python/mypy/issues/18267 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9cb4e331238c5d697ac36cb987ae336134c85fe264b7ba1b19bf40267c3c3a57/python/mypy/issues/18267 |
| og:image:alt | Bug Report mypy is incorrectly complaining that the arguments to a function have an incompatible type, when used in the context of a class attribute. In the example below, we are creating a library... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | athyuttamre |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width