Have various classes inherit from Generic for type checking #7768
Replies: 6 comments
-
hey @carltongibson, what's the position on Dango about typing ? |
Beta Was this translation helpful? Give feedback.
-
@xordoquy Currently we're not adding type hints. We added a couple of these class get item methods after some consideration. There's now a suggestion (from @bradykieffer) to add some more. I'm sceptical about that, as it was specifically discussed in the original pass, and said not to be needed. I'm currently trying to work out if there's a way around adding, essentially no-op, base implementations of these methods. It seems to me to be a source of boilerplate that should be pushed back upstream with the message Do better. Ref subclassing from Again here, why aren't we pushing back to the typing community? Is adding all this cruft really the best we can do? Is it really necessary? In all the PEPs typing was meant to be optional, not even required by convention, and yet it seems to be failing on that. Multiple requests to add hints, otherwise we can't X or Y or Z. Why aren't we pushing that back to the typing community? |
Beta Was this translation helpful? Give feedback.
-
@carltongibson I get where you're coming from, I just had these patches locally so figured I would push them up for comment instead of nuking the code. I'm 100% fine with these patches getting rejected, I just thought that inheriting from generic would be a better solution than having a no-op In the meantime, to save you sanity, could we link to the issues I raised + the mailing list on typing somewhere so that other people don't submit similar patches? |
Beta Was this translation helpful? Give feedback.
-
Hey @bradykieffer. Sure yes! We've been round the block a few times here over the last few days. I'm pretty sure you understand my doubts, and I'm genuinely interested to see what the recommended approach would be... -- Maybe a few "hey this isn't really working for us as described" might lead to better solutions, we might hope. 🙂 Thanks for your concern and input! |
Beta Was this translation helpful? Give feedback.
-
Hopefully when everything calms down some day we can come up with a path forward on this at PyCon 😄 . Where would you want that type of note? In the docstring for the currently implemented |
Beta Was this translation helpful? Give feedback.
-
@bradykieffer Sounds as good a place as any, yes. |
Beta Was this translation helpful? Give feedback.
-
Checklist
master
branch of Django REST framework.Steps to reproduce
Internally we've been using
djangorestframework-stubs
(link here) and it's been really great at helping us avoid some weird classes of errors. My proposal would be to have the following classes inherit fromGeneric
as they do withindjangorestframework-stubs
:This is definitely related to #7385 which added
__class_getitem__
to theBaseSerializer
class.Pros
__class_getitem__
to any generic classGeneric[KeyType, ValueType]
will raise an exception if not typed with two typed parametersGeneric
was introduced in Python 3.5, which is the oldest supported version of Python with DRFGeneric
to ensure that type annotations are indeed valid typesCons
mypy
checks aren't enforced on DRFGeneric
, so we'd need special handling for as long as Python 3.5 and 3.6 are supported by the projectMyClass[int]
not being the same asMyClass
(which definitely feels correct) and it breaks current functionalityI have a patch that I'll link to this ticket, I don't think there's really a burden on implementation here 😄
Beta Was this translation helpful? Give feedback.
All reactions