Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

VSCode incorrect decorated func hint #119

Closed iperov closed 1 year ago

iperov commented 1 year ago

unable to determine func signature in hint

Code_2023-01-01_15-48-32


with usual decorator all fine

Code_2023-01-01_15-52-14

Stewori commented 1 year ago

Note that I am not working on pytypes at the moment. That said, this does look more like as VSCode issue; there is a helper pytypes.util._actualfunc that retrieves the backing function if they fail to retrieve it their way. Since it is impossible to tell what VSCode is actually doing, that cannot be fixed in pytypes. For pytypes issues, please provide the stacktrace (place pytypes.clean_traceback = False at top to reveal pytypes-internal part of the stack trace) and a reproducible example. Also, you can avoid decorator issues by using profiler mode.

iperov commented 1 year ago

look more like as VSCode issue

i don't think so.

this solution works

from typing import TypeVar

import pytypes

T = TypeVar('T')

def typechecked(func : T) -> T:
    return pytypes.typechecked(func)
iperov commented 1 year ago

also, can you recommend a similar library that works with Generic?

Stewori commented 1 year ago

this solution works

For some reason VSCode tries to determine the arg types of a function. That solution likely won't help it access the original types. Anyway, if it silences the error, your adjustment is a simple enough fix. Changing the typechecked signature in typechecker.py should do the same trick. If it is as simple as that I can make that change. Can you confirm that making the change in typechecker.py fixes it for you? (changing line 1007 from def typechecked(memb): to def typechecked(memb: T)->T:)

I am not aware of any other lib that works with Generic, but I might not have an up to date picture. Maintenance of such a feature is immensely painful because of rapid changes in the typing module. pytypes only works for Pythons up to 3.8 as of this writing.

iperov commented 1 year ago

the bug reproduced in python 3.8.10

def typechecked(memb: T)->T:)

unfortunately does not work. May be the bug in vscode.

immensely painful because of rapid changes in the typing module

I understand, that's sad. It seems only your library support Generic type checking.

iperov commented 1 year ago

by the way, decorator should return func wrapper

def typechecked(memb : T) -> T:
    if not pytypes.checking_enabled:
        return memb

I think such code confuses hint evaluator

Stewori commented 1 year ago

(deleted spam)