Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stub files for SortedList, SortedSet #136

Closed
wants to merge 7 commits into from

Conversation

Madoshakalaka
Copy link

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.
Screenshot from 2020-01-09 18-34-27

@Madoshakalaka
Copy link
Author

Madoshakalaka commented Jan 10, 2020

I can come up with type stubs for SortedDict later

@Madoshakalaka Madoshakalaka changed the title Add static typing Add static typing for SortedList Jan 10, 2020
@grantjenks
Copy link
Owner

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?

@Madoshakalaka Madoshakalaka changed the title Add static typing for SortedList Add stub files for SortedList, SortedSet Jan 10, 2020
@Madoshakalaka
Copy link
Author

@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.

  1. 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.

  1. Inline using comments (all python versions including python2)

def func(a, b): # type: (str, str)->List[int]

myvar = 10 # type: int

  1. 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 :)

@Madoshakalaka
Copy link
Author

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.

@Madoshakalaka
Copy link
Author

Madoshakalaka commented Jan 10, 2020

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

@demberto
Copy link

Any progress on this?

@grantjenks
Copy link
Owner

Closing in favor of https://pypi.org/project/sortedcontainers-stubs/

@grantjenks grantjenks closed this Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants