[Python-Dev] Second post: PEP 557, Data Classes (original) (raw)

Eric V. Smith eric at trueblade.com
Mon Nov 27 08:02:11 EST 2017


On 11/27/2017 7:26 AM, Sebastian Rittau wrote:

On 27.11.2017 12:01, Sebastian Rittau wrote:

The major changes from the previous version are:

- Add InitVar to specify initialize-only fields. This is the only feature that does not sit right with me. It looks very obscure and "hacky". From what I understand, we are supposed to use the field syntax to define constructor arguments. I'd argue that the name "initialize-only fields" is a misnomer, which only hides the fact that this has nothing to do with fields at all. Couldn't dataclassses just pass *args and **kwargs to postinit()? Type checkers need to be special-cases for InitVar anyway, couldn't they instead be special cased to look at postinit argument types? I am sorry for the double post, but I thought a bit more about why this does not right with me: * As written above, InitVars look like fields, but aren't.

Same as with ClassVars, which is where the inspiration came from.

* InitVar goes against the established way to pass through arguments, *args and **kwargs. While type checking those is an unsolved problem, from what I understand, I don't think we should introduce a second way just for dataclasses. * InitVars look like a way to satisfy the type checker without providing any benefit to the programmer. Even when I'm not interested in type checking, I have to declare init vars.

Same as with ClassVars, if you're using them. And that's not just a dataclasses thing, although dataclasses is the first place I know of where it would change the code semantics.

* InitVars force me to repeat myself. I have the InitVar declaration and then I have the repeat myself in the signature of postinit(). This has all the usual problems of repeated code.

There was some discussion about this starting at https://github.com/ericvsmith/dataclasses/issues/17#issuecomment-345529717, in particular a few messages down where we discussed what would be repeated, and what mypy would be able to deduce. You won't need to repeat the type declaration.

I hope I did not misunderstood the purpose of InitVar.

I think you understand it perfectly well, especially with the "context" discussion. Thanks for bringing it up.

Eric.



More information about the Python-Dev mailing list