Title: Adding __init__ breaks type inference from __new__ · Issue #12045 · python/mypy · GitHub
Open Graph Title: Adding __init__ breaks type inference from __new__ · Issue #12045 · python/mypy
X Title: Adding __init__ breaks type inference from __new__ · Issue #12045 · python/mypy
Description: Hello, i'm trying to fix a bug in django type stubs, and i believe i've stubled upon bug in mypy. Django has following syntax for defining foreign keys: class Book(models.Model): author = models.ForeignKey(Author, null=True) This means a...
Open Graph Description: Hello, i'm trying to fix a bug in django type stubs, and i believe i've stubled upon bug in mypy. Django has following syntax for defining foreign keys: class Book(models.Model): author = models.Fo...
X Description: Hello, i'm trying to fix a bug in django type stubs, and i believe i've stubled upon bug in mypy. Django has following syntax for defining foreign keys: class Book(models.Model): author = m...
Opengraph URL: https://github.com/python/mypy/issues/12045
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Adding __init__ breaks type inference from __new__","articleBody":"Hello, i'm trying to fix a bug in django type stubs, and i believe i've stubled upon bug in mypy.\r\n\r\nDjango has following syntax for defining foreign keys:\r\n\r\n```python\r\nclass Book(models.Model):\r\n author = models.ForeignKey(Author, null=True)\r\n```\r\n\r\nThis means author should be `Author | None`.\r\n\r\nRight now django-types are using `@override` for `__init__` with differently typed `self` parameter:\r\n\r\nhttps://github.com/sbdchd/django-types/blob/90fc353b7d05ec600a4807d8b29343a6538692b1/django-stubs/db/models/fields/related.pyi#L293\r\n\r\n```\r\n@overload\r\ndef __init__(\r\n self: ForeignKey[_M],\r\n ...\r\n null: Literal[False],\r\n...\r\n@overload\r\ndef __init__(\r\n self: ForeignKey[Optional[_M]],\r\n ...\r\n null: Literal[True],\r\n...\r\n```\r\n\r\nAnd it works for `mypy`, but doesn't work for `pyright`. In mypy it works becouse it uses undocumented feature of typing self, as i discovered here:\r\n\r\n- https://github.com/microsoft/pyright/issues/2909#issuecomment-1019374993\r\n\r\nProper, and most concise way of doing this, would be overloading `__new__` with args/kwargs and `bool: Literal[...]`, and leaving full annotation with all arguments typed in `__init__` (which i attempted here https://github.com/sbdchd/django-types/pull/86).\r\n\r\nBut, when we try to do this: mypy would ignore overrides in `__new__` and will always use non-optional case.\r\n\r\n**To Reproduce**\r\n\r\nTo reproduce this without much clutter, we can use following code:\r\n\r\n```python\r\n# related_field_minimal.pyi\r\nfrom typing import Generic, Literal, Optional, Type, TypeVar, overload\r\n\r\nclass Model: ...\r\n\r\n_M = TypeVar(\"_M\", bound=Optional[Model])\r\n\r\nclass ForeignKey(Generic[_M]):\r\n @overload\r\n def __new__(\r\n cls,\r\n to: Type[_M],\r\n null: Literal[False] = ...,\r\n ) -> ForeignKey[_M]: ...\r\n @overload\r\n def __new__(\r\n cls,\r\n to: Type[_M],\r\n null: Literal[True],\r\n ) -> ForeignKey[Optional[_M]]: ...\r\n def __init__(self, to: Type[_M], null: bool) -> None: ...\r\n```\r\n\r\n```python\r\n# models.py\r\nfrom typing import Optional\r\nfrom related_field_minimal import ForeignKey, Model\r\n\r\n\r\nclass Test(Model):\r\n pass\r\n\r\n\r\nclass BookModel(Model):\r\n required_field = ForeignKey(to=Test, null=False)\r\n optional_f1 = ForeignKey(to=Test, null=True)\r\n optional_f2 = ForeignKey[Test](to=Test, null=True)\r\n optional_f3 = ForeignKey[Optional[Test]](to=Test, null=True)\r\n\r\nreveal_type(BookModel.required_field)\r\nreveal_type(BookModel.optional_f1)\r\nreveal_type(BookModel.optional_f2)\r\nreveal_type(BookModel.optional_f3)\r\n```\r\n\r\nWhen there is no `__init__` in ForeignKey, inferred types are correct, both in mypy and pyright.\r\n\r\n```\r\nmodels.py:15: note: Revealed type is \"related_field_minimal.ForeignKey[models.Test]\"\r\nmodels.py:16: note: Revealed type is \"related_field_minimal.ForeignKey[Union[models.Test, None]]\"\r\nmodels.py:17: note: Revealed type is \"related_field_minimal.ForeignKey[Union[models.Test, None]]\"\r\nmodels.py:18: note: Revealed type is \"related_field_minimal.ForeignKey[Union[models.Test, None]]\"\r\n```\r\n\r\nAs soon as we add `__init__` to our type definition in `related_field_minimal.pyi`, it becomes incorrect in mypy:\r\n\r\n```\r\nmodels.py:15: note: Revealed type is \"related_field_minimal.ForeignKey[models.Test]\"\r\nmodels.py:16: note: Revealed type is \"related_field_minimal.ForeignKey[models.Test]\"\r\nmodels.py:17: note: Revealed type is \"related_field_minimal.ForeignKey[models.Test]\"\r\nmodels.py:18: note: Revealed type is \"related_field_minimal.ForeignKey[Union[models.Test, None]]\"\r\n```\r\n\r\n(pyright still handles this case correctly).\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.931\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.10\r\n","author":{"url":"https://github.com/last-partizan","@type":"Person","name":"last-partizan"},"datePublished":"2022-01-23T10:48:30Z","url":"https://github.com/python/mypy/issues/12045"}
| 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:4b25b676-cc46-ec0d-ea52-f2b7feb83690 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E444:197995:237D607:302FD08:6B002D05 |
| html-safe-nonce | 57ee0a5d9e3712a6457e91c09cfe21225261e9debc0eeab388ff713096ac5351 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDQ0OjE5Nzk5NToyMzdENjA3OjMwMkZEMDg6NkIwMDJEMDUiLCJ2aXNpdG9yX2lkIjoiODA3MDg0MDAxNDU2MjQwNTYzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 80f20a78ebabfe7e8afe395a5044910b40eacdab08a1865ed3c59755e15cfec9 |
| hovercard-subject-tag | issue:1111835427 |
| 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/12045/issue_layout |
| twitter:image | https://opengraph.githubassets.com/97101d58d9396194f05df7a4e0db629e14a9b4cc72b6f488f451284e918c2f4f/python/mypy/issues/12045 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/97101d58d9396194f05df7a4e0db629e14a9b4cc72b6f488f451284e918c2f4f/python/mypy/issues/12045 |
| og:image:alt | Hello, i'm trying to fix a bug in django type stubs, and i believe i've stubled upon bug in mypy. Django has following syntax for defining foreign keys: class Book(models.Model): author = models.Fo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | last-partizan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width