Does converter also support default argument in field specifiers of dataclass_transform?
#2189
Replies: 2 comments 1 reply
-
|
According to the Python typing specification and statements from the dataclass_transform proposal authors, field specifiers like As mentioned in the Pyright issue, this is by design. Type checkers should not expect or support default arguments for |
Beta Was this translation helpful? Give feedback.
-
|
Just to echo: it would be much appreciated if the spec could be updated to be clear on that default arguments on (custom) field descriptors are supported. Not just in practice for some of the arguments, but for all. In particular, for our use case, we need it for Tagging @superlopuh as he encountered this issue before as well. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the typing spec (https://typing.python.org/en/latest/spec/dataclasses.html#field-specifier-parameters), when describing the field specifiers that can appear in
dataclass_transform, the spec provides the following code example:In this example, parameters like
initcan have default arguments, so users don't need to explicitly provide these arguments when calling. pyright/ty respects these default arguments, but mypy does not.However, for
converter, the situation is different. Pyright does not respect the default argument forconverter. Other type checkers seem to have not yet implemented support forconverter.I opened an issue in Pyright (microsoft/pyright#11291), but it was closed by @erictraut , who claimed
Since he is one of the authors of the
dataclass_transformproposal, and the spec is unclear on this matter, I’m also left confused.The below is my context around this question.
My current requirement is: for certain specific kinds of fields, the parameters in
__init__must be of a different type. For example:In this case, for fields of type
Result, the user must provide a specific converter (e.g.,def convert(result: Type) -> Result), while for fields of other types the user must not be allowed to provide one.If there were a type-dependent way to specify a field specifier, that would be ideal. We could automatically derive what specifier a field needs from its field type. But that clearly requires a new proposal and might not be accepted. So the alternative would be to provide a special field specifier and give it a default converter, but based on pyright maintainer's explanation that also isn’t allowed.
Beta Was this translation helpful? Give feedback.
All reactions