IMO I think using normal python functions to do these kinds of type transformations should be reconsidered as I think that’s definitely the best option. Yes, it is quite complex to implement, although I am unsure why it would be significantly more complex than the current proposal. And it would, with a bit of care, make the code readable in a way that the current proposal never will be.
This is e.g. how the select example could look.
def compute_select_return_type(BaseModel, UsedMembers: typing.BaseTypedDict):
result = typing.NewProtocol()
for key in typing.Attrs(UsedMembers):
member_type = typing.GetMemberType(BaseModel, key.name)
field_type = typing.ConvertField(member_type)
result.add_member(key.name, field_type)
return result
def select[ModelT, K: typing.BaseTypedDict](
typ: type[ModelT],
/,
**kwargs: Unpack[K],
) -> list[compute_select_return_type(ModelT, K)];
This could look even better & pythonic if some of the special forms/functions were renamed and if typing. would be dropped, but I think it’s already easier to understand what is going on.
My opinion on this is that the mini language currently in place is well suited for declarative operations - but “if/for” as used here is definitely imperative, and I think this difference is enough to consider moving out of the mini language.
IMO:
- Complexity for type checking implementers is less important than
- Complexity for people writing type expression is less important than
- Complexity for people using a typed library.
I agree that this proposal successfully reduces complexity for the last group, but I believe that we can reduce the complexity for the second group significantly by moving some of it into the first group.
Python gets lots of praise for readability. A core part of this is the indentation-based syntax, forcing separation of ideas onto separate lines. I think it would be a good idea to add some of this readability to typing instead of adding more and more special cases into the inline syntax. At some point it’s going to break. I don’t think this PEP is quite it, but I think this is a good place to jump off and do something better long term.