[issue38756] Add generic versions of weakref types to typing module

Nils Kattenbeck report at bugs.python.org
Sat Nov 9 19:20:51 EST 2019


Nils Kattenbeck <nilskemail at gmail.com> added the comment:

A possible use case would be having multiple users and some groups said users can belong to. When using a WeakSet a user won't be part of a groups after he deleted his account without having to iterate over all groups.

class User: pass

class Group:
  users: WeakSet[User]

  def __init__(self, users: Iterable[User]):
    self.users = WeakSet(users)

# somewhere else a collection of all active users


Types I would like to see added as a generic would be primarily:
- WeakKeyDictionary
- WeakValueDictionary
- WeakSet

Additionally it would be nice to have generic versions of:
- ref (would probably return Optional[T] on call?)
- WeakMethod
- ProxyType
- CallableProxyType

Although the last two could probably be just annotated using T because they mostly should behave the same...

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38756>
_______________________________________


More information about the Python-bugs-list mailing list