django-types icon indicating copy to clipboard operation
django-types copied to clipboard

CharField missing `__init__` declaration

Open bschnurr opened this issue 3 years ago • 6 comments

https://github.com/microsoft/pylance-release/issues/3401

bschnurr avatar Sep 28 '22 22:09 bschnurr

Pretty sure we use __new__ instead so that we can make both pyright and mypy happy:

https://cs.github.com/sbdchd/django-types/blob/8b5f4bcbfa3b3ee7663f2b502490f1d4a7021d98/django-stubs/db/models/fields/init.pyi?q=CharField#L687

might be worth revisiting if that's still necessary -- I can't remember which type checker preferred new to init

sbdchd avatar Sep 29 '22 01:09 sbdchd

pyright seems to prefer init.. users not getting correct signatures anymore

bschnurr avatar Oct 06 '22 16:10 bschnurr

I'll have to revert pylance back to the old stubs https://github.com/typeddjango/django-stubs for now

bschnurr avatar Oct 06 '22 19:10 bschnurr

Yeah, we could probably change the types to use __init__ instead of __new__

chdsbd avatar Oct 06 '22 20:10 chdsbd

@bschnurr I've refactored the type definitions to use __init__ in #139, but Pylance is having issues with understanding foreign keys. Mypy seems to work fine though and we do have better autocomplete.

reveal_type(comment.nullable_user_type)
mypy: Revealed type is "Union[tests.trout.models.User, None]"
Pylance: Type of "comment.nullable_user_type" is "_M@ForeignKey | None"

chdsbd avatar Oct 06 '22 22:10 chdsbd

Pylance/pyright have been updated to look at __new__

bschnurr avatar Apr 13 '23 20:04 bschnurr