python-sortedcontainers
python-sortedcontainers copied to clipboard
Add stub files for SortedList, SortedSet
Hi, I really love this repo and have to submit this pull request!! static typing in python is really a thing now. Modern IDE does static analysis with type hints to autofill and to find code errors. Also, the official static type analysis tool Mypy is wildly accepted.
I think type hinting is especially important to SortedContainers as it's a data structure package and most people will spread the usage across their code. Enabling type hinting will give tremendous support in users' code.
This pull request implemented type hinting for sortedlist as per PEP 518. The process is non-destructive with almost only one additional file.
This is a screenshot with a piece of sample code using the type-enabled SortedList. It's shown that the IDE Pycharm is able to correct basic errors with type hinting.

I can come up with type stubs for SortedDict later
I agree. It’s time to merge type hints. I’d like to put them inline with the code and drop Python 2 support. Would you be willing to do that work?
You also found the other existing work to add type hints. Do yours match theirs?
@grantjenks I added a new stub file for SortedDict.
Do yours match theirs
Yes! we were talking about the same thing in the discussion. There are three ways to add type hints, and they can be mixed.
- Inline (only can be used with python3.5+)
def func(a: int, b: str) -> List[int]
myvar: int = 10 (variable inline typing only with python 3.6 +)
Some more functionalities from build-in typing module are gradually imported in versions >= 3.5. There's an official backport typing-extensions which backports them. However I don't see the necessity for the backport in this repo.
- Inline using comments (all python versions including python2)
def func(a, b): # type: (str, str)->List[int]
myvar = 10 # type: int
- Stub files (all python versions including python2)
Write .pyi stub files as I did here.
So just for type hinting's sake, it's actually not necessary to drop python 2 support. We can write inline comments. A bonus is package mypy comes with handy stub file generation utility stubgen if that's what is desired.
For my pull request, I chose stub files because it looks the most non-invasive and I wish my pull request gets accepted :)
My bad! There are actually two typing backports. One is just called typing, which backports basic typing to python3.4. typing-extensions is for other later introduced functionalities.
I noticed the appveyor build failed cuz of python3.4. I'd like to add typing as install_requires and try again!
Just a reminder, python has officially dropped support for python 3.4, some of the tools I know already moved along, like pytest, colorama. It's a possibility to drop python3.4 too here.
From pytest's documentation:
Python 2.7 EOL has been reached in 2020, with the last release planned for mid-April, 2020. Python 3.4 EOL has been reached in 2019, with the last release made in March, 2019. For those reasons, in Jun 2019 it was decided that pytest 4.6 series will be the last to support Python 2.7 and 3.4.
So python 3.4 actually ends earlier than python2.7
Also I noticed the build for python3.4 in appveyor still failed. It's very concerning that line 45 shows pytest 5.x was installed. As pytest 5.x dropped python3.4 support and pytest 4.x should be used.
I suggest python3.4 support should be dropped. Or we can try to further limit pytest version to 4.x
Any progress on this?
Closing in favor of https://pypi.org/project/sortedcontainers-stubs/