Skip to content

builtin_function_or_method is also either a function or a method #91370

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
ravi140222 mannequin opened this issue Apr 4, 2022 · 6 comments
Closed

builtin_function_or_method is also either a function or a method #91370

ravi140222 mannequin opened this issue Apr 4, 2022 · 6 comments
Labels
3.11 only security fixes build The build process and cross-build

Comments

Image for: Comments
Copy link
Mannequin

ravi140222 mannequin commented Apr 4, 2022

BPO 47214
Nosy @stevendaprano, @sweeneyde, @ravi140222

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-04-04.11:51:09.624>
labels = ['build', '3.11']
title = 'builtin_function_or_method is also either a function or a method'
updated_at = <Date 2022-04-04.17:51:35.304>
user = 'https://github.com/ravi140222'

bugs.python.org fields:

activity = <Date 2022-04-04.17:51:35.304>
actor = 'apostofes'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Build']
creation = <Date 2022-04-04.11:51:09.624>
creator = 'apostofes'
dependencies = []
files = []
hgrepos = []
issue_num = 47214
keywords = []
message_count = 5.0
messages = ['416664', '416673', '416680', '416682', '416686']
nosy_count = 3.0
nosy_names = ['steven.daprano', 'Dennis Sweeney', 'apostofes']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue47214'
versions = ['Python 3.11']

Copy link
Mannequin Author

ravi140222 mannequin commented Apr 4, 2022

the.

import inspect
inspect.isfunction

does not consider a

builtin_function_or_method

as a function.
for example,

inspect.isfunction(abs)

gives,

False

in the background even the builtin abs is a function, so shouldn't it return True?

the way this check is implemented is by matching the type of a builtin_function_or_method with,

type(len)

and since, type(len) is builtin_function_or_method, so, the inspect.isfunction check gives False.
But in my opinion, it should return True, even for builtin functions or methods.

ravi140222 mannequin added 3.11 only security fixes build The build process and cross-build labels Apr 4, 2022
Copy link
Member

https://docs.python.org/3/library/inspect.html#inspect.isfunction says this:

"""
inspect.isfunction(object)
Return True if the object is a Python function, which includes functions created by a lambda expression.
"""

Emphasis on the "Python function", as in, something written in Python using a def statement or a lambda expression. If isfunction returns True, you can presumably access function-specific implementation details like the functions's f.code attribute. If you need to check for "anything that works as a function", you can use callable():

>>> callable(lambda: 2)
True
>>> callable(abs)
True
>>> def f(x): return x
>>> callable(f)
True

I'm not an expert on the inspect module, but I'm guessing it's not worth breaking backwards-compatibility to change this behavior.

Would extra emphasis in the documentation have been helpful for you, or were you mostly attempting to rely on the function's name?

Copy link
Mannequin Author

ravi140222 mannequin commented Apr 4, 2022

but callable returns True for classes with __call__ also, it does not check whether the argument passed to it is a function or not.

I want some way to return True for both builtin functions and Python functions, but not for classes.

And similarly, some way to return True for both builtin methods and Python methods.

Should the documentation for inspect.isfunction include, this does not include builtin functions., and similarly for inspect.ismethod?

Should I create a PR to add these two sentences to the documentation?

Copy link
Member

Perhaps what you want is inspect.isroutine ?

https://docs.python.org/3/library/inspect.html#inspect.isroutine

I agree with Dennis that the isfunction test is for **Python** (def or lambda) functions, not builtins. The docstring for the inspect.is* methods make promises about what attributes an object will have:

def isbuiltin(object):
    """Return true if the object is a built-in function or method.
    Built-in functions and methods provide these attributes:
        __doc__         documentation string
        __name__        original name of this function or method
        __self__        instance to which a method is bound, or None"""


def isfunction(object):
    """Return true if the object is a user-defined function.
    Function objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this function was defined
        __code__        code object containing compiled function bytecode
        __defaults__    tuple of any default values for arguments
        __globals__     global namespace in which this function was defined
        __annotations__ dict of parameter annotations
        __kwdefaults__  dict of keyword only parameters with defaults"""

def (and lambda) functions have a different API from builtin_function_or_method objects. They should be kept separate.

Copy link
Mannequin Author

ravi140222 mannequin commented Apr 4, 2022

yes, I think inspect.isroutine does the required functionality.
sorry, I did not know about it, and could not think of the word routine when checking for functions.

ezio-melotti transferred this issue from another repository Apr 10, 2022
Copy link
Contributor

I believe this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes build The build process and cross-build
4 participants