First note: Python itself does not enforce that annotations be type objects. So for your system, you are free to use (a,b) if you like. (As long as a and b are defined. Of course, then your code wouldn’t work with static type checkers. But the trick is that annotations are Python, and (a,b) is an instance of tuple, not the tuple type. When should a type checker use the type of the annotation rather than the annotation itself? So this would be introducing a typing language that’s a bit different that Python — see a recent thread on this list about that. Another note: making it really easy to type things as concrete types like list and tuple would encourage overly specific typing:-( Of course, if this was actually done, then [] could mean MutableSequence, rather than list, which I think would be pretty cool :-) -CHB On Tue, Jan 18, 2022 at 11:53 PM Vincent Risi <vincent.risi@gmail.com> wrote:
I have written an xsd parser to generate python classes. I also have written a utility to read in an xml file to populate the python classes. As well as a utility to write xml from the python classes thus generated. For example the code below that I include here shows a small sample of the generated code. The __annotations__ for these are very easy to use by the utility code for doing their job.
Using the typing list[x] and tuple[a,b] do not work as well as - [x] instead of list[x] - (a, b) instead of tuple(a, b)
I seem to feel list[x] and tuple[a,b] are far more non Python, requiring having to parse the string they return in a complicated way.
Looping through the annotations to get the field types it's easy to test for list or tuple and then get the field type for the list or tuple and the usage for the tuple.
I have been using Python from version 1.6 and have been involved in writing and generating tons of lines of Python.
ATTRIB, PSEUDO, ASIS = range(3)
class RecordType: isNew: str column: (str,ATTRIB) whizz: (str,PSEUDO) def __init__(self): self.isNew = '' self.whizz = '' self.column = ''
class MsgTableType: record: [RecordType] def __init__(self): self.record = []
rt = RecordType() for annote in rt.__annotations__: print (annote, type(rt.__annotations__[annote]))
mtt = MsgTableType() for annote in mtt.__annotations__: print (annote, type(mtt.__annotations__[annote])) _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ANSJXQTU... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython