False Positive: Pyright Reports Missing Argument for `Client.get_bucket` in `google-cloud-storage==2.18.2`
Issue Description
Pyright incorrectly reports a missing argument error when calling storage.Client().get_bucket("my-bucket") in google-cloud-storage==2.18.2. The function is expected to accept a bucket name as a string, but Pyright reports that an argument is missing.
Steps to Reproduce
- Install
google-cloud-storage==2.18.2:pip install google-cloud-storage==2.18.2 - Create a Python file (
repro.py) with the following code:from google.cloud import storage client = storage.Client() client.get_bucket("my-bucket") - Run Pyright on the file:
./node_modules/pyright/index.js repro.py
Expected Behavior
Pyright should not report an error, as get_bucket accepts a bucket name as a string according to the official google-cloud-storage documentation.
Observed Behavior
Pyright reports an incorrect error:
/home/ewianda/projects/bsmining/repro.py
/home/ewianda/projects/bsmining/repro.py:4:1 - error: Argument missing for parameter "bucket_or_name" (reportCallIssue)
1 error, 0 warnings, 0 informations
Environment Information
- Pyright version: 1.1.394
- google-cloud-storage version: 2.18.2
- Python version: 3.11
Pyright has no specialized knowledge of any third-party libraries. It simply enforces typing rules based on the type information provided by those libraries. If the library doesn't provide type information, it does its best to infer the type information.
In this case, it appears that the google-cloud-storage library has no type annotations. The get_bucket function is decorated with a call to @create_trace_span("..."). This decorator is also unannotated, and its inferred return type obscures the type of the function being decorated.
So pyright is working as intended here.
Your options include:
- Report the issue to the maintainers of this library and request that they add (even minimal) type annotations.
- See if you can find a stub library that provides more accurate types for this library.
- Tell pyright not to attempt to infer types for libraries that omit type annotations. To do this, set
useLibraryCodeForTypesto false in your pyright config. This will effectively disable all type checking (and related features like completion suggestions) for all untyped libraries.
Thanks @erictraut
@erictraut What exactly makes you believe that the issue is that google-cloud-storage is missing type annotations?
- A Google dev responded that they could not see anything obscuring type on the function when using the
@create_trace_span(). - I tried messing around a little with the
google-cloud-storagecode and adding type annotations and it did not help.
I would be extremely grateful if you could help me resolve this issue. The real issue seems to be with using a ContextManager as a decorator.
Here's a small reproduction of the issue:
from contextlib import contextmanager
@contextmanager
def ben_context():
with open("blah.txt", "w") as f:
yield f
class Ben:
@ben_context()
def blah(self, b: int) -> None:
pass
ben = Ben()
ben.blah(1)
I don't exactly know why the context manager being used as a decorator is broken, but it is.
AFAIK, using a context manager as a decorator is a problem with pyright, but it clearly is. This behavior is also part of Python since 3.2:
@benglewis, the problem you're seeing is not related to this issue. The OP's example involved a library that was unannotated. In your case, the symbol contextmanager is fully annotated via the typeshed stubs that are bundled with pyright.
Feel free to open a new discussion question in this forum or the Python typing forum if you need help with general Python typing concepts.
Sorry, @erictraut. I am trying to understand your comment that In your case, the symbol context manager is fully annotated via the type shed stubs that are bundled with copyright What @benglewis's example demonstrates is that this issue is not specific to an untyped library.
I have observed that using the contextmanager function directly as a decorator throws an error while using the functool.wrap preserves the function signature.
from contextlib import contextmanager
from functools import wraps
@contextmanager
def ben_context():
"""Context manager that opens 'blah.txt' for writing."""
with open("blah.txt", "w") as f:
yield f
def ben_context_decorator():
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
with ben_context():
return func(*args, **kwargs)
return wrapper
return decorator
class Ben:
@ben_context_decorator()
def blah(self, b: int) -> None:
"""Example method that uses ben_context."""
print(f"Processing {b}")
ben = Ben()
ben.blah(1)
N.B
- This happened when upgrading from 1.1.292 to the latest
- The error itself is saying missing argument vs type error, such that the error goes away with this change
ben = Ben()
-ben.blah(1)
+ben.blah(self=ben, b=1)
@erictraut Thank you. I am just going to mention that ticket #9196 should fix the issue faced in this ticket too as far as I can tell.
@benglewis @erictraut Hello, I'm a maintainer of the linked Google Cloud Storage library. We were a bit confused by this issue as our use of signature introspection seems to show that the decorators are correctly transmitting the underlying function signature and docstring. Although it looks like, from the most recent comment, that this issue may now be resolved in the latest issue of pyright, I'd like to understand the problem a bit better. What kind of introspection is pyright currently performing that runs afoul of our decorator strategy? This may impact our library in other ways so I'd like to learn more about it.
My analysis was incorrect. These issues are related.
The root cause turns out to be a recent regression in the typeshed contextlib.pyi stub file. It has since been fixed, and the next release of pyright will pick up the fix.
@andrewsg, thanks for maintaining the Google Cloud Storage library. As a library maintainer, you should consider adding type annotations to the library, at least for the public-facing interface. Relying on pyright's type inference behaviors may be convenient, but this solution won't work for other static analysis tools in the Python ecosystem like mypy. Even with pyright, type inference can be wrong, and it's computationally expensive, so it's best for library maintainers to include inlined type annotations that accurately describe the library's interface contract. For more details, refer to this section of the Python typing spec.
Thanks for letting us know. We're not currently a user of pyright, this bug report was not directly related to our operation. I would very much like to add type annotations; we're looking into prioritization and resourcing.