推荐学习书目
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 · Aug 21, 2024 · 2622 views
    This topic created in 630 days ago, the information mentioned may be changed or developed.
    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 replies    2024-08-22 18:08:43 +08:00
    so1n
        1
    so1n  
       Aug 21, 2024
    好像只有 vscode 的才支持
    Maerd
        2
    Maerd  
       Aug 22, 2024
    这不是装饰器的问题,pycharm 对装饰器协议支持还是蛮好的,但是对__get__这类的描述器协议就无力支持了,这两年 pylance 要比 pycharm 的语言服务器显著好用
    yuban10703
        3
    yuban10703  
    OP
       Aug 22, 2024
    @Maerd pycharm 无解了么🤔
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5709 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 01:36 · PVG 09:36 · LAX 18:36 · JFK 21:36
    ♥ Do have faith in what you're doing.