googleapis / python-aiplatform

A Python SDK for Vertex AI, a fully managed, end-to-end platform for data science and machine learning.
Apache License 2.0
645 stars 349 forks source link

Consider adding py.typed to repo to support type checking #4654

Open acciaioli opened 1 week ago

acciaioli commented 1 week ago

Is your feature request related to a problem? Please describe. Despite being typed, this package doesnt include a py.typed file. As a user, i am struggling to benefit from the defined types

Consider the code featured in the quickstart page

import vertexai
from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below line
PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")
reveal_type(model)
response = model.generate_content(
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
)
reveal_type(response)
print(response.text)
►mypy
scripts/gc_sdk.py:1: error: Skipping analyzing "vertexai": module is installed, but missing library stubs or py.typed marker  [import-untyped]
scripts/gc_sdk.py:2: error: Skipping analyzing "vertexai.generative_models": module is installed, but missing library stubs or py.typed marker  [import-untyped]
scripts/gc_sdk.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
scripts/gc_sdk.py:9: note: Revealed type is "Any"
scripts/gc_sdk.py:13: note: Revealed type is "Any"

And if I add the py.typed manually..

►mypy
scripts/gc_sdk.py:9: note: Revealed type is "vertexai.generative_models._generative_models.GenerativeModel"
scripts/gc_sdk.py:13: note: Revealed type is "vertexai.generative_models._generative_models.GenerationResponse"
Found 2 errors in 1 file (checked 48 source files)

This would help find issues using the lib, for example:

model = GenerativeModel("gemini-1.5-flash", version="002") 
scripts/gc_sdk.py:8: error: Unexpected keyword argument "version" for "GenerativeModel"  [call-arg]

Describe the solution you'd like I would like you to consider adding a empty file py.typed inside the vertexai directory

Describe alternatives you've considered I am considering creating the py.typed just before I ran mypy in my CI This way, I could use this lib with type safety

Additional context

vladimir-chernykh commented 4 days ago

Hey!

I've just faced the same issue and made a PR here: https://github.com/googleapis/python-aiplatform/pull/4700