-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Description
Bug report
Bug description:
If you call help on a dataclass in Python 3.14 or later it shows the internal type names used for the exec call in the signature.
Docstring included to hide the dataclasses generated __doc__ which would just confuse things.
from dataclasses import dataclass
@dataclass
class Example:
"docstring"
x: int
help(Example)Before, on 3.13
Help on class Example in module __main__:
class Example(builtins.object)
| Example(x: int) -> None
|
| docstring
|
| Methods defined here:
|
| __eq__(self, other)
| Return self==value.
|
| __init__(self, x: int) -> None
| Initialize self. See help(type(self)) for accurate signature.
...
Now, on 3.14 or later
Help on class Example in module __main__:
class Example(builtins.object)
| Example(x: __dataclass_type_x__) -> __dataclass___init___return_type__
|
| docstring
|
| Methods defined here:
|
| __eq__(self, other)
| Return self==value.
|
| __init__(self, x: __dataclass_type_x__) -> __dataclass___init___return_type__
| Initialize self. See help(type(self)) for accurate signature.
...
This looks a bit messy and reveals inner details that users of help probably don't need to see.
It looks like this comes from inspect.signature(cls, annotation_format=Format.STRING) being used in pydoc, but I think it's a dataclasses issue.
attrs doesn't suffer the same fate as it attaches annotations as __annotations__ instead of defining them in the source code string to be evaluated.
Inspired by that, for my own dataclass-like implementation for 3.14+ I'm collecting the annotations and generating an __annotate__ function which appears to solve this issue: see here
dataclasses could potentially do something similar.
CPython versions tested on:
3.14, CPython main branch
Operating systems tested on:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status