0

I have a problem in my code that I can't seem to find a way to fix. I have a custom help command which works fine but when I define the docstring as a fstring it gives None as an output

code from my help command:

if cmd.help:
  emd.add_field(name=f'{self.client.command_prefix}{cmd}', value=f'{aliases}\n{cmd.help}\u200b', inline=False)
              found_cmd = True
else:
  print(cmd.help)
  emd.add_field(name=f'{self.client.command_prefix}{cmd}', value=f'{aliases}\n - \u200b', inline=False)

The roll command I tried the test command:

@commands.command()
  async def roll(self, ctx, sides:int = 6, num_of_times_to_roll:int=1):
    f'''syntax:`{self.client.command_prefix}roll [number of sides] [number of times to roll]` \nThis command rolls a die or dice with any number of sides and any number of times \n\n`{self.client.command_prefix}roll_side` \nThis command only takes the number of sides as an input'''

I think that python does not read fstrings as docstrings because when I try the help command with normal docstrings it works fine. If anyone knows how to fix this problem please tell me. Thanks in advance.

Broken DEV
  • 39
  • 7

2 Answers2

2

It's because the python's f-string is a runtime facility that inflates the string at runtime. On the other hand, docstrings are compile-time facilities that are parsed even prior to the code being interpreted.
In simple words, you can't benefit from runtime facilities on compile-time, since nothing actually is running!
For more info this thread may be helpful.

Emran
  • 544
  • 7
  • 26
1

Formatted string literals cannot be used as docstrings, even if they do not include expressions.

example:

   >>> def foo():
   ...     f"Not a docstring"
   ...
   >>> foo.__doc__ is None
   True

There is a possible solution for you in this answer

Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20