GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Adding __init__ breaks type inference from __new__ · Issue #12045 · python/mypy Open Labels bugmypy got something wrong DescriptionHello, 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 author should be Right now django-types are using And it works for Proper, and most concise way of doing this, would be overloading But, when we try to do this: mypy would ignore overrides in To Reproduce To reproduce this without much clutter, we can use following code: # related_field_minimal.pyi from typing import Generic, Literal, Optional, Type, TypeVar, overload class Model: ... _M = TypeVar("_M", bound=Optional[Model]) class ForeignKey(Generic[_M]): @overload def __new__( cls, to: Type[_M], null: Literal[False] = ..., ) -> ForeignKey[_M]: ... @overload def __new__( cls, to: Type[_M], null: Literal[True], ) -> ForeignKey[Optional[_M]]: ... def __init__(self, to: Type[_M], null: bool) -> None: ... # models.py from typing import Optional from related_field_minimal import ForeignKey, Model class Test(Model): pass class BookModel(Model): required_field = ForeignKey(to=Test, null=False) optional_f1 = ForeignKey(to=Test, null=True) optional_f2 = ForeignKey[Test](to=Test, null=True) optional_f3 = ForeignKey[Optional[Test]](to=Test, null=True) reveal_type(BookModel.required_field) reveal_type(BookModel.optional_f1) reveal_type(BookModel.optional_f2) reveal_type(BookModel.optional_f3) When there is no As soon as we add (pyright still handles this case correctly). Your Environment
Reacted by Sascha Desch Activity
MetadataMetadataAssigneesLabelsbugmypy got something wrong ProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |