asgi-server-timing-middleware icon indicating copy to clipboard operation
asgi-server-timing-middleware copied to clipboard

Cython functions aren't supported because of the way `inspect.isfunction` works

Open wxd opened this issue 4 years ago • 4 comments

inspect.isfunction returns False for Cython functions (see this discussion).

As a result, tracking pydantic.fields.ModelField.validate for pydantic==1.7.3 is impossible.

wxd avatar Jan 20 '21 14:01 wxd

Is this exclusive to Cython functions or would any C function cause that problem? Your mailing list thread links to pep-0575, which in turn links to pep-0579, and both seem to recommend making changes to inspect.isfunction that don't seem to have gone through.

There's always the possibility of just going the duck-typing route and searching for a __code__ attribute on the functions, possibly alongside an extra check to ensure the functions being passed aren't bound methods (which I believe didn't work with yappi, but I would have to double-check), which seems to be what's recommended in the case of Cython, but some testing will be needed in that regard.

sm-Fifteen avatar Jan 20 '21 15:01 sm-Fifteen

I don't have any deep insight into this: I've found your repo while exploring different options for FastAPI profiling, tried to run the example in smoke_test.py, and hit that exception. So I only have an example of a Cython function.

I just tried replacing inspect.isfunction(profiled) with inspect.isroutine(profiled) (inspired by this), and it worked in the sense that Yappi didn't complain and returned YFuncStats containing Cython functions (and bound methods, too). The numbers look OK.

wxd avatar Jan 20 '21 15:01 wxd

callable(profiled) might be another option. I'll check and report tomorrow.

wxd avatar Jan 20 '21 15:01 wxd

FWIW - this stopped pydantic.fields.ModelField.validate (included in the readme) from working correctly because inspect.isfunction returns False. validate is a cython function.

Changing to callable(profiled) worked

traviscook21 avatar Sep 02 '21 21:09 traviscook21