classification
Title: Add generic versions of weakref types to typing module
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Nils Kattenbeck, gvanrossum, levkivskyi
Priority: normal Keywords:

Created on 2019-11-09 17:03 by Nils Kattenbeck, last changed 2019-11-10 23:29 by gvanrossum. This issue is now closed.

Messages (10)
msg356304 - (view) Author: Nils Kattenbeck (Nils Kattenbeck) Date: 2019-11-09 17:03
I would like to request the addition of generic variants of some of the types in weakref to the typing module.
This would for example include weakref.WeakKeyDictionary among others.

Doing so would allow one to add type annotations to code using such data structures.
msg356311 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-11-09 22:55
OK, can you give some example use cases? Can you provide a list of types that you want to see added?
msg356313 - (view) Author: Nils Kattenbeck (Nils Kattenbeck) Date: 2019-11-10 00:20
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...
msg356318 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-11-10 05:36
Well, in typeshed, weakref.WeakSet and the others are generic, so you should be able to write

  from __future__ import annotations

  from weakref import WeakSet

  users: WeakSet[User]

Or if you need to support Python versions < 3.7, you could write

  users: 'WeakSet[User]'

Does that not solve your problem? It would be easier than adding yet another random class (or classes) to typing, which won't work for Python < 3.9 because typing.py is in the stdlib (and PEP 484 is no longer provisional).
msg356324 - (view) Author: Nils Kattenbeck (Nils Kattenbeck) Date: 2019-11-10 10:58
Yes thank you, using 'from __future__ import annotations' works fantastic.

I did not knew about this functionality of the annotations future import statement, I thought it was only for postponed evaluation but I probably missed something in the PEP...
msg356325 - (view) Author: Nils Kattenbeck (Nils Kattenbeck) Date: 2019-11-10 11:30
Okay nevermind, after looking in the PEP again and inspecting the __annotations__ property I learned that annotations are never evaluated and just saved as a string when using said future statement.

However this may still be relevant as typing.get_type_hints will still fail. For my use case however this is sufficient.
msg356331 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-11-10 16:33
@ilevkivskyi Is this important enough to change get_type_hints()?

Otherwise let’s close this issue.
msg356333 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-11-10 18:26
We already have https://github.com/python/typing/issues/508 for "smart" `get_type_hints()` that would use LBYL. Also we had a similar discussion about PathLike, and ultimately decided not to make `typing` a place for generic versions of everything.

Finally, in view of PEP 585 some of these things may actually become subscriptable at runtime. So I propose to just close this issue.
msg356335 - (view) Author: Nils Kattenbeck (Nils Kattenbeck) Date: 2019-11-10 21:04
Okay, if I want to start a discussion about adding weakref types to PEP 585 where should do it? The mailing list or as an issue in the PEP repo?
msg356342 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-11-10 23:29
PEP 585 says

Discussions-To: Typing-Sig <typing-sig@python.org>

So I'd start there.

(But honestly I think what Ivan meant is that it would automatically work. Read the PEP carefully before posting if you disagree.)
History
Date User Action Args
2019-11-10 23:29:02gvanrossumsetmessages: + msg356342
2019-11-10 21:04:48Nils Kattenbecksetmessages: + msg356335
2019-11-10 18:26:16levkivskyisetstatus: open -> closed
resolution: wont fix
messages: + msg356333

stage: resolved
2019-11-10 16:33:42gvanrossumsetmessages: + msg356331
2019-11-10 11:30:45Nils Kattenbecksetmessages: + msg356325
2019-11-10 10:58:25Nils Kattenbecksetmessages: + msg356324
2019-11-10 05:36:00gvanrossumsetmessages: + msg356318
2019-11-10 00:20:51Nils Kattenbecksetmessages: + msg356313
2019-11-09 22:55:28gvanrossumsetmessages: + msg356311
2019-11-09 17:12:47xtreaksetnosy: + gvanrossum, levkivskyi
2019-11-09 17:03:42Nils Kattenbeckcreate