python / cpython

The Python programming language
https://www.python.org
Other
63.83k stars 30.56k forks source link

ENH: Improve help() to suggest related commands when invalid input is given #127284

Open bushraqurban opened 6 days ago

bushraqurban commented 6 days ago

Feature or enhancement

Proposal:

Problem

Currently, when a user enters an invalid input into help() (e.g., a misspelled function name or a non-existent function), the response is simply:

No Python documentation found for 'invalid_input'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

This doesn't help users who may have made a typo or are unsure of the correct function name. It would be more user-friendly if help() could suggest similar, valid commands from the available documentation in both the built-in Python modules and any currently imported modules.

Proposed Feature

When a user enters an invalid function or method name, help() should return a suggestion of closely related commands from the built-in and imported modules. For example, if the user types a misspelled method name, help() should suggest the correct method(s) from the current environment.

Example Usage

Input

help('pandas.duplicated()')

Output Did you mean: help('pandas.DataFrame.duplicated')

Input

help('math.sqrted')

Output Did you mean: help('math.sqrt')?

Benefits

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

skirpichev commented 6 days ago

the response is simply

BTW, you might query for docs differently:

>>> help(abt)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    help(abt)
         ^^^
NameError: name 'abt' is not defined. Did you mean: 'abs'?
>>> help(abs)
Help on built-in function abs in module builtins:

abs(x, /)
    Return the absolute value of the argument.

This is a minor feature, which does not need previous discussion elsewhere

I doubt it's a minor feature. You examples demonstrate, that a "helper" for the help() will be assumed to do very non-trivial things, for external projects like Pandas - too. It's not clear (1) how exactly, (2) at which cost (there possible security, performance issues, etc).

IMO, this kind of stuff should be first discussed on the https://discuss.python.org/c/ideas/6. Preferably, with much more detailed proposal to show how this may work algorithmically.

bushraqurban commented 6 days ago

Thanks for your feedback! I really appreciate your input. I see your point about the complexity of implementing this feature, especially for third-party packages like Pandas. I’ll take some time to consider these issues more carefully, particularly how we can limit the scope of the suggestions to only trusted modules and imported libraries. I’m planning to explore solutions that balance usability and performance.

I’ll follow up with a more detailed proposal once I’ve thought through the details. Thanks again for your insights!