Skip to content

Documentation from inspect.getdoc() for non-callable attributes math.e can be misleading. #135316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
astrolamb-gaming opened this issue Jun 9, 2025 · 6 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@astrolamb-gaming
Copy link

astrolamb-gaming commented Jun 9, 2025

Bug report

Bug description:

inspect module returns wrong documentation for math.e.

print(inspect.getdoc(math.e))
# Returns: 
# "Convert a string or number to a floating point number, if possible."

This should return something to the effect of:
"""
Returns the nearest representable value to the mathematical constant e, which has a value of 2.718281..." and is the base of natural logorithms.
"""

CPython versions tested on:

3.11

Operating systems tested on:

Windows

@astrolamb-gaming astrolamb-gaming added the type-bug An unexpected behavior, bug, or error label Jun 9, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jun 9, 2025
@zware
Copy link
Member

zware commented Jun 9, 2025

math.e is just a float instance; inspect.getdoc can only return data about the float type from it. I'm afraid there's nothing we can do here.

@zware zware added the pending The issue will be closed if no feedback is provided label Jun 9, 2025
@ZeroIntensity
Copy link
Member

In theory, we could create a separate float subclass for each constant (e, inf, etc.) that provides a specific docstring. But I don't think it's worth it, nor worth encouraging.

@ZeroIntensity ZeroIntensity closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2025
@ZeroIntensity ZeroIntensity removed the pending The issue will be closed if no feedback is provided label Jun 10, 2025
@terryjreedy terryjreedy changed the title documentation from inspect.getdoc() for math.e is incorrect Documentation from inspect.getdoc() for non-callable attributes math.e can be misleading. Jun 10, 2025
@terryjreedy
Copy link
Member

The issue with getdoc() is essentially the same as for help(). Only modules, classes, and functions have individual docstrings. dir(math.e) equals dir(float), with equal __doc__s, and the same is true for every float.

@astrolamb-gaming
Copy link
Author

Admittedly while I've been working on the project that triggered this, I keep forgetting that math.e isn't a function. The explanations given make sense in that context. I do wonder, though, if it would be worth using a function that returns e, or tau, or pi, or whatever other constants, instead of just having the constant. Would there be unnecessary performance impact? I don't know, but in my opinion, it would be helpful to have a useful docstring about these constants.

@ZeroIntensity
Copy link
Member

Yeah, it would be slower and more importantly, it would break a lot of code. I think a better approach would be to design a way to generally add docstrings to arbitrary objects (such as a global __docs__ dict). But there isn't any active proposal to do that as far as I'm aware.

@zware
Copy link
Member

zware commented Jun 10, 2025

Another possibility would be introducing module-level property, but that would be just as slow as a regular function call. On the plus (?) side, it could also prevent redefining fundamental constants :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants