-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: fix mypy errors #15482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: fix mypy errors #15482
Conversation
Summary of ChangesHello @parthea, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on improving the type checking robustness of the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to fix mypy errors, and the changes in noxfile.py to add pandas-stubs are correct. However, the changes in google/cloud/automl_v1beta1/services/tables/gcs_client.py to handle optional dependencies using boolean flags introduce a new problem for static analysis: mypy will report a NameError because the module names (pandas, storage) are not defined if their respective imports fail.
I've provided suggestions to revert to the more standard pattern of setting the module variable to None on ImportError. This resolves the NameError for mypy. The type: ignore for the storage import is preserved as it seems necessary to resolve a separate mypy issue with that library. With these changes, the mypy errors should be correctly resolved.
| try: | ||
| import pandas | ||
| _PANDAS_INSTALLED = True | ||
| except ImportError: # pragma: NO COVER | ||
| pandas = None | ||
| _PANDAS_INSTALLED = False | ||
|
|
||
| try: | ||
| from google.cloud import storage | ||
| # TODO(https://github.com/googleapis/python-storage/issues/318): | ||
| # Remove `type: ignore` once this bug is fixed | ||
| from google.cloud import storage # type: ignore | ||
| _STORAGE_INSTALLED = True | ||
| except ImportError: # pragma: NO COVER | ||
| storage = None | ||
| _STORAGE_INSTALLED = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach of using boolean flags to track optional dependencies can introduce a NameError for static type checkers like mypy. If an import fails, the module name is not defined, which will cause a mypy error on later usage (e.g., pandas.DataFrame).
The original pattern of setting the module to None on ImportError is more robust. The mypy errors can be fixed by other means. For storage, the type: ignore you've added is correct. For pandas, mypy should correctly handle the if pandas is None: raise type guard, especially with pandas-stubs installed.
I suggest reverting this block to the conventional pattern, which will also require reverting the checks below.
| try: | |
| import pandas | |
| _PANDAS_INSTALLED = True | |
| except ImportError: # pragma: NO COVER | |
| pandas = None | |
| _PANDAS_INSTALLED = False | |
| try: | |
| from google.cloud import storage | |
| # TODO(https://github.com/googleapis/python-storage/issues/318): | |
| # Remove `type: ignore` once this bug is fixed | |
| from google.cloud import storage # type: ignore | |
| _STORAGE_INSTALLED = True | |
| except ImportError: # pragma: NO COVER | |
| storage = None | |
| _STORAGE_INSTALLED = False | |
| try: | |
| import pandas | |
| except ImportError: # pragma: NO COVER | |
| pandas = None | |
| try: | |
| # TODO(https://github.com/googleapis/python-storage/issues/318): | |
| # Remove `type: ignore` once this bug is fixed | |
| from google.cloud import storage # type: ignore | |
| except ImportError: # pragma: NO COVER | |
| storage = None |
| environment. | ||
| """ | ||
| if storage is None: | ||
| if not _STORAGE_INSTALLED: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| A string representing the GCS URI of the uploaded CSV. | ||
| """ | ||
| if pandas is None: | ||
| if not _PANDAS_INSTALLED: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP