Issue 36009: weakref.ReferenceType is not a valid typing type (original) (raw)

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 "", line 1, in 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:11 admin set github: 80190
2019-02-17 06:23:31 thehesiod set messages: +
2019-02-16 12:11:58 levkivskyi set status: open -> closedresolution: wont fixmessages: + stage: resolved
2019-02-16 08:30:42 thehesiod set messages: +
2019-02-16 08:09:38 xtreak set nosy: + gvanrossum, levkivskyi
2019-02-16 08:04:16 thehesiod create