Larry Hastings <la...@hastings.org> added the comment:

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!

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43901>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to