GeezerVison is showing github.com — Set difference unexpectedly permissive · Issue #1840 · python/typeshed
Set difference unexpectedly permissive #1840DescriptionMypy 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? Activitygvanrossum commented on Jan 24, 2018 But if we did that, wouldn't mypy just infer T as object and still allow it? chadaustin commented on Jan 24, 2018 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 commented on Jan 24, 2018 Yeah, but the point is that the set operation itself wouldn't get the error
-- and in some cases the error would come much later and surprise people. chadaustin commented on Jan 24, 2018 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 commented on 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. added 2 commits that reference this issue on Mar 28, 2020This issue will close once commit 2f03dc7 is merged into the 'master' branch.2f03dc7 This issue will close once commit 22ffd19 is merged into the 'master' branch.22ffd19 added a commit that references this issue on Sep 17, 2020This issue will close once commit b186563 is merged into the 'main' branch.b186563
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|