检查模块是否包含funcscorosmethods。

问题描述 投票:0回答:1

我有一个目录,像这样。

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── my_test_file.py
|
├── file1.py
├── file2.py
└── file3.txt

现在我想检查每个python文件中的每个函数和方法, 如果预期的返回值是Coroutine类型.

问题:有什么方法可以检查这些文件(通过绝对路径)?├── sub_dir

├── bar.py my_test_file.py └── file4.txt inspect.getmembers() ├── config.py

我知道我可以通过获取函数方法来检查返回值的类型。__annotations__ 喜欢。

async def bar(j):
    return None

def foo(i:int,j:int)-> Coroutine:
    res = i+j
    return bar

foo.__annotations__["return"]

会回来的 typing.Coroutine 我可以用它来比较。

python python-3.x inspect
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.