home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 748684119

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
748684119 MDU6SXNzdWU3NDg2ODQxMTk= 4601 Don't type check __getattr__? 10194086 open 0     8 2020-11-23T10:41:21Z 2023-09-25T05:33:09Z   MEMBER      

In #4592 I had the issue that mypy did not raise an error on a missing method:

```python from xarray.core.common import DataWithCoords

hasattr(xr.core.common.DataWithCoords, "reduce") # -> False

def test(x: "DataWithCoords"): x.reduce() # mypy does not error ```

This is because DataWithCoords implements __getattr__:

```python

class A: pass

class B: def getattr(self, name): ...

def testA(x: "A"): x.reduce() # mypy errors

def testB(x: "B"): x.reduce() # mypy does not error ```

The solution seems to be to not typecheck __getattr__ (see https://github.com/python/mypy/issues/6251#issuecomment-457287161):

```python from typing import no_type_check

class C: @no_type_check def getattr(self, name): ...

def testC(x: "C"): x.reduce() # mypy errors ```

The only __getattr__ within xarray is here:

https://github.com/pydata/xarray/blob/17358922d480c038e66430735bf4c365a7677df8/xarray/core/common.py#L221

Using @no_type_check leads to 24 errors and not all of them can be trivially solved. E.g. DataWithCoords wants of use self.isel but does not implement the method. The solution is probably to add isel to DataWithCoords as an ABC or using NotImplemented.

Thoughts?

All errors

```python-traceback xarray/core/common.py:370: error: "DataWithCoords" has no attribute "isel" xarray/core/common.py:374: error: "DataWithCoords" has no attribute "dims" xarray/core/common.py:378: error: "DataWithCoords" has no attribute "indexes" xarray/core/common.py:381: error: "DataWithCoords" has no attribute "sizes" xarray/core/common.py:698: error: "DataWithCoords" has no attribute "_groupby_cls" xarray/core/common.py:761: error: "DataWithCoords" has no attribute "_groupby_cls" xarray/core/common.py:866: error: "DataWithCoords" has no attribute "_rolling_cls"; maybe "_rolling_exp_cls"? xarray/core/common.py:977: error: "DataWithCoords" has no attribute "_coarsen_cls" xarray/core/common.py:1108: error: "DataWithCoords" has no attribute "dims" xarray/core/common.py:1109: error: "DataWithCoords" has no attribute "dims" xarray/core/common.py:1133: error: "DataWithCoords" has no attribute "indexes" xarray/core/common.py:1144: error: "DataWithCoords" has no attribute "_resample_cls"; maybe "resample"? xarray/core/common.py:1261: error: "DataWithCoords" has no attribute "isel" xarray/core/alignment.py:278: error: "DataAlignable" has no attribute "copy" xarray/core/alignment.py:283: error: "DataAlignable" has no attribute "dims" xarray/core/alignment.py:286: error: "DataAlignable" has no attribute "indexes" xarray/core/alignment.py:288: error: "DataAlignable" has no attribute "sizes" xarray/core/alignment.py:348: error: "DataAlignable" has no attribute "dims" xarray/core/alignment.py:351: error: "DataAlignable" has no attribute "copy" xarray/core/alignment.py:353: error: "DataAlignable" has no attribute "reindex" xarray/core/alignment.py:356: error: "DataAlignable" has no attribute "encoding" xarray/core/weighted.py:157: error: "DataArray" has no attribute "notnull" xarray/core/dataset.py:3792: error: "Dataset" has no attribute "virtual_variables" xarray/core/dataset.py:6135: error: "DataArray" has no attribute "isnull" ```

Edit: one problem is certainly the method injection, as mypy cannot detect those types.

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/4601/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
    13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 5 rows from issue in issue_comments
Powered by Datasette · Queries took 0.731ms · About: xarray-datasette