El mar, 18 ene 2022 a las 11:42, Erik De Bonte via Typing-sig (<typing-sig@python.org>) escribió:

Thanks to everyone that has provided feedback on the dataclass_transform PEP so far!

 

There’s one open issue right now, and I wondered if anyone had feedback specifically on it:

 

converter field descriptor parameter

The attrs library supports a converter field descriptor parameter, which is passed a callable that is called by the generated __init__ method to convert the supplied value to some other desired value. This is tricky to support since the parameter type in the synthesized __init__ method needs to accept uncovered values, but the resulting field is typed according to the output of the converter.

 

There may be no good way to support this because there's not enough information to derive the type of the input parameter. We currently have two ideas:

 

  1. Add support for a converter field descriptor parameter but then use the Any type for the corresponding parameter in the __init__ method.

I'm not sure why we'd need Any. If I understand correctly, a converter is basically a callable (T1) -> T2, that makes it so that for a field of type T2, the constructor accepts arguments of type T1. So if we see something like `attr: int = field(converter=len)`, the type checker has enough information to create an `__init__` with type `attr: Sized`.

That said, I don't have a strong opinion on whether this should be included in the proposal. I can see how this feature can be useful, but it does increase complexity.
 
  1. Say that converters are unsupported and recommend that attrs users avoid them.

 

Some aspects of this issue are discussed on the pyright GitHub site. [https://github.com/microsoft/pyright/discussions/1782?sort=old#discussioncomment-653909]

 

Thanks,

Erik

_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: jelle.zijlstra@gmail.com