-
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
Closed
Closed
fix: fix mypy errors #15482
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,17 @@ | |
|
|
||
| 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 | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _PANDAS_REQUIRED = "pandas is required to verify type DataFrame." | ||
|
|
@@ -59,7 +63,7 @@ def __init__(self, bucket_name=None, client=None, credentials=None, project=None | |
| the client will attempt to ascertain the credentials from the | ||
| environment. | ||
| """ | ||
| if storage is None: | ||
| if not _STORAGE_INSTALLED: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| raise ImportError(_STORAGE_REQUIRED) | ||
|
|
||
| if client is not None: | ||
|
|
@@ -119,7 +123,7 @@ def upload_pandas_dataframe(self, dataframe, uploaded_csv_name=None): | |
| Returns: | ||
| A string representing the GCS URI of the uploaded CSV. | ||
| """ | ||
| if pandas is None: | ||
| if not _PANDAS_INSTALLED: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| raise ImportError(_PANDAS_REQUIRED) | ||
|
|
||
| if not isinstance(dataframe, pandas.DataFrame): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
NameErrorfor static type checkers likemypy. If an import fails, the module name is not defined, which will cause amypyerror on later usage (e.g.,pandas.DataFrame).The original pattern of setting the module to
NoneonImportErroris more robust. Themypyerrors can be fixed by other means. Forstorage, thetype: ignoreyou've added is correct. Forpandas,mypyshould correctly handle theif pandas is None: raisetype guard, especially withpandas-stubsinstalled.I suggest reverting this block to the conventional pattern, which will also require reverting the checks below.