I personally think F-strings should not be usable as docstrings. If you want a dynamically calculated docstring you should assign it dynamically, not smuggle it in using a string-like expression. We don't allow "blah {x} blah".format(x=1) as a docstring either, not "foo %s bar" % x.
On Tue, Jan 11, 2022 at 10:29 AM Guido van Rossum <gu...@python.org> wrote:I personally think F-strings should not be usable as docstrings. If you want a dynamically calculated docstring you should assign it dynamically, not smuggle it in using a string-like expression. We don't allow "blah {x} blah".format(x=1) as a docstring either, not "foo %s bar" % x.Agreed. If we wanted to remove the wart of constant f-strings happening to work as an implementation detail in this context, that could be made into a warning. But that kind of check may be best left to a linter for all of these dynamic situations that don't wind up populating __doc__.
On Tue, Jan 11, 2022 at 10:40 AM Gregory P. Smith <gr...@krypto.org> wrote:
On Tue, Jan 11, 2022 at 10:29 AM Guido van Rossum <gu...@python.org> wrote:
I personally think F-strings should not be usable as docstrings. If you want a dynamically calculated docstring you should assign it dynamically, not smuggle it in using a string-like expression. We don't allow "blah {x} blah".format(x=1) as a docstring either, not "foo %s bar" % x.
Agreed. If we wanted to remove the wart of constant f-strings happening to work as an implementation detail in this context, that could be made into a warning. But that kind of check may be best left to a linter for all of these dynamic situations that don't wind up populating __doc__.
Agreed on not supporting it and linters catching such a mistake.
Just to be clear, we don't support this.
>>> class C: 'foo'
...
>>> C.__doc__ == 'foo'
True
>>> class C: f'foo'
...
>>> C.__doc__ == 'foo'
False
>>> C.__doc__ is None
True
And there's a test to make sure constant f-strings are not doc
strings:
https://github.com/python/cpython/blob/dce642f24418c58e67fa31a686575c980c31dd37/Lib/test/test_fstring.py#L406
Eric
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/J5EMBDNY52ZHZPPUZSRUOCUYF4RCSRZH/
Having something that looks like it sets the docstring, but silently
doesn't is very surprising, though. Linters can warn about this, but
linters are not a universal fix, and this is something that
superficially looks entirely reasonable.
I personally think F-strings should not be usable as docstrings. If you want a dynamically calculated docstring you should assign it dynamically, not smuggle it in using a string-like expression. We don't allow "blah {x} blah".format(x=1) as a docstring either, not "foo %s bar" % x.