Skip to content
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

set[str].discard(str | None) #7121

Open
Akuli opened this issue Feb 3, 2022 · 3 comments · May be fixed by #11403
Open

set[str].discard(str | None) #7121

Akuli opened this issue Feb 3, 2022 · 3 comments · May be fixed by #11403

Comments

@Akuli
Copy link
Collaborator

Akuli commented Feb 3, 2022

Consider the following:

names: set[str] = {"a", "b"}
name: str | None = None
names.discard(name)

It would be nice if this wasn't a type checker error. Unlike .remove(), the .discard() method does nothing if the item is not in the set, so I don't see a good reason to ban None values.

@Akuli
Copy link
Collaborator Author

Akuli commented Feb 3, 2022

Related: #7015, #6597

@AlexWaygood
Copy link
Member

I agree, though we can't use object as the annotation -- it would have to be Any:

>>> s = {1, 2}
>>> s.discard([])
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    s.discard([])
TypeError: unhashable type: 'list'

@kkirsche
Copy link
Contributor

kkirsche commented Aug 18, 2022

Would the Hashable type be able to be used here, as None is able to be hashed:

In [1]: hash(None)
Out[1]: 271809023

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 a pull request may close this issue.

3 participants