This works, but it feels fairly low-level and inconsistent.
Modern Python tooling increasingly depends on callable metadata:
FastAPI-like frameworks
CLI generators
RPC systems
dependency injection
schema generation
IDE tooling
LLM/agent frameworks
Most of these projects seem to end up building their own normalization layer over existing APIs.
I’m wondering whether Python could benefit from a more structured high-level callable introspection API, perhaps something conceptually like:
r = inspect.reflect(func)
r.signature
r.parameters
r.return_type
r.capabilities.async_callable
r.doc
r.source
instead of directly interacting with many separate APIs and implementation details.
Importantly, I’m not suggesting runtime mutation/reflection features or AST rewriting. This would mainly be a structured introspection layer over existing metadata.
Has something like this already been explored before?
Would this make sense as an enhancement to inspect, or is the current API surface considered sufficient?
Speaking about standardization and missing features - in my older post (Unified way of introspecting callables) I wrote about the inspect module is not treating all callables uniformly and I proposed that a minimal common base should be defined. There should be a clearly defined set of special attributes like __name__, __qualname__, __docs__, __module__, __annotations__ that can be safely accessed no matter what subtype of a callable you have.
That would break code expecting to e.g. filter out builtin objects by not being able to access certain attributes. If that issue can be prevented though, this would be a quite good solution imo.
It’s not so much that the information “lives” in inspect. The information is in __code__ as the co_argcount attribute and friends, and inspect contains logic that makes it much more intuitively accessible.
“Descriptor” is unfortunately ambiguous. It refers to things with __get__, or (less commonly) with __set_name__. A better name would possibly be FunctionInfo, which has precedents in inspect (the ArgInfo named tuple and FrameInfo returned by getargvalues and get*frame* functions respectively).
but did the inspect mod fixed/not fixed this problem?
in fact … i mean that this extension should work exactly like the builtin func but just make it more organized.
ok i mean a dataclass or something alike.
just like the reflection objects in java/kotlin or something, it returns a organizaed information set, instead of making you access many unrelated objects just for a func’s metadata.
nobody want to access 10 different objects in 10 modules just for a func.name
I’m planning to put together a more complete proposal. Would it be appropriate to move this to a PEP once I have a reference implementation and a draft spec?
PS: I’m 13 and I wrote this in my spare time after school. If this is dumb, tell me why. If it’s not, tell me how to make it better.
I want to work this forward.
This looks quite nice as a structure. The one question I’d have is how often do you need all of this information at once? The advantage of having individual functions is that you can just get precisely what you want, and not pay the cost for getting things you don’t care about.
Optimising too early is often a bad idea, but in this case you’re competing with an existing API that’s faster.
Maybe you could make the individual attributes into properties, and calculate them on demand?