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

Switched SortedList base class to Sequence #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pganssle
Copy link

@pganssle pganssle commented Jan 17, 2020

Since SortedList does not implement the MutableSequence interface (many of MutableSequence's mixin methods return NotImplemented), it should not derive from MutableSequence.

This change will allow type checkers to properly reject SortedList as an argument to a function that takes MutableSequence.

This is a breaking change (albeit a minor one).

Fixes #138

@grantjenks
Copy link
Owner

Those methods are a remnant of blist's implementation which include all the MutableSequence methods. It's been helpful, as a drop-in replacement, to have those methods implemented and raising NotImplementedError with useful error messages and doc strings. Raising AttributeError will be much more cryptic for folks.

Otherwise, I don't see much benefit with this change except appeasing type checkers which seems kind of dubious to me. Did you see a specific case where a type checker would have caught your concern?

@pganssle
Copy link
Author

Those methods are a remnant of blist's implementation which include all the MutableSequence methods. It's been helpful, as a drop-in replacement, to have those methods implemented and raising NotImplementedError with useful error messages and doc strings. Raising AttributeError will be much more cryptic for folks.

I have implemented a custom __getattr__ so that the same message is raised, just using AttributeError rather than NotImplementedError. The primary change here is the error type raised - if it raises AttributeError, hasattr and getattr will properly understand that these methods are not implemented, and any isinstance checks that work based on the interface will work correctly.

Otherwise, I don't see much benefit with this change except appeasing type checkers which seems kind of dubious to me. Did you see a specific case where a type checker would have caught your concern?

I have recently been trying to upgrade SortedList on a large code base with a lot of type checking. This would make it much easier to find situations where someone is programming as if something is a list and expecting .extend or .append to work, but what was actually passed was a SortedList.

Same thing with regards to dispatch - people dispatching on MutableSequence expect the thing to actually be a MutableSequence. This is just a mutable Sequence, which is a different thing.

Since SortedList does not implement the MutableSequence interface (many
of MutableSequence's mixin methods return NotImplemented), it should not
derive from MutableSequence.

This change will allow type checkers to properly reject SortedList as an
argument to a function that takes MutableSequence.

This is a breaking change (albeit a minor one).
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.

SortedList is not actually a MutableSequence
2 participants