Eric V. Smith <e...@trueblade.com> added the comment:

The more I think about this, the more I think Łukasz is correct that just 
checking for strings starting with "ClassVar", "typing.ClassVar", or 
"t.ClassVar" is the correct thing to do. This is the change he made in 
https://github.com/python-attrs/attrs/pull/367.

In some private email I had discussed extracting the module name, if any, from 
the string annotation, and looking it up in sys.modules. This doesn't buy you 
much because you have to know how the caller imported typing. If they used 
"import typing as t", then you can't look up "t" in sys.modules. You could do 
some horrible frame trick to find out what the caller knew as "t", but that 
still won't work in plenty of cases. I don't want to use a complex solution 
that still doesn't work in all cases, and would indeed work in fewer places 
than just examining the string. The only name we could reliably detect is 
"typing.ClassVar", all others wouldn't be in sys.modules correctly.

So, that leaves us with just looking at the string and guessing what the caller 
meant. I think the three strings that Łukasz suggested are good enough.

This will need to be well documented. The documentation should note that things 
like this will break:

from __future__ import annotations
from typing import ClassVar as CV
@dataclass
class C:
    x: CV

x will not be a class variable here, since @dataclass will see the annotation 
as "CV" and not know that it means typing.ClassVar.

InitVar has a similar problem. What strings to use there? "InitVar" and 
"dataclasses.InitVar", of course. But maybe "dc.InitVar"? It's hard to say with 
no in-the-field usage examples to search for. I'll start with just the first 
two strings.

I really don't want to use regexes here. But a refinement would be to not just 
use .startswith, and instead use a word boundary. After all, "ClassVarOfMine" 
should probably not be considered a ClassVar. I'll leave that issue for another 
day, though.

I'll have a patch ready before the sprints are over.

----------
assignee:  -> eric.smith

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

Reply via email to