GeezerVison is showing github.com — fix: make `pyarrow` an optional dependency post-3.20.0 yanked release… · kiraksi/python-bigquery@21714e1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_pandas_helpers.py +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | import pandas # type: ignore | 33 | 33 |
| 34 | 34 | pandas_import_exception = None | 35 | | -except ImportError as exc: # pragma: NO COVER | | 35 | +except ImportError as exc: | 36 | 36 | pandas = None | 37 | 37 | pandas_import_exception = exc | 38 | 38 | else: | | 44 | 44 | date_dtype_name = db_dtypes.DateDtype.name | 45 | 45 | time_dtype_name = db_dtypes.TimeDtype.name | 46 | 46 | db_dtypes_import_exception = None | 47 | | -except ImportError as exc: # pragma: NO COVER | | 47 | +except ImportError as exc: | 48 | 48 | db_dtypes = None | 49 | 49 | db_dtypes_import_exception = exc | 50 | 50 | date_dtype_name = time_dtype_name = "" # Use '' rather than None because pytype | 51 | 51 |
| 52 | | -pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import(raise_if_error=True) | 53 | | -from pyarrow import ArrowTypeError # type: ignore # noqa: E402 | 54 | | - | 55 | | -_BIGNUMERIC_SUPPORT = False | 56 | | -if pyarrow is not None: # pragma: NO COVER | 57 | | - _BIGNUMERIC_SUPPORT = True | | 52 | +pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import() | 58 | 53 |
| 59 | 54 | try: | 60 | 55 | # _BaseGeometry is used to detect shapely objevys in `bq_to_arrow_array` | 61 | 56 | from shapely.geometry.base import BaseGeometry as _BaseGeometry # type: ignore | 62 | | -except ImportError: # pragma: NO COVER | | 57 | +except ImportError: | 63 | 58 | # No shapely, use NoneType for _BaseGeometry as a placeholder. | 64 | 59 | _BaseGeometry = type(None) | 65 | 60 | else: | | 61 | + # We don't have any unit test sessions that install shapely but not pandas. | 66 | 62 | if pandas is not None: # pragma: NO COVER | 67 | 63 |
| 68 | 64 | def _to_wkb(): | @@ -309,10 +305,10 @@ def bq_to_arrow_array(series, bq_field): | 309 | 305 | if field_type_upper in schema._STRUCT_TYPES: | 310 | 306 | return pyarrow.StructArray.from_pandas(series, type=arrow_type) | 311 | 307 | return pyarrow.Array.from_pandas(series, type=arrow_type) | 312 | | - except ArrowTypeError: # pragma: NO COVER | | 308 | + except pyarrow.ArrowTypeError: | 313 | 309 | msg = f"""Error converting Pandas column with name: "{series.name}" and datatype: "{series.dtype}" to an appropriate pyarrow datatype: Array, ListArray, or StructArray""" | 314 | 310 | _LOGGER.error(msg) | 315 | | - raise ArrowTypeError(msg) | | 311 | + raise pyarrow.ArrowTypeError(msg) | 316 | 312 |
| 317 | 313 |
| 318 | 314 | def get_column_or_index(dataframe, name): | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_pyarrow_helpers.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 |
| 21 | 21 | try: | 22 | 22 | import pyarrow # type: ignore | 23 | | -except ImportError: # pragma: NO COVER | | 23 | +except ImportError: | 24 | 24 | pyarrow = None | 25 | 25 |
| 26 | 26 |
| @@ -49,7 +49,7 @@ def pyarrow_timestamp(): | 49 | 49 | _BQ_TO_ARROW_SCALARS = {} | 50 | 50 | _ARROW_SCALAR_IDS_TO_BQ = {} | 51 | 51 |
| 52 | | -if pyarrow: # pragma: NO COVER | | 52 | +if pyarrow: | 53 | 53 | # This dictionary is duplicated in bigquery_storage/test/unite/test_reader.py | 54 | 54 | # When modifying it be sure to update it there as well. | 55 | 55 | # Note(todo!!): type "BIGNUMERIC"'s matching pyarrow type is added in _pandas_helpers.py | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_tqdm_helpers.py +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 |
| 24 | 24 | try: | 25 | 25 | import tqdm # type: ignore | 26 | | - import tqdm.notebook as notebook # type: ignore | 27 | | - | 28 | | -except ImportError: # pragma: NO COVER | | 26 | +except ImportError: | 29 | 27 | tqdm = None | 30 | 28 |
| | 29 | +try: | | 30 | + import tqdm.notebook as tqdm_notebook # type: ignore | | 31 | +except ImportError: | | 32 | + tqdm_notebook = None | | 33 | + | 31 | 34 | if typing.TYPE_CHECKING: # pragma: NO COVER | 32 | 35 | from google.cloud.bigquery import QueryJob | 33 | 36 | from google.cloud.bigquery.table import RowIterator | | 42 | 45 |
| 43 | 46 | def get_progress_bar(progress_bar_type, description, total, unit): | 44 | 47 | """Construct a tqdm progress bar object, if tqdm is installed.""" | 45 | | - if tqdm is None: | | 48 | + if tqdm is None or tqdm_notebook is None and progress_bar_type == "tqdm_notebook": | 46 | 49 | if progress_bar_type is not None: | 47 | 50 | warnings.warn(_NO_TQDM_ERROR, UserWarning, stacklevel=3) | 48 | 51 | return None | @@ -58,7 +61,7 @@ def get_progress_bar(progress_bar_type, description, total, unit): | 58 | 61 | unit=unit, | 59 | 62 | ) | 60 | 63 | elif progress_bar_type == "tqdm_notebook": | 61 | | - return notebook.tqdm( | | 64 | + return tqdm_notebook.tqdm( | 62 | 65 | bar_format="{l_bar}{bar}|", | 63 | 66 | desc=description, | 64 | 67 | file=sys.stdout, | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_versions_helpers.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,7 @@ def try_import(self, raise_if_error: bool = False) -> Any: | 73 | 73 | """ | 74 | 74 | try: | 75 | 75 | import pyarrow | 76 | | - except ImportError as exc: # pragma: NO COVER | | 76 | + except ImportError as exc: | 77 | 77 | if raise_if_error: | 78 | 78 | raise exceptions.LegacyPyarrowError( | 79 | 79 | "pyarrow package not found. Install pyarrow version >=" | @@ -212,7 +212,7 @@ def try_import(self, raise_if_error: bool = False) -> Any: | 212 | 212 | """ | 213 | 213 | try: | 214 | 214 | import pandas | 215 | | - except ImportError as exc: # pragma: NO COVER | | 215 | + except ImportError as exc: | 216 | 216 | if raise_if_error: | 217 | 217 | raise exceptions.LegacyPandasError( | 218 | 218 | "pandas package not found. Install pandas version >=" | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/job/query.py +1-6Lines changed: 1 addition & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 56 | 56 |
| 57 | 57 | try: | 58 | 58 | import pandas # type: ignore | 59 | | -except ImportError: # pragma: NO COVER | | 59 | +except ImportError: | 60 | 60 | pandas = None | 61 | 61 |
| 62 | | -try: | 63 | | - import db_dtypes # type: ignore | 64 | | -except ImportError: # pragma: NO COVER | 65 | | - db_dtypes = None | 66 | | - | 67 | 62 | if typing.TYPE_CHECKING: # pragma: NO COVER | 68 | 63 | # Assumption: type checks are only used by library developers and CI environments | 69 | 64 | # that have all optional dependencies installed, thus no conditional imports. | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/magics/magics.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 95 | 95 | import IPython # type: ignore | 96 | 96 | from IPython import display # type: ignore | 97 | 97 | from IPython.core import magic_arguments # type: ignore | 98 | | -except ImportError: # pragma: NO COVER | | 98 | +except ImportError: | 99 | 99 | raise ImportError("This module can only be loaded in IPython.") | 100 | 100 |
| 101 | 101 | from google.api_core import client_info | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/table.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 |
| 27 | 27 | try: | 28 | 28 | import pandas # type: ignore | 29 | | -except ImportError: # pragma: NO COVER | | 29 | +except ImportError: | 30 | 30 | pandas = None | 31 | 31 |
| 32 | 32 | try: | 33 | 33 | import pyarrow # type: ignore | 34 | | -except ImportError: # pragma: NO COVER | | 34 | +except ImportError: | 35 | 35 | pyarrow = None | 36 | 36 |
| 37 | 37 | try: | 38 | 38 | import db_dtypes # type: ignore | 39 | | -except ImportError: # pragma: NO COVER | | 39 | +except ImportError: | 40 | 40 | db_dtypes = None | 41 | 41 |
| 42 | 42 | try: | |
Collapse file Copy file name to clipboardExpand all lines: noxfile.py +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,7 +86,7 @@ def default(session, install_extras=True): | 86 | 86 | install_target = ".[all]" | 87 | 87 | else: | 88 | 88 | install_target = "." | 89 | | - session.install("-e", install_target) | | 89 | + session.install("-e", install_target, "-c", constraints_path) | 90 | 90 | session.run("python", "-m", "pip", "freeze") | 91 | 91 |
| 92 | 92 | # Run py.test against the unit tests. | @@ -115,14 +115,15 @@ def unit(session): | 115 | 115 | def unit_noextras(session): | 116 | 116 | """Run the unit test suite.""" | 117 | 117 |
| 118 | | - # Install optional dependencies that are out-of-date. | | 118 | + # Install optional dependencies that are out-of-date to see that | | 119 | + # we fail gracefully. | 119 | 120 | # https://github.com/googleapis/python-bigquery/issues/933 | 120 | | - # There is no pyarrow 1.0.0 package for Python 3.9. | 121 | | - | | 121 | + # | | 122 | + # We only install this extra package on one of the two Python versions | | 123 | + # so that it continues to be an optional dependency. | | 124 | + # https://github.com/googleapis/python-bigquery/issues/1877 | 122 | 125 | if session.python == UNIT_TEST_PYTHON_VERSIONS[0]: | 123 | | - session.install("pyarrow>=3.0.0") | 124 | | - elif session.python == UNIT_TEST_PYTHON_VERSIONS[-1]: | 125 | | - session.install("pyarrow") | | 126 | + session.install("pyarrow==1.0.0") | 126 | 127 |
| 127 | 128 | default(session, install_extras=False) | 128 | 129 |
| |
Collapse file Copy file name to clipboardExpand all lines: samples/desktopapp/requirements-test.txt -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,4 +2,3 @@ google-cloud-testutils==1.4.0 | 2 | 2 | pytest===7.4.4; python_version == '3.7' | 3 | 3 | pytest==8.1.1; python_version >= '3.8' | 4 | 4 | mock==5.1.0 | 5 | | -pyarrow>=3.0.0 |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# samples/snippets should be runnable with no "extras" | 1 | 2 | google-cloud-testutils==1.4.0 | 2 | 3 | pytest===7.4.4; python_version == '3.7' | 3 | 4 | pytest==8.1.1; python_version >= '3.8' | 4 | 5 | mock==5.1.0 | 5 | | -pyarrow>=3.0.0 |
|