henribru / google-api-python-client-stubs

Type stubs for google-api-python-client
https://pypi.org/project/google-api-python-client-stubs/
Apache License 2.0
51 stars 7 forks source link

Fix discovery.build requestBuilder param type #40

Closed mgraczyk closed 11 months ago

mgraczyk commented 1 year ago

requestBuilder can take any function that returns an HttpRequest

mgraczyk commented 1 year ago

Sorry the param type is not quite right. I will fix it.

mgraczyk commented 1 year ago

Ok I think this works:

class _HttpRequestBuilder(Protocol, HttpRequest): ...

Not 100% sure that this is right though, open to suggestions. I'm trying to express "this is a function that takes the same arguments as the HttpRequest init method"

henribru commented 1 year ago

Hi, thanks for contributing!

Unfortunately you can't do class _HttpRequestBuilder(Protocol, HttpRequest): ... because protocols are forbidden from inheriting from normal classes: https://peps.python.org/pep-0544/#protocols-subclassing-normal-classes

I think the best solution might be to define a callable protocol with a signature that matches the HttpRequest initializer, i.e. something like:

class _HttpRequestBuilder(Protocol):
    def __call__(self, ...) -> HttpRequest: ...

where you replace the first ... with the parameter list of HttpRequest.__init__. Unfortunately I haven't written the actual types for that one: https://github.com/henribru/google-api-python-client-stubs/blob/master/googleapiclient-stubs/http.pyi#L93-L103 So if you want an accurate signature for the request builder you'll have to figure out those types first and then copy that signature