It's current convention (and is used by typing module and static type checkers) that string annotations evaluate to valid Python types.

On Thu, 2021-03-11 at 10:45 -0800, Ethan Furman wrote:
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:

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.  ;-)

Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct: