Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

[BUG] Improve Type Hints on Python Model V2 #1161

Closed baluyotraf closed 1 year ago

baluyotraf commented 1 year ago

Hello! We are playing around with the Python Model V2. We think it looks great, however the decorators fail when we apply our type checker (pyright on strict settings). Looks like the culprit is the type hint definitions on the decorators. The Callable type hint are not defined completely. For example:

# https://github.com/Azure/azure-functions-python-library/blob/dev/azure/functions/decorators/function_app.py#L285

def function_name(self, name: str) -> Callable:
    ...

These can use the recommendation from pyright or using the newer ParamSpec for typing.

I can work on this if needed since it's pretty simple yet pretty impactful for us.

bhagyshricompany commented 1 year ago

Hi @baluyotraf thanks for update will update you soon

bhagyshricompany commented 1 year ago

It sounds like you're encountering an issue where your type checker (pyright) is failing when applied to the decorators in the Azure Functions Python Library (azure-functions-python-library). The problem seems to be with the type hint definitions on the decorators, specifically the use of the Callable type hint. The Callable type hint is not defined completely, which is causing issues with your type checker.

One solution to this issue would be to update the type hint definitions on the decorators to be more specific. For example, instead of using Callable, you could use a more specific type hint such as Callable[[Arg1Type, Arg2Type], ReturnType]. This will give the type checker more information about the types of arguments and return values that the decorated function should have. Another way could be using new typing module introduced in python 3.10 called ParamSpec which is more specific, allowing more detailed function type hints.

Another solution would be to configuring your type checker to be less strict. This may allow it to process the decorators as they are currently defined, but it may also make it less effective at catching type-related errors in your code.

baluyotraf commented 1 year ago

Yes. It looks like we're on the same page on the problem.

AFAIK there's no way to override the azure-functions-python-library type definitions without copying the entire stubs in our repository. Maintaining the entire stubs of typing for the decorators seems a bit too much.

To allow the type checking to pass through our pipelines, we just put # type: ignore on every Azure functions decorator that we use in Python. While not ideal, we would like to keep the strict settings on our typings.

I guess on our side, we would like to know if there's a nicer workaround on the issue, or if there are plans to update the typing in the python library. For now we are fine to just get by with the # type: ignore.

bhagyshricompany commented 1 year ago

@vrdmr please comment on this

YunchuWang commented 1 year ago

@baluyotraf Hi baluyotraf, i submitted an PR addressing this issue, can you please take a look and let me know if you have any questions

YunchuWang commented 1 year ago

pr merged, changes will go in next release in Feb

baluyotraf commented 1 year ago

Yup no complains here. Thanks!