How to annotate default values of dataclass (or attrs) fields

When I define a class as

@dataclass
class Klass:
    a: str
    b: int = 0
    c: list = field(default_factory=[2])

the hover-over descriptor yields

class Klass(
    a: str,
    b: int = ...,
    c: list = ...
)

Is there anything I could do to get the same hints I get with a normal function with default values, ie the hover-over text (on VS Code) would read:

class Klass(
    a: str,
    b: int = 0,
    c: list = [2]
)

?

The information is there; it’s up to whatever tool you are using to make use of it.

(By the way, [2] is not a valid default factory; it needs to be lambda: [2], as the factory needs to be called. I would not expect any tool to report the return value of a default factory as the default value of c.)

1 Like

Do you have any advice how I can discover why my particular IDE is not making use of the information, or what information it is making use of (so that I can put the information in the correct place for the IDE)?
And: how can I confirm that the information is there, where it is supposed to be?

(Yes, I made a mistake whilst reducing my code to a minimal example. I do use a lambda in the version that I’m actually using. Oops.)

... appers to simply mean “a default exists”. Determining what that default is requires some effort, and any effort may be more than what the IDE wants to exert.

0 is pretty simple. 1 + 3 is not quite as simple, but not too bad. pow(3, 2) is getting a little more complicated. field(...) is a conceptual leap: the result of field is not the default, but some value (if any!) produced by the field, which isn’t obvious as all. And that’s just for a function call that you could hard-code logic for, because it’s in the standard library. What about something similar provided by an arbitrary 3rd-party package?

I think it’s reasonable for an editor to not make any assumptions, other than “a default exists” and represent any and all syntactic defaults with a single ....

2 Likes

mmm.

That sound like a different answer from what you gave before?

I would be willing to spend a bit of code adding to the metadata, if that meant that I got good type hints for the init. If there was a way to add to the metadata so that both the init and the attribute got the correct type hints, that would be even better.

For example if there is a attribute with the field = field(default_factory=list) I could imagine adding to that

__init_repr__ = `list | None = []`
__atrr_repr__ = `list`

and I’d be quite happy to spend the ‘effort’ typing that if it helped me every time I (or someone else) used the class.

The IDE is not part of the language. Type hints and default values exist for reasons other than for the IDE to provide pop-up hints about how to write code. The effort I am talking about is not programmer effort, but IDE effort to derive static information from what might otherwis be dynamically evaluated code.

An IDE could specify that it would use “extra” code for its own purposes, but I, as someone who does not use that IDE, would not want to work with code cluttered with hints I have no use for.