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

Type hints #132

Closed
thijsvandien opened this issue May 2, 2021 · 9 comments
Closed

Type hints #132

thijsvandien opened this issue May 2, 2021 · 9 comments
Labels

Comments

@thijsvandien
Copy link

The library does not yet provide type hints for use with Mypy for example. It would be great to get rid of # type: ignore and enjoy the additional safety.

@SethMMorton
Copy link
Owner

natsort is pretty flexible with types - I am not sure how much help type hints would provide:

def natsort(seq: Iterable[Any], key: Callable[[Any], Any], reverse: bool, alg: int): -> List[Any]:
    ...

Having said that - I am not against it. Is this something you are willing to submitting a PR to implement?

@thijsvandien
Copy link
Author

thijsvandien commented May 3, 2021

Not in the short term, but knowing it would be welcomed, in time I might.

By the way, typing that function would become a lot more useful with generics.

@domdfcoding
Copy link
Contributor

If anyone wants a starting point I made domdfcoding/natsort-stubs for my own use. Feel free to use them.

I think I made with with mypy's stubgen and added what else I could infer by hand. The only issue I ran into was with the ns object. I faked it as an Enum as mypy wasn't keen on the namedtuple.

@SethMMorton
Copy link
Owner

So, I recently started using mypy on a project and have come to be aware how annoying it is if no types are declared. I am motivated to do this.

Based on my reading, it looks like all I have to do is add the annotations in-code, and then create a (blank) file called py.typed in the repository. And that's it? Anything I am missing (from someone who has hopefully done this before)?

@SethMMorton
Copy link
Owner

@domdfcoding Thanks - this will be useful to get me started.

@thethiny
Copy link
Contributor

@SethMMorton I have issued a PR that is working for me (only on Python 3.7+, for <3.7 it needs to be typing_extensions instead of typing). You can take a look at it and implement something similar!

@SethMMorton
Copy link
Owner

@domdfcoding This is amazing. I will shamelessly steal a lot of your work :)

@SethMMorton
Copy link
Owner

Closed with #138.

@thijsvandien
Copy link
Author

Congratulations on getting this done so swiftly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants