It's current convention (and is used by typing module and static type checkers) that string annotations evaluate to valid Python types.
On 3/10/21 9:47 PM, Eric V. Smith wrote:
I'm not sure of the best way to achieve this. Using flags to field() doesn't sound awesome, but could be made to work. Or maybe special field names or types? I'm not crazy about that, but using special types would let you do something like:
@dataclasses.dataclass
class Point:
x: int = 0
_: dataclasses.KEYWORD_ONLY
y: int
z: int
t: int = 0
Maybe something like this?
class Hmm:
#
this: int
that: float
#
pos: '/'
#
these: str
those: str
#
key: '*'
#
some: list
>>> Hmm.__dict__['__annotations__']
{
'this': <class 'int'>,
'that': <class 'float'>,
'pos': '/',
'these': <class 'str'>,
'those': <class 'str'>,
'key': '*',
'some': <class 'list'>,
}
The name of 'pos' and 'key' can be convention, since the actual name is irrelevant. They do have to be unique, though. ;-)
--
~Ethan~
_______________________________________________