[Python-Dev] Re: Let's Fix Class Annotations -- And Maybe Annotations Generally

2021年4月19日 22:06:36 -0700

As long as I'm gravedigging old conversations...! Remember this one, also from January of this year? Here's a link to the thread in the c.l.p-d Mailman archive. The first message in the thread is a good overview of the problem:
 
https://mail.python.org/archives/list/[email protected]/thread/AWKVI3NRCHKPIDPCJYGVLW4HBYTEOQYL/
Here's kind of where we left it:
On 1/12/21 7:48 PM, Guido van Rossum wrote:
On Tue, Jan 12, 2021 at 6:35 PM Larry Hastings <[email protected] <mailto:[email protected]>> wrote:
 On 1/12/21 5:28 PM, Brett Cannon wrote:
 The other thing to keep in mind is we are talking about every
 module, class, and function getting 64 bytes ... which I bet
 isn't that much.
 Actually it's only every module and class. Functions don't have
 this problem because they've always stored __annotations__
 internally--meaning, peeking in their __dict__ doesn't work, and
 they don't support inheritance anyway. So the number is even
 smaller than that.
 If we can just make __annotations__ default to an empty dict on
 classes and modules, and not worry about the memory consumption,
 that goes a long way to cleaning up the semantics.
I would like that very much. And the exception for functions is especially helpful.
First of all, I've proposed a function that should also help a lot:
 https://bugs.python.org/issue43817
The function will be called inspect.get_annotations(o). It's like typing.get_type_hints(o) except less opinionated. This function would become the best practice for everybody who wants annotations**, like so:
 import inspect
 if hasattr(inspect, "get_annotations"):
   how_i_get_annotations = inspect.get_annotations
 else:
   # do whatever it was I did in Python 3.9 and before...
** Everybody who specifically wants /type hints/ should instead call typing.get_type_hints(), and good news!, /that/ function has existed for several versions now. So they probably already /do/ call it. I'd still like to add a default empty __annotations__ dict to all classes and modules for Python 3.10, for everybody who doesn't switch to using this as-yet-unwritten inspect.get_annotations() function. The other changes I propose in that thread (e.g. deleting __annotations__ always throws TypeError) would be nice, but honestly they aren't high priority. They can wait until after Python 3.10. Just these these two things (inspect.get_annotations() and always populating __annotations__ for classes and modules) would go a long way to cleaning up how people examine annotations. Long-term, hopefully we can fold the desirable behaviors of inspect.get_annotations() into the language itself, at which point we could probably deprecate the function. That wouldn't be until a long time from now of course. Does this need a lot of discussion, or can I just go ahead with the bpo and PR and such? I mean, I'd JFDI, as Barry always encourages, but given how much debate we've had over annotations in the last two weeks, I figured I should first bring it up here.
Happy two-weeks'-notice,
//arry/
p.s. I completely forgot about this until just now--sorry. At least I remembered before Python 3.10b1!
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/J4LZEIZTYZQWGIM5VZGNMQPWB5ZWVEXP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to