-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve collections.Counter
stub
#7464
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
stdlib/collections/__init__.pyi
Outdated
def __or__(self, other: Counter[_T]) -> Counter[_T]: ... # type: ignore[override] | ||
def __add__(self, other: Counter[_S]) -> Counter[_T | _S]: ... | ||
def __sub__(self, other: Counter[Any]) -> Counter[_T]: ... | ||
def __and__(self, other: Counter[Any]) -> Counter[_T]: ... |
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 don't think we should change to Any
, unless users complain. With Any
, you won't get an error for counter_of_ints - counter_of_strings
, which is bad IMO. With _T
, you will get an error for counter_of_strings - counter_of_strings_or_nones
, which apparently doesn't come up in practice because nobody has complained about it.
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.
The same argument surely applies for Abstract Set.__sub__
:
Line 841 in c987c78
def __sub__(self, other: AbstractSet[Any]) -> AbstractSet[_T_co]: ... |
Are we saying that in an ideal world, that also wouldn't be AbstractSet[Any]
, but it would probably be too disruptive to change it now?
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.
Changing AbstractSet
would be more disruptive, but the argument doesn't apply to AbstractSet
as much as it does to Counter
s:
- It's common to have sets that contain
None
s and subtract between them, which I think is the most common use for subtracting sets of different types. I use sets containingNone
s all the time. - A counter that contains
None
s would be something like{None: 2, "foo": 1, "bar": 6}
, which seems like it could useful in some case but not very often. I don't remember ever using a counter like this, for example.
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'm still sorta unconvinced. I'll revert for now, and maybe open an issue to discuss in more depth later :)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
TwoThree changes:RemoveChange the bad overload which the comment states is bad, and use a# type: ignore
instead.Counter
arithmetic dunders.Counter
is halfway between aset
and adict
, and the arithmetic dunders come from theset
-like side of its personality. I think it makes sense to harmoniseCounter
's__sub__
,__and__
,__or__
and__add__
methods with the equivalent ones ontyping.AbstractSet
.Counter[Any]
instead ofCounter[object]
in variousCounter
dunders, due todict
invariance.