GeezerVison is showing github.com — feat: enable mypy session for db-dtypes (#16689) · googleapis/google-cloud-python@856731e
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: packages/db-dtypes/db_dtypes/__init__.py +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,8 @@ class TimeDtype(core.BaseDatetimeDtype): | 55 | 55 | name = time_dtype_name | 56 | 56 | type = datetime.time | 57 | 57 |
| 58 | | - def construct_array_type(self): | | 58 | + @classmethod | | 59 | + def construct_array_type(cls): | 59 | 60 | return TimeArray | 60 | 61 |
| 61 | 62 | @staticmethod | @@ -213,7 +214,8 @@ class DateDtype(core.BaseDatetimeDtype): | 213 | 214 | name = date_dtype_name | 214 | 215 | type = datetime.date | 215 | 216 |
| 216 | | - def construct_array_type(self): | | 217 | + @classmethod | | 218 | + def construct_array_type(cls): | 217 | 219 | return DateArray | 218 | 220 |
| 219 | 221 | @staticmethod | @@ -322,7 +324,7 @@ def __add__(self, other): | 322 | 324 | if isinstance(other, TimeArray): | 323 | 325 | return (other._ndarray - _NPEPOCH) + self._ndarray | 324 | 326 |
| 325 | | - return super().__add__(other) | | 327 | + return super().__add__(other) # type: ignore[misc] | 326 | 328 |
| 327 | 329 | def __radd__(self, other): | 328 | 330 | return self.__add__(other) | @@ -334,7 +336,7 @@ def __sub__(self, other): | 334 | 336 | if isinstance(other, self.__class__): | 335 | 337 | return self._ndarray - other._ndarray | 336 | 338 |
| 337 | | - return super().__sub__(other) | | 339 | + return super().__sub__(other) # type: ignore[misc] | 338 | 340 |
| 339 | 341 |
| 340 | 342 | def _check_python_version(): | |
Collapse file Copy file name to clipboardExpand all lines: packages/db-dtypes/db_dtypes/core.py +13-2Lines changed: 13 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | # See the License for the specific language governing permissions and | 13 | 13 | # limitations under the License. | 14 | 14 |
| 15 | | -from typing import Optional | | 15 | +from typing import Optional, Callable, Any | 16 | 16 |
| 17 | 17 | import numpy | 18 | 18 | import pandas | @@ -50,6 +50,13 @@ class BaseDatetimeArray(pandas_backports.OpsMixin, _mixins.NDArrayBackedExtensio | 50 | 50 | # https://github.com/pandas-dev/pandas/blob/main/pandas/core/arrays/_mixins.py | 51 | 51 | _internal_fill_value = numpy.datetime64("NaT") | 52 | 52 |
| | 53 | + _box_func: Callable[[Any], Any] | | 54 | + _from_backing_data: Callable[[Any], Any] | | 55 | + | | 56 | + @classmethod | | 57 | + def _datetime(cls, value: Any) -> Any: | | 58 | + raise NotImplementedError | | 59 | + | 53 | 60 | def __init__(self, values, dtype=None, copy: bool = False): | 54 | 61 | if not ( | 55 | 62 | isinstance(values, numpy.ndarray) and values.dtype == numpy.dtype("<M8[ns]") | @@ -58,7 +65,11 @@ def __init__(self, values, dtype=None, copy: bool = False): | 58 | 65 | elif copy: | 59 | 66 | values = values.copy() | 60 | 67 |
| 61 | | - super().__init__(values=values, dtype=values.dtype) | | 68 | + # We must pass values and dtype to the base constructor. | | 69 | + # Manual assignment (self._ndarray = values) will fail at runtime with | | 70 | + # AttributeError because the base is a Cython-backed 'NDArrayBacked' | | 71 | + # object with non-writable attributes. | | 72 | + super().__init__(values=values, dtype=values.dtype) # type: ignore[call-arg] | 62 | 73 |
| 63 | 74 | @classmethod | 64 | 75 | def __ndarray(cls, scalars): | |
Collapse file Copy file name to clipboardExpand all lines: packages/db-dtypes/db_dtypes/json.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,12 +32,12 @@ class JSONDtype(pd.api.extensions.ExtensionDtype): | 32 | 32 | name = "dbjson" | 33 | 33 |
| 34 | 34 | @property | 35 | | - def na_value(self) -> pd.NA: | | 35 | + def na_value(self) -> pd.NAType: # type: ignore[name-defined] | 36 | 36 | """Default NA value to use for this type.""" | 37 | 37 | return pd.NA | 38 | 38 |
| 39 | 39 | @property | 40 | | - def type(self) -> type[str]: | | 40 | + def type(self) -> type[str]: # type: ignore[override] | 41 | 41 | """ | 42 | 42 | Return the scalar type for the array elements. | 43 | 43 | The standard JSON data types can be one of `dict`, `list`, `str`, `int`, `float`, | @@ -203,7 +203,7 @@ def __getitem__(self, item): | 203 | 203 | assert item.dtype.kind == "b" | 204 | 204 | return type(self)(self.pa_data.filter(item)) | 205 | 205 | elif isinstance(item, tuple): | 206 | | - item = indexers.unpack_tuple_and_ellipses(item) | | 206 | + item = indexers.unpack_tuple_and_ellipses(item) # type: ignore[attr-defined] | 207 | 207 |
| 208 | 208 | if common.is_scalar(item) and not common.is_integer(item): | 209 | 209 | # e.g. "foo" or 2.5 | |
Collapse file Copy file name to clipboardExpand all lines: packages/db-dtypes/db_dtypes/pandas_backports.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | pandas_release = packaging.version.parse(pandas.__version__).release | 27 | 27 |
| 28 | 28 | # # Create aliases for private methods in case they move in a future version. | 29 | | -nanall = pandas.core.nanops.nanall | 30 | | -nanany = pandas.core.nanops.nanany | 31 | | -nanmax = pandas.core.nanops.nanmax | 32 | | -nanmin = pandas.core.nanops.nanmin | | 29 | +nanall = pandas.core.nanops.nanall # type: ignore[attr-defined] | | 30 | +nanany = pandas.core.nanops.nanany # type: ignore[attr-defined] | | 31 | +nanmax = pandas.core.nanops.nanmax # type: ignore[attr-defined] | | 32 | +nanmin = pandas.core.nanops.nanmin # type: ignore[attr-defined] | 33 | 33 | numpy_validate_all = pandas.compat.numpy.function.validate_all | 34 | 34 | numpy_validate_any = pandas.compat.numpy.function.validate_any | 35 | 35 | numpy_validate_max = pandas.compat.numpy.function.validate_max | 36 | 36 | numpy_validate_min = pandas.compat.numpy.function.validate_min | 37 | 37 |
| 38 | | -nanmedian = pandas.core.nanops.nanmedian | | 38 | +nanmedian = pandas.core.nanops.nanmedian # type: ignore[attr-defined] | 39 | 39 | numpy_validate_median = pandas.compat.numpy.function.validate_median | 40 | 40 |
| 41 | 41 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/db-dtypes/noxfile.py +15-3Lines changed: 15 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -487,6 +487,18 @@ def core_deps_from_source(session): | 487 | 487 | @nox.session(python=DEFAULT_PYTHON_VERSION) | 488 | 488 | def mypy(session): | 489 | 489 | """Run the type checker.""" | 490 | | - # TODO(https://github.com/googleapis/google-cloud-python/issues/16014): | 491 | | - # Add mypy tests | 492 | | - session.skip("mypy tests are not yet supported") | | 490 | + session.install( | | 491 | + "mypy<1.16.0", | | 492 | + "types-requests", | | 493 | + "types-protobuf", | | 494 | + "pandas-stubs", | | 495 | + ) | | 496 | + session.install("-e", ".") | | 497 | + session.run( | | 498 | + "mypy", | | 499 | + "-p", | | 500 | + "db_dtypes", | | 501 | + "--check-untyped-defs", | | 502 | + "--ignore-missing-imports", | | 503 | + *session.posargs, | | 504 | + ) |
|