python-valid8 icon indicating copy to clipboard operation
python-valid8 copied to clipboard

Use stub files `*.pyi` rather than `@with_signature` everywhere possible

Open smarie opened this issue 6 years ago • 7 comments

That will be more compliant with IDE autocomplete features.

smarie avatar Oct 07 '19 19:10 smarie

I'm writing a library fully typed with annotations and checked with mypy. I want to use python-valid8 to validate my functions but right now I can't without make all of them untyped (and it's the opposite of what I want). The main problem are the decorators, for other public functions I can make stub files. There are plans to made it compliant to mypy?

Thanks

galen1090 avatar Jan 02 '20 17:01 galen1090

Unfortunately I have never used mypy, so I do not have the experience for this. I would gladly accept any PR but please explain first what you intend to do (for example with a single mod) before going into too much development.

I would be curious to understand why mypy raises an issue here: after all it is scanning your code, not mine ? :)

smarie avatar Jan 16 '20 16:01 smarie

Here a simple example:

from mini_lambda import s, Len
from valid8 import validate_arg
from valid8.validation_lib import instance_of

@validate_arg('name', instance_of(str), Len(s) > 0, 
              help_msg='name should be a non-empty string')
def build_house(name: str) -> int:
    print(f'name is: {name}')
    return 10

def build_house2(name: str) -> int:
    print(f'name is: {name}')
    return 10

build_house("john")
build_house(5)

build_house2("john")
build_house2(5)

this is the output of mypy:

test.py:1: error: Cannot find implementation or library stub for module named 'mini_lambda'
test.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
test.py:2: error: Cannot find implementation or library stub for module named 'valid8'
test.py:3: error: Cannot find implementation or library stub for module named 'valid8.validation_lib'
test.py:5: error: Untyped decorator makes function "build_house" untyped
test.py:19: error: Argument 1 to "build_house2" has incompatible type "int"; expected "str"

As you can see, I pass a wrong type to either build_house and build_house2 function, but as mypy said, validate_arg untyped decorator makes function build_house untyped. So all function decorated with valid8 decorator become untyped and cannot be checked.

galen1090 avatar Jan 16 '20 16:01 galen1090

I see, thanks for the details! I pushed a PR for this: https://github.com/smarie/python-valid8/pull/54

The PR is only focused on the module you mention for now. Can you try and see if it solves your problem ? Thanks !

smarie avatar Jan 17 '20 10:01 smarie

Sorry for the delay. I have tried it and everything is like before.

galen1090 avatar Jan 21 '20 19:01 galen1090

Thanks for the feedback ! I managed to reproduce this and fix it. It was actually not related to the stub file presence but to the type hint of the decorator function, that was simply wrong. I opened a dedicated issue #56

smarie avatar Jan 22 '20 10:01 smarie

This is all in 5.1.0 that should ship in a few minutes when travis has done processing. Let me know how it goes ! Thanks again for the feedback

smarie avatar Jan 22 '20 10:01 smarie