Do you think we’d have one annotations extension to add docstrings to class variables, and an entirely separate system for what I proposed?
I think they are too deeply connected for that to happen. Before python had type hints, people used to put type hints in the doc string of functions, like so:
def function_with_types_in_docstring(param1, param2):
"""Example function with types documented in the docstring.
:type param1: int
:type param2: str
:rtype: bool
"""
You would see ‘variable docstrings’ being used the same way if that ends up being the only way to properly type hint dataclass/attrs properties.
So then, if the ‘docstrings for variables’ is implemented in a manner that is not compatible with adding proper type hints for dataclass, we might never get proper type hints for dataclass. And that would really bother me.
Adding to this, if I have proper type hints I often don’t need a docstring. (Partially because you can make custom types.) But if I have a docstring I still need proper type hints.
Adding to my original post, I forgot you’d also need to be able to annotate it explicitly with the default value, and the get_type should probably be possible to do with a normal type hint, so it could also be reasonable to have
@attrs.define
class MyDataClass():
a: str = attrs.field(converter=int, default='33')
|| set_type = str|float|int
|| default = '33'
|| "A variable that is correctly type hinted in the class constructor"
If these 2 things were implemented separately, what would we end up with?
@attrs.define
class MyDataClass():
a: str = attrs.field(converter=int, default='33')
|| "A variable that is correctly type hinted in the class constructor"
## set_type = str|float|int
## default = '33'
??
I really don’t see that happening.
And again, that would be bloody annoying, if we can’t have good type hints for classes because we half-arsed the docstrings.
So no, I think it is worth making this change well, if and when it is made.