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:

@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~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/BIAVX4O6JRPQY7S3XG2IX6BSBZLAR2NS/
Code of Conduct: http://python.org/psf/codeofconduct/