V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yuban10703
V2EX  ›  Python

pycharm 被装饰过的函数参数提示问题

  •  1
     
  •   yuban10703 · 26 天前 · 969 次点击
    B = TypeVar("B", bound="Bot")
    R = TypeVar("R")
    P = ParamSpec("P")
    
    class API(Generic[B, P, R]):
        def __init__(self, func: Callable[Concatenate[B, P], Awaitable[R]]) -> None:
            self.func = func
    
        def __set_name__(self, owner: Type[B], name: str) -> None:
            self.name = name
    
        @overload
        def __get__(self, obj: None, objtype: Type[B]) -> "API[B, P, R]": ...
    
        @overload
        def __get__(
            self, obj: B, objtype: Optional[Type[B]]
        ) -> Callable[P, Awaitable[R]]: ...
    
        def __get__(
            self, obj: Optional[B], objtype: Optional[Type[B]] = None
        ) -> "API[B, P, R] | Callable[P, Awaitable[R]]":
            if obj is None:
                return self
    
            return partial(obj.call_api, self.name)  # type: ignore
    
        async def __call__(self, inst: B, *args: P.args, **kwds: P.kwargs) -> R:
            return await self.func(inst, *args, **kwds)
        
    

    可以参考这个库的代码 https://github.com/nonebot/adapter-dodo/blob/83d55b09a9787c55d06fb01a5bb9c1f2d6a61fde/nonebot/adapters/dodo/utils.py#L39

    像下面这个函数 https://github.com/nonebot/adapter-dodo/blob/83d55b09a9787c55d06fb01a5bb9c1f2d6a61fde/nonebot/adapters/dodo/bot.py#L298

    被上面这个装饰器装饰过后就没有参数提示了... 在不换 IDE 的情况下该怎么做😭😭😭

    3 条回复    2024-08-22 18:08:43 +08:00
    so1n
        1
    so1n  
       26 天前
    好像只有 vscode 的才支持
    Maerd
        2
    Maerd  
       25 天前
    这不是装饰器的问题,pycharm 对装饰器协议支持还是蛮好的,但是对__get__这类的描述器协议就无力支持了,这两年 pylance 要比 pycharm 的语言服务器显著好用
    yuban10703
        3
    yuban10703  
    OP
       25 天前
    @Maerd pycharm 无解了么🤔
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   950 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.