Channels: Add type hints

Created on 9 Feb 2018  路  16Comments  路  Source: django/channels

Python 3.5 supports type hints.

Adding these throughout the code base will improve autocomplete etc for many users IDEs

enhancement exintermediate maybe

Most helpful comment

@hundredeir Channels is Python 3.5+. Unless I'm missing something, just using inline code annotations is totally fine.

All 16 comments

And those type hints should be compatible with mypy, ideally.

This is not something I'm going to work on as a priority, as it's very much a "nice to have", but I'll leave this open if someone wants to pick it up.

I will try to have a basic coverage done this weekend for this.

Is anyone working on this?

@hundredeir I was going to but felt that the multiplexing was more important (for my work) so feel free to give it a spin.

Since, django supports python 3.4,3.5; And type annotations for variables were first introduced in python 3.6.
There are two ways left to annotate variables in it. Either create stub file or use comments for annotating.
Which way would be preferred ?

@hundredeir Channels is Python 3.5+. Unless I'm missing something, just using inline code annotations is totally fine.

Hi @carltongibson ,Sorry, it was a typo.
I was talking about PEP526 which was introduced in 3.6. Which allows type annotations for variables.

Type hinting is not all or nothing. Personally, I鈥檇 begin with the function annotations. We can worry about the rest later.

Watch out using typing in Python 3.5. A few of the classes in there didn't exist in the earlier 3.5 releases (example: Queue).

Also note that binary (.exe) builds of newest python 3.5 versions are not available for Windows users.

I'm available in working on a PR to start adding typing to channels, but I don't understand what's the difference in older versions of python. Channels 2 supports python 3.5 and up. Isn't typing available in 3.5?
https://docs.python.org/3.5/library/typing.html

If anybody can tell me the difference between 3.5 and newer versions I can start working on that soon.

Probably the biggest thing introduced in 3.6 is variable annotations.

https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html#variables

Python 3.7 introduces postponed evaluation, which I think is significant:

https://docs.python.org/3/whatsnew/3.7.html#whatsnew37-pep563

Definitely not show stoppers, but, yes, things you do need to be aware of.

Keep in mind that some classes in typing came in later in the 3.5 series (like Queue for example).

So if 3.5.0 is supported you will have to avoid a few classes in typing that are only present in later versions.

https://github.com/django/channels/issues/1282 looks like 3.6 will be the minimum spec soon?

Thank you @brianmay I started working on it and I will post a pr draft shortly for other related questions. I used the mypy docs to see how to treat the awaitables and changed the order of a class declaration for the second point to keep it backward compatible.

@skewty Yes, thank you, I don't think some of those classes are in use in channels directly, or at least I didn't come across them

@hishnash I understand your point of view but please, don't drop other versions of python, it makes it hard for people to stay updated with important updates. As a matter of fact in my opinion too many changes has happened in version 3 that completely change the way of coding

Was this page helpful?
0 / 5 - 0 ratings