Skip to content

Make operations on set take AbstractSet[object] #1533

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

Merged
merged 1 commit into from
Aug 12, 2017

Conversation

ilinum
Copy link
Contributor

@ilinum ilinum commented Aug 8, 2017

Previously they were AbstractSet[Any]

Previously they were `AbstractSet[Any]`
# TODO more set operations

class frozenset(AbstractSet[_T], Generic[_T]):
def __init__(self, iterable: Iterable[_T] = ...) -> None: ...
def copy(self) -> FrozenSet[_T]: ...
def difference(self, *s: Iterable[Any]) -> FrozenSet[_T]: ...
def intersection(self, *s: Iterable[Any]) -> FrozenSet[_T]: ...
def difference(self, *s: Iterable[object]) -> FrozenSet[_T]: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, __sub__ below (which is implemented the same way in C) takes AbstractSet[_T]; I'm not sure why we can't specify _T here.

Copy link
Contributor Author

@ilinum ilinum Aug 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good catch both of these should be the same.

However, I think we should change the __sub__ function, in order to allow the following code:

from typing import Set, Union, FrozenSet

s1 = frozenset([1, 2])  # type: FrozenSet[int]
s2 = frozenset([1, '2'])  # type: FrozenSet[Union[str, int]]

# currently, mypy  you an error on the following line but I don't think it should.
s3 = s1 - s2  # type: FrozenSet[int]

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense from the perspective of minimizing false positives. It seems a shame that mypy won't tell you if you're mixing sets of different types in your operations (which is surely often a bug), but we should generally err on the side of reducing false positives.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should first decide how to do that for ==; then maybe we'll be able to generalize for other operations on other objects. I think for == there's probably already an open issue.

@JelleZijlstra
Copy link
Member

Generally looks good; I wonder if we should use _T instead of object for at least some of these.

@JelleZijlstra JelleZijlstra merged commit 4491e41 into python:master Aug 12, 2017
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.

3 participants