Skip to content
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

Pydoc: fall back to __text_signature__ if inspect.signature() fails #107782

Closed
serhiy-storchaka opened this issue Aug 8, 2023 · 0 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 8, 2023

Pydoc uses inspect.signature() to get a function signature. inspect.signature() supports Python functions, and also extension functions defined with Argument Clinic (by parsing the __text_signature__ attribute). Unfortunately Argument Clinic is used with functions whose signature cannot be expressed in Python, e.g. getattr() or dict.pop(). It produces a human readable __text_signature__, but it can not be represented as a Signature object, so inspect.signature() fails. Pydoc display generic (...) for such functions.

Since pydoc only needs a string representation of signature, not a Signature object, I propose to use __text_signature__ if inspect.signature() fails. It needs only some trivial processing.

Before:

getattr(...)
    Get a named attribute from an object.

    getattr(x, 'y') is equivalent to x.y
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.

After:

getattr(object, name, default=<unrepresentable>, /)
    Get a named attribute from an object.

    getattr(x, 'y') is equivalent to x.y
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.

Linked PRs

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Aug 8, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Aug 8, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 8, 2023
…gnature() fails

It allows to show signatures which are not representable in Python,
e.g. for getattr and dict.pop.
serhiy-storchaka added a commit that referenced this issue Aug 11, 2023
…e() fails (GH-107786)

It allows to show signatures which are not representable in Python,
e.g. for getattr and dict.pop.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 12, 2023
Builtin functions and methods that have non-representable signatures today
will have representable signatures yesterday, and they will become unusable
for testing this feature.

So we need to add special functions and methods to the _testcapi module
that always have non-representable signatures.
serhiy-storchaka added a commit that referenced this issue Sep 14, 2023
)

Builtin functions and methods that have non-representable signatures today
will have representable signatures yesterday, and they will become unusable
for testing this feature.

So we need to add special functions and methods to the _testcapi module
that always have non-representable signatures.
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-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants