On Thu, Nov 18, 2021 at 7:44 PM Stephen J. Turnbull firstname.lastname@example.org wrote:
Steven D'Aprano writes:
On Wed, Nov 17, 2021 at 02:26:16PM -0000, email@example.com wrote:
@dataclass class A: """Docstring for class A.""" x: int """Docstring for x""" y: bool = True "Docstring for y"
However a real problem is that bare strings like that are already legal, although only as a no-op. People sometimes use them as multiline comments.
I agree it's a real problem. But I don't think it's a show-stopper. That is, the proposal as written also seems to be a near no-op from the point of view of backwards compatibility (I guess if you list the attributes of the class you would discover the new dunder).
So we would have no real way of distinguishing between these cases:
class A: """Docstring for class A.""" x: int """Docstring for x""" y: bool = True """Just a comment."""
True, but why would one want to, except to save the space? And for that, there's -OO.
Possible wrinkle: What if a string literal immediately follows something that isn't an attribute declaration?
class A: "Class docstring" x: int "x docstring" y: bool; True "y docstring?" print("Hello, world?") "print docstring??" def spam(self): ... "spam docstring???"
I presume it would have to be permitted and ignored, as per current behaviour (otherwise it'd create bizarre errors in currently-legal code). But that may make for some hard-to-track-down bugs when there's an error in a declaration, or when something technically breaks the declaration from the docstring (like the presumably-unintended semicolon in the y line).
I'm personally -0 on this - I don't declare class level type annotations enough to want to use this, but if it existed, I'd probably find a way to use it for a DSL or something. It's of minimal value, but it should also be of minimal disruption.