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.

Author larry
Recipients Guido.van.Rossum, eric.smith, gvanrossum, larry
Date 2021-04-24.22:36:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1619303793.53.0.554862763808.issue43901@roundup.psfhosted.org>
In-reply-to
Content
Aaaaand I just had a realization.  Lazy creation of an empty annotations dict, for both classes and modules, will work fine.

As stated in my previous comment in this issue, my goal here is to improve best practices in 3.10+, while preserving the unfortunate best practices of 3.9 and before.  I don't know why I couldn't see it before, but: adding a getset to modules and user classes, and lazily creating the empty annotations dict if not set, ought to work fine.  The getset code will have to store the annotations dict in the class / module dict but that's not a big deal.

If you're in 3.10+, and your code looks in the class dict for annotations, and we lazy-create it with a getset and store it in the class dict, then you still see what you expected to see.

* If the class has annotations, they'll be in the class dict.
* If the class has no annotations, but someone accessed
  cls.__annotations__, the empty dict will be lazily created
  and you'll see it.
* If the class doesn't have annotations, then the class dict
  won't have an '__annotations__' key, which is what you were expecting.

In any scenario the old best practices code works fine.

If you're in 3.10+, and your code uses cls.__annotations__ or getattr() to get the class dict, and we lazy-create it with a getset and store it in the class dict, then you also get what you expected.  If the class or module has annotations, you get them; if it doesn't, it lazy creates an empty dict and stores and returns that.  Your code works fine too.

Sorry I've been slow on figuring this out--but at least I got there in the end, before beta.  And it's good news!
History
Date User Action Args
2021-04-24 22:36:33larrysetrecipients: + larry, gvanrossum, eric.smith, Guido.van.Rossum
2021-04-24 22:36:33larrysetmessageid: <1619303793.53.0.554862763808.issue43901@roundup.psfhosted.org>
2021-04-24 22:36:33larrylinkissue43901 messages
2021-04-24 22:36:32larrycreate