-
Notifications
You must be signed in to change notification settings - Fork 57
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
Make Map subscriptable #13
Conversation
Yes, that's the idea.
True. We only need the |
@1st1 Gotcha, thanks for taking the time to explain! I'll try to update the PR soon :) |
…crease refcount for returned type in C implementation
I added fixes that addresses your comments as well as a test case. Perhaps the documentation should be updated with an example as well? |
Looks great! Only a few nits left. |
I have a few questions on the mutation API. Should As for both I'm also curious as to why |
Yes.
Ideally, yes.
I guess using
This is to emulate a "positional-only argument", IIRC. |
I added more type hints, so the mutation API should now be covered as well as the keys, values and items methods. I added a |
class BitmapNode: ... | ||
|
||
|
||
class MapKeys(Generic[K]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess mapping views should derive from corresponding collections.abc
.
class MapKeys(Generic[K]): | |
class MapKeys(Generic[K], collections.abc.KeysView): |
in this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a corresponding generic type that we could use: https://docs.python.org/3/library/typing.html#typing.KeysView
Looking at this table in the collections docs it looks like the views also need to implement __contains__
to fulfill the interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, typing.KeysView
is even better.
The definition can be reduced to
class MapKeys(typing.KeysView[K]):
pass
MapKeys
should implement set operations though to conform to the ABC requirement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it would probably make sense for these classes to conform to those interfaces, but maybe we could leave that to a separate PR? The reason is that I am pretty uncomfortable with writing C. I think the PR in its current scope could make sense as a first step towards a typed interface. What do you think? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to me but @1st1 makes decisions here :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good start. Let's merge as is, I'll test this with EdgeDB and current Mypy to see how this behaves. If there's problems, we'll fix them forward.
Ping me if you need me to rebase or make other changes :) |
Merged. @antonagestam thank you for your hard work! @ambv please test this with edgedb codebase. If all good, I'll release a new version. |
@1st1, so far, so good. Tested with EdgeDB and Mypy, behaves well. |
Alright, I'll schedule a release now. |
immutables@0.10 is out, you can now bump the EdgeDB dep. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late to the party for this PR, but spotted a few things and maybe it's better late than never?
(Currently working on type hints for my https://github.com/jab/bidict library, which is why I'm looking around at other Mapping
implementations' type hints.)
from typing import Union | ||
|
||
|
||
K = TypeVar('K', bound=Hashable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why "bound=Hashable
" here when it does not appear in the KT
TypeVar used along with the Mapping
supertype itself: https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi#L60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my intention here was that having that bound will give the user a type error for non-hashable keys instead of a runtime error.
V = TypeVar('V', bound=Any) | ||
D = TypeVar('D', bound=Any) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the "bound=Any
" here buy anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a matter of style, I prefer to more clearly communicate whenever type vars are unbounded.
|
||
|
||
K = TypeVar('K', bound=Hashable) | ||
V = TypeVar('V', bound=Any) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the V
TypeVar set covariant=True
? That would match https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi#L413 better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that seems reasonable.
Fixes #12