Skip to content

Set difference unexpectedly permissive #1840

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

Closed
chadaustin opened this issue Jan 22, 2018 · 5 comments · Fixed by #3886
Closed

Set difference unexpectedly permissive #1840

chadaustin opened this issue Jan 22, 2018 · 5 comments · Fixed by #3886
Labels
stubs: false negative Type checkers do not report an error, but should

Comments

@chadaustin
Copy link

Mypy passes accepts the following snippet, even though the set subtraction is guaranteed to be a no-op.

    a: Set[str] = set(["hello"])
    b: Set[bytes] = set([b"world"])
    b -= a

Would it make sense to require Set[T] for both arguments to a difference operation?

@gvanrossum
Copy link
Member

But if we did that, wouldn't mypy just infer T as object and still allow it?

@chadaustin
Copy link
Author

In context, I had the function's return type as List[bytes], and the return statement return sorted(b - a), so even if sorted(b - a) returned List[object], I'd still see a type error when it checks against List[bytes], right?

@gvanrossum
Copy link
Member

gvanrossum commented Jan 24, 2018 via email

@chadaustin
Copy link
Author

Better a surprising type error (easily diagnosed with reveal_type) than shipping a no-op to production, as I did before submitting this issue. :)

@srittau srittau added stubs: false positive Type checkers report false errors size-small labels Oct 28, 2018
@srittau
Copy link
Collaborator

srittau commented Oct 28, 2018

Changing object to _T in this line:

def __isub__(self, s: AbstractSet[object]) -> Set[_T]: ...

makes mypy complain about the last line in the example above. This seems reasonable to me. (Similar to the other set operations.) This is also in line with the other container types. For example, we only ordering comparisons between lists of the same type, but for sets we allow any set. This is inconsistent.

@srittau srittau added stubs: false negative Type checkers do not report an error, but should and removed stubs: false positive Type checkers report false errors labels Oct 31, 2018
proost added a commit to proost/typeshed that referenced this issue Mar 28, 2020
proost added a commit to proost/typeshed that referenced this issue Mar 29, 2020
srittau pushed a commit that referenced this issue Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false negative Type checkers do not report an error, but should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants