-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 stubs for sortedcontainers #8574
Comments
Sounds like a great idea to me! There's some instructions in |
Note that there is a PR to sortedcontainers with stubs: grantjenks/python-sortedcontainers#107 We use these in Synapse and not had any problems with them. I did have to update the stubs to be compatible with a typeshed change though: matrix-org/synapse@ |
Thanks for the heads up! I believe, I am already half-way through preparing a fresh stub package for typeshed, which is what the maintainer seams to prefer at this point. My focus lies on applying current typeshed best practices as much as possible. In my eyes, the existing PR looks a bit dated in that respect: use of This is somewhat new territory for me, so I fully expect a few rounds of reviews and changes, but I will cross-check with the existing PR to make sure I incorporate any lessons learned in my work before I push. I am looking forward to getting any feedback once this is out. |
I see several options to address this:
Could someone offer me guidance, please? |
@jakob-keller, there's no ideal solution here, but the following hack works reasonably well, I think: from collections.abc import Callable
from typing import overload
class SortedSet:
@overload
def __new__(cls, key: None = ...) -> SortedSet: ...
@overload
def __new__(cls, key: Callable[[], int]) -> _SortedSetWithExtraMethods: ...
class _SortedSetWithExtraMethods(SortedSet):
def extra_method(self) -> None: ...
reveal_type(SortedSet()) # Revealed type is "SortedSet"
reveal_type(SortedSet(key=lambda: 5)) # Revealed type is "_SortedSetWithExtraMethods" https://mypy-play.net/?mypy=latest&python=3.10&gist=0d151897cc1862ff8671a1acbfb2cbbc The downside is we have to make up this fictional subclass (I've no idea whether the types here are correct; I've only skimmed the source code in the last 5 minutes. This is just a proof of concept to give you some ideas :) |
Perfect, that's what I had in mind as option 3. Just wasn't sure if that level of 'creativity' was permitted here :-D |
Alright, I finally got around to finishing a draft PR #8731. From my point of view, there are a few peculiar details in the way sortedcontainers is implemented that does not quite lend itself to type hints. I tried to deal with it as best as I could. Can't wait for your feedback! |
Sorry, I was unable to complete a PR since I migrated away from sortedcontainers in the meantime. |
What did you migrate to? |
Basically, my use cases are now covered by a combination of regular lists and bisect.insort(), which supports the key argument since Python 3.10. |
For googlers, here is now a |
Hello!
I am interested in adding stubs for sortedcontainers to typeshed. The maintainer, @grantjenks, supports this initiative.
Are there any concerns? Otherwise I would start drafting a PR.
The text was updated successfully, but these errors were encountered: