This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: weakref.ReferenceType is not a valid typing type
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, levkivskyi, thehesiod
Priority: normal Keywords:

Created on 2019-02-16 08:04 by thehesiod, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg335674 - (view) Author: Alexander Mohr (thehesiod) * Date: 2019-02-16 08:04
For valid types which encapsulate other types, like typing.List or typing.Tuple, you can declare what's contained in them like so:

foo: typing.List[int] = []

Unfortunately weakref.ReferenceType does not allow you to do this.  You get the error:
>>> foo: weakref.ReferenceType[typing.Any] = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

so either ReferenceType should be fixed or a new type made available.
msg335675 - (view) Author: Alexander Mohr (thehesiod) * Date: 2019-02-16 08:30
I'm not a typing expert, but from what I understand you could do:

  class Bar: pass

  foo: Callable[[], Bar] = weakref.ref(Bar())

but you lose the typing weakref.ref.

OTOH if you simply do:

  foo: weakref.ref = weakref.ref(Bar())

you lose the info of Bar ;)
msg335681 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-02-16 12:11
This question appeared several times before, and the conclusion is that we are not going to do this.

There are many cases in standard library that are generic in stubs (and by nature) but are not declared as such at runtime, so we simply can't provide a typing equivalent for all of them.

In Python 3.7 you can just use from __future__ import annotations for those, and/or use the recipes described here https://mypy.readthedocs.io/en/latest/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime

Maybe at some point we will start using `Generic` in standard library, but for now I am closing this as wontfix.
msg335755 - (view) Author: Alexander Mohr (thehesiod) * Date: 2019-02-17 06:23
doing the __future__ doesn't help anything as it is an invalid type
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80190
2019-02-17 06:23:31thehesiodsetmessages: + msg335755
2019-02-16 12:11:58levkivskyisetstatus: open -> closed
resolution: wont fix
messages: + msg335681

stage: resolved
2019-02-16 08:30:42thehesiodsetmessages: + msg335675
2019-02-16 08:09:38xtreaksetnosy: + gvanrossum, levkivskyi
2019-02-16 08:04:16thehesiodcreate