gurudesh/pytracebugs huggingface dataset:
# Buggy code
def status(self):
    Status = collections.namedtuple("Status", ["name", "state", "provider", "ports"])
    status_list = []
    for container in self.instances:
        name = container.get("name")
        if container.get("created"):
            cd = self._docker.containers(filters={"name": name})[0]
            status_list.append(
                Status(
                    name=name,
                    state=cd.get("Status"),
                    provider=self.provider,
                    ports=cd.get("Ports"),
                )
            )
        else:
            status_list.append(
                Status(name=name, state="not_created", provider=self.provider, ports=[])
            )

    return status_list


# Traceback (IndexError)
docker stop test-01 &amp;&amp; molecule -v &amp;&amp; molecule status
1.11.0
Traceback (most recent call last):
File "/usr/local/bin/molecule", line 11, in <module>
sys.exit(main())
File "/Library/Python/2.7/site-packages/molecule/cli.py", line 68, in main
CLI().main()
File "/Library/Python/2.7/site-packages/molecule/cli.py", line 64, in main
util.sysexit(c.execute()[0])
File "/Library/Python/2.7/site-packages/molecule/command/status.py", line 57, in execute
status = self.molecule.driver.status()
File "/Library/Python/2.7/site-packages/molecule/driver/dockerdriver.py", line 176, in status
cd = self._docker.containers(filters={'name': name})[0]
IndexError: list index out of range

# Fixed code
def status(self):
    Status = collections.namedtuple("Status", ["name", "state", "provider", "ports"])
    status_list = []
    for container in self.instances:
        name = container.get("name")
        try:
            d = self._docker.containers(filters={"name": name})[0]
            state = d.get("Status")
            ports = d.get("Ports")
        except IndexError:
            state = "not_created"
            ports = []
        status_list.append(
            Status(name=name, state=state, provider=self.provider, ports=ports)
        )

    return status_list
# Buggy code
def _get_driver_name(self):
    driver = self.args.get("--driver")
    if driver:
        return driver
    elif self.config.config.get("driver"):
        return self.config.config["driver"].get("name")
    elif "vagrant" in self.config.config:
        return "vagrant"
    elif "docker" in self.config.config:
        return "docker"
    elif "openstack" in self.config.config:
        return "openstack"


# Traceback (TypeError)
# molecule create --platform osx
--> Creating instances ...
Traceback (most recent call last):
File "/usr/local/bin/molecule", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/site-packages/molecule/cli.py", line 68, in main
CLI().main()
File "/usr/local/lib/python2.7/site-packages/molecule/cli.py", line 64, in main
util.sysexit(c.execute()[0])
File "/usr/local/lib/python2.7/site-packages/molecule/command/create.py", line 55, in execute
self.molecule.driver.up(no_provision=True)
File "/usr/local/lib/python2.7/site-packages/molecule/driver/vagrantdriver.py", line 131, in up
self._populate_platform_instances()
File "/usr/local/lib/python2.7/site-packages/molecule/driver/vagrantdriver.py", line 190, in _populate_platform_instances
1) and (self.molecule.args['--platform'] == 'all'
TypeError: object of type 'bool' has no len()

# Fixed code
def _get_driver_name(self):
    driver = self.args.get("driver")
    if driver:
        return driver
    elif self.config.config.get("driver"):
        return self.config.config["driver"].get("name")
    elif "vagrant" in self.config.config:
        return "vagrant"
    elif "docker" in self.config.config:
        return "docker"
    elif "openstack" in self.config.config:
        return "openstack"
# Buggy code
def _populate_platform_instances(self):
    if self.molecule.args.get("platform"):
        if (
            len((self.molecule.config.config["vagrant"]["platforms"]) > 1)
            and (self.molecule.args.get("platform") == "all")
            and not self._updated_multiplatform
        ):
            new_instances = []

            for instance in self.molecule.config.config["vagrant"]["instances"]:
                for platform in self.molecule.config.config["vagrant"]["platforms"]:
                    platform_instance = copy.deepcopy(instance)
                    platform_instance["platform"] = platform["box"]
                    platform_instance["name"] = (
                        instance["name"] + "-" + platform["name"]
                    )
                    platform_instance["vm_name"] = (
                        instance["name"] + "-" + platform["name"]
                    )
                    new_instances.append(platform_instance)

            self.molecule.config.config["vagrant"]["instances"] = new_instances
            self._updated_multiplatform = True


# Traceback (TypeError)
# molecule create --platform osx
--> Creating instances ...
Traceback (most recent call last):
File "/usr/local/bin/molecule", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/site-packages/molecule/cli.py", line 68, in main
CLI().main()
File "/usr/local/lib/python2.7/site-packages/molecule/cli.py", line 64, in main
util.sysexit(c.execute()[0])
File "/usr/local/lib/python2.7/site-packages/molecule/command/create.py", line 55, in execute
self.molecule.driver.up(no_provision=True)
File "/usr/local/lib/python2.7/site-packages/molecule/driver/vagrantdriver.py", line 131, in up
self._populate_platform_instances()
File "/usr/local/lib/python2.7/site-packages/molecule/driver/vagrantdriver.py", line 190, in _populate_platform_instances
1) and (self.molecule.args['--platform'] == 'all'
TypeError: object of type 'bool' has no len()

# Fixed code
def _populate_platform_instances(self):
    if self.molecule.args.get("platform"):
        if (
            (self.molecule.config.config["vagrant"]["platforms"] > 1)
            and (self.molecule.args["platform"] == "all")
            and not self._updated_multiplatform
        ):
            new_instances = []

            for instance in self.molecule.config.config["vagrant"]["instances"]:
                for platform in self.molecule.config.config["vagrant"]["platforms"]:
                    platform_instance = copy.deepcopy(instance)
                    platform_instance["platform"] = platform["box"]
                    platform_instance["name"] = (
                        instance["name"] + "-" + platform["name"]
                    )
                    platform_instance["vm_name"] = (
                        instance["name"] + "-" + platform["name"]
                    )
                    new_instances.append(platform_instance)

            self.molecule.config.config["vagrant"]["instances"] = new_instances
            self._updated_multiplatform = True
# Buggy code
def _build_config_paths(self):
    """
    Convenience function to build up paths from our config values.  Path
    will not be relative to ``molecule_dir``, when a full path was provided
    in the config.

    :return: None
    """
    for item in ["state_file", "vagrantfile_file", "rakefile_file"]:
        d = self.config.get("molecule")
        if d:
            d[item] = os.path.join(
                self.config["molecule"]["molecule_dir"], self.config["molecule"][item]
            )

    for item in ["config_file", "inventory_file"]:
        d = self.config.get("ansible")
        if d:
            d[item] = os.path.join(
                self.config["molecule"]["molecule_dir"], self.config["ansible"][item]
            )


# Traceback (IOError)
molecule destroy
Traceback (most recent call last):
File "/usr/local/bin/molecule", line 10, in <module>
sys.exit(main())
File "/Users/abrown/projects/cisco/molecule/molecule/cli.py", line 68, in main
CLI().main()
File "/Users/abrown/projects/cisco/molecule/molecule/cli.py", line 64, in main
utilities.sysexit(c.execute()[0])
File "/Users/abrown/projects/cisco/molecule/molecule/commands/destroy.py", line 47, in execute
self.molecule._create_templates()
File "/Users/abrown/projects/cisco/molecule/molecule/core.py", line 197, in _create_templates
self._config.config['ansible']['config_file'], kwargs=kwargs)
File "/Users/abrown/projects/cisco/molecule/molecule/utilities.py", line 104, in write_template
with open(dest, 'w') as f:
IOError: [Errno 2] No such file or directory: '.molecule/../../.molecule/ansible.cfg'

# Fixed code
def _build_config_paths(self):
    """
    Convenience function to build up paths from our config values.  Path
    will not be relative to ``molecule_dir``, when a full path was provided
    in the config.

    :return: None
    """
    md = self.config.get("molecule")
    ad = self.config.get("ansible")
    for item in ["state_file", "vagrantfile_file", "rakefile_file"]:
        if md and not self._is_path(md[item]):
            md[item] = os.path.join(md["molecule_dir"], md[item])

    for item in ["config_file", "inventory_file"]:
        if ad and not self._is_path(ad[item]):
            ad[item] = os.path.join(md["molecule_dir"], ad[item])
# Buggy code
def create(self, event_id, data, url):
    data = self.validate(data, event_id)
    payload = self.fix_payload_post(event_id, data)
    speakers = payload.pop("speakers", None)
    session, status_code, location = ServiceDAO.create(
        self, event_id, payload, url, validate=False
    )
    if speakers:
        session.speakers = speakers
        save_to_db(session)
    if session.state == "pending":
        trigger_new_session_notifications(session.id, event_id=event_id)
    return session, status_code, location


# Traceback (KeyError)
[2016-07-28 00:35:59,077: ERROR/MainProcess] Received unregistered task of type 'import.event'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'utc': True, 'chord': None, 'args': ('/home/aviaryan/github/open-event-orga-server/static/temp/event109.zip',), 'retries': 0, 'expires': None, 'task': 'import.event', 'callbacks': None, 'errbacks': None, 'timelimit': (None, None), 'taskset': None, 'kwargs': {'_flask_request_context': {'headers': {'Origin': u'http://localhost:5000', 'Content-Length': u'342747', 'Accept-Language': u'en-US,en;q=0.8', 'Accept-Encoding': u'gzip, deflate', 'Connection': u'keep-alive', 'Accept': u'*/*', 'User-Agent': u'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36', 'Host': u'localhost:5000', 'X-Requested-With': u'XMLHttpRequest', 'Cookie': u'_ga=GA1.1.135907395.1461594114;... (2240b)
Traceback (most recent call last):
File "/home/aviaryan/anaconda2/lib/python2.7/site-packages/celery/worker/consumer.py", line 456, in on_task_received
strategies[name](message, body,
KeyError: 'import.event'

# Fixed code
def create(self, event_id, data, url):
    data = self.validate(data, event_id)
    payload = self.fix_payload_post(event_id, data)
    speakers = payload.pop("speakers", None)
    session, status_code, location = ServiceDAO.create(
        self, event_id, payload, url, validate=False
    )
    if speakers:
        session.speakers = speakers
        save_to_db(session)
    if not self.is_importing and session.state == "pending":
        trigger_new_session_notifications(session.id, event_id=event_id)
    return session, status_code, location
# Buggy code
def __init__(
    self,
    name=None,
    logo=None,
    start_time=None,
    end_time=None,
    latitude=None,
    longitude=None,
    location_name=None,
    email=None,
    color=None,
    slogan=None,
    url=None,
):
    self.name = name
    self.logo = logo
    self.email = email
    self.color = color
    self.start_time = start_time
    self.end_time = end_time
    self.latitude = latitude
    self.longitude = longitude
    self.location_name = location_name
    self.slogan = slogan
    self.url = url


# Traceback (UnicodeEncodeError)
t  = Track.query.all()[-1]   # the above track
t
<Track u'\uff61\u25d5\u203f\u25d5\uff61)'>
print t
Traceback (most recent call last):
File "<console>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

# Fixed code
def __init__(
    self,
    name=None,
    logo=None,
    start_time=None,
    end_time=None,
    latitude=None,
    longitude=None,
    location_name=None,
    email=None,
    color=None,
    slogan=None,
    url=None,
):
    self.name = name
    self.logo = logo
    self.email = email
    self.color = color
    self.start_time = start_time
    self.end_time = end_time
    self.latitude = latitude
    self.longitude = longitude
    self.location_name = location_name
    self.slogan = slogan
    self.url = url
# Buggy code
def __str__(self):
    return self.name


# Traceback (UnicodeEncodeError)
t  = Track.query.all()[-1]   # the above track
t
<Track u'\uff61\u25d5\u203f\u25d5\uff61)'>
print t
Traceback (most recent call last):
File "<console>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

# Fixed code
def __str__(self):
    return unicode(self).encode("utf-8")
# Buggy code
def __str__(self):
    return self.title


# Traceback (UnicodeEncodeError)
t  = Track.query.all()[-1]   # the above track
t
<Track u'\uff61\u25d5\u203f\u25d5\uff61)'>
print t
Traceback (most recent call last):
File "<console>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

# Fixed code
def __str__(self):
    return unicode(self).encode("utf-8")
# Buggy code
def serialize(self):
    """Return object data in easily serializeable format"""
    return {
        "version": [
            {
                "id": self.id,
                "event_id": self.event_id,
                "event_ver": self.event_ver,
                "session_ver": self.session_ver,
                "speakers_ver": self.speakers_ver,
                "tracks_ver": self.tracks_ver,
                "sponsors_ver": self.sponsors_ver,
                "microlocations_ver": self.microlocations_ver,
            }
        ]
    }


# Traceback (UnicodeEncodeError)
t  = Track.query.all()[-1]   # the above track
t
<Track u'\uff61\u25d5\u203f\u25d5\uff61)'>
print t
Traceback (most recent call last):
File "<console>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

# Fixed code
def serialize(self):
    """Return object data in easily serializeable format"""
    return {
        "version": [
            {
                "id": self.id,
                "event_id": self.event_id,
                "event_ver": self.event_ver,
                "session_ver": self.session_ver,
                "speakers_ver": self.speakers_ver,
                "tracks_ver": self.tracks_ver,
                "sponsors_ver": self.sponsors_ver,
                "microlocations_ver": self.microlocations_ver,
            }
        ]
    }
# Buggy code
def astype(data, **kwargs):
    try:
        import sparse
    except ImportError:
        sparse = None

    if (
        sparse is not None
        and isinstance(data, sparse_array_type)
        and LooseVersion(sparse.__version__) < LooseVersion("0.11.0")
        and "casting" in kwargs
    ):
        warnings.warn(
            "The current version of sparse does not support the 'casting' argument. It will be ignored in the call to astype().",
            RuntimeWarning,
            stacklevel=4,
        )
        kwargs.pop("casting")

    return data.astype(**kwargs)


# Traceback (TypeError)
In [1]: import xarray as xr

In [2]: xr.__version__
Out[2]: '0.16.2'

In [3]: xr.DataArray([[1.0, 2.0], [3.0, 4.0]]).astype(dtype='d', order='F').values.strides
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-208ab49008ef> in <module>
----> 1 xr.DataArray([[1.0, 2.0], [3.0, 4.0]]).astype(dtype='d', order='F').values.strides

TypeError: astype() got an unexpected keyword argument 'order'

# Fixed code
def astype(data, dtype, **kwargs):
    try:
        import sparse
    except ImportError:
        sparse = None

    if (
        sparse is not None
        and isinstance(data, sparse_array_type)
        and LooseVersion(sparse.__version__) < LooseVersion("0.11.0")
        and "casting" in kwargs
    ):
        warnings.warn(
            "The current version of sparse does not support the 'casting' argument. It will be ignored in the call to astype().",
            RuntimeWarning,
            stacklevel=4,
        )
        kwargs.pop("casting")

    return data.astype(dtype, **kwargs)
# Buggy code
def _get_engine_from_magic_number(filename_or_obj):
    # check byte header to determine file type
    if isinstance(filename_or_obj, bytes):
        magic_number = filename_or_obj[:8]
    else:
        if filename_or_obj.tell() != 0:
            raise ValueError(
                "file-like object read/write pointer not at zero "
                "please close and reopen, or use a context "
                "manager"
            )
        magic_number = filename_or_obj.read(8)
        filename_or_obj.seek(0)

    if magic_number.startswith(b"CDF"):
        engine = "scipy"
    elif magic_number.startswith(b"\211HDF\r\n\032\n"):
        engine = "h5netcdf"
        if isinstance(filename_or_obj, bytes):
            raise ValueError(
                "can't open netCDF4/HDF5 as bytes "
                "try passing a path or file-like object"
            )
    else:
        if isinstance(filename_or_obj, bytes) and len(filename_or_obj) > 80:
            filename_or_obj = filename_or_obj[:80] + b"..."
        raise ValueError(
            "{} is not a valid netCDF file "
            "did you mean to pass a string for a path instead?".format(filename_or_obj)
        )
    return engine


# Traceback (TypeError)
xr.open_dataset('netCDF4.nc', engine='scipy')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
[...]
TypeError: Error: netCDF4.nc is not a valid NetCDF 3 file
If this is a NetCDF4 file, you may need to install the
netcdf4 library, e.g.,

$ pip install netcdf4

fp = open('netCDF4.nc')
xr.open_dataset(fp, engine='scipy')
<xarray.Dataset>
Dimensions:       (line: 41587, pixel: 17751)
Coordinates:
* line          (line) int64 0 1 2 3 4 5 ... 41582 41583 41584 41585 41586
* pixel         (pixel) int64 0 1 2 3 4 5 ... 17746 17747 17748 17749 17750
Data variables:
phase         (line, pixel) float32 ...
Attributes:
Conventions:             CF-1.7
[...]

# Fixed code
def _get_engine_from_magic_number(filename_or_obj):
    # check byte header to determine file type
    if isinstance(filename_or_obj, bytes):
        magic_number = filename_or_obj[:8]
    else:
        if filename_or_obj.tell() != 0:
            raise ValueError(
                "file-like object read/write pointer not at zero "
                "please close and reopen, or use a context manager"
            )
        magic_number = filename_or_obj.read(8)
        filename_or_obj.seek(0)

    if magic_number.startswith(b"CDF"):
        engine = "scipy"
    elif magic_number.startswith(b"\211HDF\r\n\032\n"):
        engine = "h5netcdf"
    else:
        raise ValueError(
            f"{magic_number} is not the signature of any supported file format "
            "did you mean to pass a string for a path instead?"
        )
    return engine
# Buggy code
def _normalize_path(path):
    if is_remote_uri(path):
        return path
    else:
        return os.path.abspath(os.path.expanduser(path))


# Traceback (TypeError)
xr.open_dataset('netCDF4.nc', engine='scipy')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
[...]
TypeError: Error: netCDF4.nc is not a valid NetCDF 3 file
If this is a NetCDF4 file, you may need to install the
netcdf4 library, e.g.,

$ pip install netcdf4

fp = open('netCDF4.nc')
xr.open_dataset(fp, engine='scipy')
<xarray.Dataset>
Dimensions:       (line: 41587, pixel: 17751)
Coordinates:
* line          (line) int64 0 1 2 3 4 5 ... 41582 41583 41584 41585 41586
* pixel         (pixel) int64 0 1 2 3 4 5 ... 17746 17747 17748 17749 17750
Data variables:
phase         (line, pixel) float32 ...
Attributes:
Conventions:             CF-1.7
[...]

# Fixed code
def _normalize_path(path):
    if isinstance(path, Path):
        path = str(path)

    if isinstance(path, str) and not is_remote_uri(path):
        path = os.path.abspath(os.path.expanduser(path))

    return path
# Buggy code
def open(
    cls,
    filename,
    mode="r",
    format="NETCDF4",
    group=None,
    clobber=True,
    diskless=False,
    persist=False,
    lock=None,
    lock_maker=None,
    autoclose=False,
):
    import netCDF4

    if format is None:
        format = "NETCDF4"

    if lock is None:
        if mode == "r":
            if is_remote_uri(filename):
                lock = NETCDFC_LOCK
            else:
                lock = NETCDF4_PYTHON_LOCK
        else:
            if format is None or format.startswith("NETCDF4"):
                base_lock = NETCDF4_PYTHON_LOCK
            else:
                base_lock = NETCDFC_LOCK
            lock = combine_locks([base_lock, get_write_lock(filename)])

    kwargs = dict(clobber=clobber, diskless=diskless, persist=persist, format=format)
    manager = CachingFileManager(netCDF4.Dataset, filename, mode=mode, kwargs=kwargs)
    return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)


# Traceback (TypeError)
xr.open_dataset('netCDF4.nc', engine='scipy')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
[...]
TypeError: Error: netCDF4.nc is not a valid NetCDF 3 file
If this is a NetCDF4 file, you may need to install the
netcdf4 library, e.g.,

$ pip install netcdf4

fp = open('netCDF4.nc')
xr.open_dataset(fp, engine='scipy')
<xarray.Dataset>
Dimensions:       (line: 41587, pixel: 17751)
Coordinates:
* line          (line) int64 0 1 2 3 4 5 ... 41582 41583 41584 41585 41586
* pixel         (pixel) int64 0 1 2 3 4 5 ... 17746 17747 17748 17749 17750
Data variables:
phase         (line, pixel) float32 ...
Attributes:
Conventions:             CF-1.7
[...]

# Fixed code
def open(
    cls,
    filename,
    mode="r",
    format="NETCDF4",
    group=None,
    clobber=True,
    diskless=False,
    persist=False,
    lock=None,
    lock_maker=None,
    autoclose=False,
):
    import netCDF4

    if not isinstance(filename, str):
        raise ValueError(
            "can only read bytes or file-like objects with engine='scipy' or 'h5netcdf'"
        )

    if format is None:
        format = "NETCDF4"

    if lock is None:
        if mode == "r":
            if is_remote_uri(filename):
                lock = NETCDFC_LOCK
            else:
                lock = NETCDF4_PYTHON_LOCK
        else:
            if format is None or format.startswith("NETCDF4"):
                base_lock = NETCDF4_PYTHON_LOCK
            else:
                base_lock = NETCDFC_LOCK
            lock = combine_locks([base_lock, get_write_lock(filename)])

    kwargs = dict(clobber=clobber, diskless=diskless, persist=persist, format=format)
    manager = CachingFileManager(netCDF4.Dataset, filename, mode=mode, kwargs=kwargs)
    return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)
# Buggy code
def last_item(array):
    """Returns the last item of an array in a list or an empty list."""
    if array.size == 0:
        # work around for https://github.com/numpy/numpy/issues/5195
        return []

    indexer = (slice(-1, None),) * array.ndim
    return np.ravel(array[indexer]).tolist()


# Traceback (AttributeError)
Traceback (most recent call last):
File "tmp.py", line 11, in <module>
ds = xarray.decode_cf(ds)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/conventions.py", line 479, in decode_cf
decode_coords, drop_variables=drop_variables, use_cftime=use_cftime)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/conventions.py", line 401, in decode_cf_variables
stack_char_dim=stack_char_dim, use_cftime=use_cftime)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/conventions.py", line 306, in decode_cf_variable
var = coder.decode(var, name=name)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/coding/times.py", line 419, in decode
self.use_cftime)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/coding/times.py", line 90, in _decode_cf_datetime_dtype
last_item(values) or [0]])
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/core/formatting.py", line 99, in last_item
return np.ravel(array[indexer]).tolist()
AttributeError: 'Array' object has no attribute 'tolist'

# Fixed code
def last_item(array):
    """Returns the last item of an array in a list or an empty list."""
    if array.size == 0:
        # work around for https://github.com/numpy/numpy/issues/5195
        return []

    indexer = (slice(-1, None),) * array.ndim
    return np.ravel(np.asarray(array[indexer])).tolist()
# Buggy code
def __sub__(self, other):
    if isinstance(other, CFTimeIndex):
        return pd.TimedeltaIndex(np.array(self) - np.array(other))
    elif isinstance(other, pd.TimedeltaIndex):
        return CFTimeIndex(np.array(self) - other.to_pytimedelta())
    else:
        return CFTimeIndex(np.array(self) - other)


# Traceback (TypeError)
In [1]: import xarray

In [2]: times = xarray.cftime_range('2000', periods=3)

In [3]: times - times[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-97cbca76a8af> in <module>
----> 1 times - times[0]

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in __sub__(self, other)
417             return CFTimeIndex(np.array(self) - other.to_pytimedelta())
418         else:
--> 419             return CFTimeIndex(np.array(self) - other)
420
421     def _add_delta(self, deltas):

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in __new__(cls, data, name)
238         result = object.__new__(cls)
239         result._data = np.array(data, dtype='O')
--> 240         assert_all_valid_date_type(result._data)
241         result.name = name
242         return result

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in assert_all_valid_date_type(data)
194             raise TypeError(
195                 'CFTimeIndex requires cftime.datetime '
--> 196                 'objects. Got object of {}.'.format(date_type))
197         if not all(isinstance(value, date_type) for value in data):
198             raise TypeError(

TypeError: CFTimeIndex requires cftime.datetime objects. Got object of <class 'datetime.timedelta'>.

# Fixed code
def __sub__(self, other):
    import cftime

    if isinstance(other, (CFTimeIndex, cftime.datetime)):
        return pd.TimedeltaIndex(np.array(self) - np.array(other))
    elif isinstance(other, pd.TimedeltaIndex):
        return CFTimeIndex(np.array(self) - other.to_pytimedelta())
    else:
        return CFTimeIndex(np.array(self) - other)
# Buggy code
def dask_rolling_wrapper(moving_func, a, window, min_count=None, axis=-1):
    """wrapper to apply bottleneck moving window funcs on dask arrays"""
    # inputs for ghost
    if axis < 0:
        axis = a.ndim + axis
    depth = {d: 0 for d in range(a.ndim)}
    depth[axis] = window - 1
    boundary = {d: np.nan for d in range(a.ndim)}
    # create ghosted arrays
    ag = da.ghost.ghost(a, depth=depth, boundary=boundary)
    # apply rolling func
    out = ag.map_blocks(
        moving_func, window, min_count=min_count, axis=axis, dtype=a.dtype
    )
    # trim array
    result = da.ghost.trim_internal(out, depth)
    return result


# Traceback (ValueError)
ValueError                                Traceback (most recent call last)
<ipython-input-57-6acf382cdd3d> in <module>()
4 da_day_clim = da.groupby('S.dayofyear').mean('S')
5 da_day_clim2 = da_day_clim.chunk({'dayofyear': 366})
----> 6 da_day_clim_smooth = da_day_clim2.rolling(dayofyear=31, center=True).mean()

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/rolling.py in wrapped_func(self, **kwargs)
307             if self.center:
308                 values = values[valid]
--> 309             result = DataArray(values, self.obj.coords)
310
311             return result

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/dataarray.py in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
224
225             data = as_compatible_data(data)
--> 226             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
227             variable = Variable(dims, data, attrs, encoding, fastpath=True)
228

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/dataarray.py in _infer_coords_and_dims(shape, coords, dims)
79                 raise ValueError('conflicting sizes for dimension %r: '
80                                  'length %s on the data but length %s on '
---> 81                                  'coordinate %r' % (d, sizes[d], s, k))
82
83         if k in sizes and v.shape != (sizes[k],):

ValueError: conflicting sizes for dimension 'dayofyear': length 351 on the data but length 366 on coordinate 'dayofyear'

# Fixed code
def dask_rolling_wrapper(moving_func, a, window, min_count=None, axis=-1):
    """wrapper to apply bottleneck moving window funcs on dask arrays"""
    dtype, fill_value = dtypes.maybe_promote(a.dtype)
    a = a.astype(dtype)
    # inputs for ghost
    if axis < 0:
        axis = a.ndim + axis
    depth = {d: 0 for d in range(a.ndim)}
    depth[axis] = window - 1
    boundary = {d: fill_value for d in range(a.ndim)}
    # create ghosted arrays
    ag = da.ghost.ghost(a, depth=depth, boundary=boundary)
    # apply rolling func
    out = ag.map_blocks(
        moving_func, window, min_count=min_count, axis=axis, dtype=a.dtype
    )
    # trim array
    result = da.ghost.trim_internal(out, depth)
    return result
# Buggy code
def prepare_variable(self, k, v, *args, **kwargs):
    new_var = Variable(v.dims, np.empty_like(v), v.attrs)
    # we copy the variable and stuff all encodings in the
    # attributes to imitate what happens when writing to disk.
    new_var.attrs.update(v.encoding)
    self._variables[k] = new_var
    return new_var, v.data


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fff1cd753e7c> in <module>()
2 fs = s3fs.S3FileSystem(anon=False)
3 d = s3fs.S3Map(f_zarr, s3=fs)
----> 4 xr.open_zarr(d)

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in open_zarr(store, group, synchronizer, auto_chunk, decode_cf, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in <listcomp>(.0)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in maybe_chunk(name, var)
471                 token2 = tokenize(name, var._data)
472                 name2 = 'zarr-%s' % token2
--> 473                 return var.chunk(chunks, name=name2, lock=None)
474             else:
475                 return var

/opt/conda/lib/python3.6/site-packages/xarray/core/variable.py in chunk(self, chunks, name, lock)
820             data = indexing.ImplicitToExplicitIndexingAdapter(
821                 data, indexing.OuterIndexer)
--> 822             data = da.from_array(data, chunks, name=name, lock=lock)
823
824         return type(self)(self.dims, data, self._attrs, self._encoding,

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in from_array(x, chunks, name, lock, asarray, fancy, getitem)
1988     >>> a = da.from_array(x, chunks=(1000, 1000), lock=True)  # doctest: +SKIP
1989     """
-> 1990     chunks = normalize_chunks(chunks, x.shape)
1991     if name in (None, True):
1992         token = tokenize(x, chunks)

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in normalize_chunks(chunks, shape)
1918             raise ValueError(
1919                 "Chunks and shape must be of the same length/dimension. "
-> 1920                 "Got chunks=%s, shape=%s" % (chunks, shape))
1921
1922     if shape is not None:

ValueError: Chunks and shape must be of the same length/dimension. Got chunks=(3, 64), shape=(3,)

# Fixed code
def prepare_variable(self, k, v, *args, **kwargs):
    new_var = Variable(v.dims, np.empty_like(v), v.attrs)
    self._variables[k] = new_var
    return new_var, v.data
# Buggy code
def _encode_nc4_variable(var):
    if var.dtype.kind == "S":
        var = conventions.maybe_encode_as_char_array(var)
    return var


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fff1cd753e7c> in <module>()
2 fs = s3fs.S3FileSystem(anon=False)
3 d = s3fs.S3Map(f_zarr, s3=fs)
----> 4 xr.open_zarr(d)

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in open_zarr(store, group, synchronizer, auto_chunk, decode_cf, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in <listcomp>(.0)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in maybe_chunk(name, var)
471                 token2 = tokenize(name, var._data)
472                 name2 = 'zarr-%s' % token2
--> 473                 return var.chunk(chunks, name=name2, lock=None)
474             else:
475                 return var

/opt/conda/lib/python3.6/site-packages/xarray/core/variable.py in chunk(self, chunks, name, lock)
820             data = indexing.ImplicitToExplicitIndexingAdapter(
821                 data, indexing.OuterIndexer)
--> 822             data = da.from_array(data, chunks, name=name, lock=lock)
823
824         return type(self)(self.dims, data, self._attrs, self._encoding,

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in from_array(x, chunks, name, lock, asarray, fancy, getitem)
1988     >>> a = da.from_array(x, chunks=(1000, 1000), lock=True)  # doctest: +SKIP
1989     """
-> 1990     chunks = normalize_chunks(chunks, x.shape)
1991     if name in (None, True):
1992         token = tokenize(x, chunks)

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in normalize_chunks(chunks, shape)
1918             raise ValueError(
1919                 "Chunks and shape must be of the same length/dimension. "
-> 1920                 "Got chunks=%s, shape=%s" % (chunks, shape))
1921
1922     if shape is not None:

ValueError: Chunks and shape must be of the same length/dimension. Got chunks=(3, 64), shape=(3,)

# Fixed code
def _encode_nc4_variable(var):
    for coder in [
        coding.strings.EncodedStringCoder(allows_unicode=True),
        coding.strings.CharacterArrayCoder(),
    ]:
        var = coder.encode(var)
    return var
# Buggy code
def _nc4_dtype(var):
    if var.dtype.kind == "U":
        dtype = str
    elif var.dtype.kind in ["i", "u", "f", "c", "S"]:
        dtype = var.dtype
    else:
        raise ValueError("cannot infer dtype for netCDF4 variable")
    return dtype


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fff1cd753e7c> in <module>()
2 fs = s3fs.S3FileSystem(anon=False)
3 d = s3fs.S3Map(f_zarr, s3=fs)
----> 4 xr.open_zarr(d)

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in open_zarr(store, group, synchronizer, auto_chunk, decode_cf, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in <listcomp>(.0)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in maybe_chunk(name, var)
471                 token2 = tokenize(name, var._data)
472                 name2 = 'zarr-%s' % token2
--> 473                 return var.chunk(chunks, name=name2, lock=None)
474             else:
475                 return var

/opt/conda/lib/python3.6/site-packages/xarray/core/variable.py in chunk(self, chunks, name, lock)
820             data = indexing.ImplicitToExplicitIndexingAdapter(
821                 data, indexing.OuterIndexer)
--> 822             data = da.from_array(data, chunks, name=name, lock=lock)
823
824         return type(self)(self.dims, data, self._attrs, self._encoding,

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in from_array(x, chunks, name, lock, asarray, fancy, getitem)
1988     >>> a = da.from_array(x, chunks=(1000, 1000), lock=True)  # doctest: +SKIP
1989     """
-> 1990     chunks = normalize_chunks(chunks, x.shape)
1991     if name in (None, True):
1992         token = tokenize(x, chunks)

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in normalize_chunks(chunks, shape)
1918             raise ValueError(
1919                 "Chunks and shape must be of the same length/dimension. "
-> 1920                 "Got chunks=%s, shape=%s" % (chunks, shape))
1921
1922     if shape is not None:

ValueError: Chunks and shape must be of the same length/dimension. Got chunks=(3, 64), shape=(3,)

# Fixed code
def _nc4_dtype(var):
    if coding.strings.is_unicode_dtype(var.dtype):
        dtype = str
    elif var.dtype.kind in ["i", "u", "f", "c", "S"]:
        dtype = var.dtype
    else:
        raise ValueError("unsupported dtype for netCDF4 variable: {}".format(var.dtype))
    return dtype
# Buggy code
def encode_nc3_variable(var):
    var = conventions.maybe_encode_as_char_array(var)
    data = coerce_nc3_dtype(var.data)
    attrs = encode_nc3_attrs(var.attrs)
    return Variable(var.dims, data, attrs, var.encoding)


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fff1cd753e7c> in <module>()
2 fs = s3fs.S3FileSystem(anon=False)
3 d = s3fs.S3Map(f_zarr, s3=fs)
----> 4 xr.open_zarr(d)

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in open_zarr(store, group, synchronizer, auto_chunk, decode_cf, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in <listcomp>(.0)
476
477         variables = OrderedDict([(k, maybe_chunk(k, v))
--> 478                                  for k, v in ds.variables.items()])
479         return ds._replace_vars_and_dims(variables)
480     else:

/opt/conda/lib/python3.6/site-packages/xarray/backends/zarr.py in maybe_chunk(name, var)
471                 token2 = tokenize(name, var._data)
472                 name2 = 'zarr-%s' % token2
--> 473                 return var.chunk(chunks, name=name2, lock=None)
474             else:
475                 return var

/opt/conda/lib/python3.6/site-packages/xarray/core/variable.py in chunk(self, chunks, name, lock)
820             data = indexing.ImplicitToExplicitIndexingAdapter(
821                 data, indexing.OuterIndexer)
--> 822             data = da.from_array(data, chunks, name=name, lock=lock)
823
824         return type(self)(self.dims, data, self._attrs, self._encoding,

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in from_array(x, chunks, name, lock, asarray, fancy, getitem)
1988     >>> a = da.from_array(x, chunks=(1000, 1000), lock=True)  # doctest: +SKIP
1989     """
-> 1990     chunks = normalize_chunks(chunks, x.shape)
1991     if name in (None, True):
1992         token = tokenize(x, chunks)

/opt/conda/lib/python3.6/site-packages/dask/array/core.py in normalize_chunks(chunks, shape)
1918             raise ValueError(
1919                 "Chunks and shape must be of the same length/dimension. "
-> 1920                 "Got chunks=%s, shape=%s" % (chunks, shape))
1921
1922     if shape is not None:

ValueError: Chunks and shape must be of the same length/dimension. Got chunks=(3, 64), shape=(3,)

# Fixed code
def encode_nc3_variable(var):
    for coder in [
        coding.strings.EncodedStringCoder(allows_unicode=False),
        coding.strings.CharacterArrayCoder(),
    ]:
        var = coder.encode(var)
    data = coerce_nc3_dtype(var.data)
    attrs = encode_nc3_attrs(var.attrs)
    return Variable(var.dims, data, attrs, var.encoding)
# Buggy code
def __repr__(self):
    return "%s(%r, func=%r, dtype=%r)" % (
        type(self).__name__,
        self.array,
        self._func,
        self._dtype,
    )


# Traceback (UnboundLocalError)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-26-2052b95f2d95> in <module>()
----> 1 no2 = xr.open_dataset(product, group='/PRODUCT')

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, group, decode_cf, mask_and_scale, decode_times, autoclose, concat_characters, decode_coords, engine, chunks, lock, cache, drop_variables)
303             lock = _default_lock(filename_or_obj, engine)
304         with close_on_error(store):
--> 305             return maybe_decode_store(store, lock)
306     else:
307         if engine is not None and engine != 'scipy':

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/backends/api.py in maybe_decode_store(store, lock)
223             store, mask_and_scale=mask_and_scale, decode_times=decode_times,
224             concat_characters=concat_characters, decode_coords=decode_coords,
--> 225             drop_variables=drop_variables)
226
227         _protect_dataset_variables_inplace(ds, cache)

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf(obj, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables)
1153     vars, attrs, coord_names = decode_cf_variables(
1154         vars, attrs, concat_characters, mask_and_scale, decode_times,
-> 1155         decode_coords, drop_variables=drop_variables)
1156     ds = Dataset(vars, attrs=attrs)
1157     ds = ds.set_coords(coord_names.union(extra_coords).intersection(vars))

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables)
1086             k, v, concat_characters=concat_characters,
1087             mask_and_scale=mask_and_scale, decode_times=decode_times,
-> 1088             stack_char_dim=stack_char_dim)
1089         if decode_coords:
1090             var_attrs = new_vars[k].attrs

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf_variable(name, var, concat_characters, mask_and_scale, decode_times, decode_endianness, stack_char_dim)
998         if (has_fill or scale_factor is not None or add_offset is not None):
999             if has_fill and np.array(fill_value).dtype.kind in ['U', 'S', 'O']:
-> 1000                 if string_encoding is not None:
1001                     raise NotImplementedError(
1002                         'variable %r has a _FillValue specified, but '

UnboundLocalError: local variable 'string_encoding' referenced before assignment

# Fixed code
def __repr__(self):
    return "%s(%r, func=%r, dtype=%r)" % (
        type(self).__name__,
        self.array,
        self.func,
        self.dtype,
    )
# Buggy code
def __init__(self, array):
    self.array = indexing.as_indexable(array)
    self.unsigned_dtype = np.dtype("u%s" % array.dtype.itemsize)


# Traceback (UnboundLocalError)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-26-2052b95f2d95> in <module>()
----> 1 no2 = xr.open_dataset(product, group='/PRODUCT')

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, group, decode_cf, mask_and_scale, decode_times, autoclose, concat_characters, decode_coords, engine, chunks, lock, cache, drop_variables)
303             lock = _default_lock(filename_or_obj, engine)
304         with close_on_error(store):
--> 305             return maybe_decode_store(store, lock)
306     else:
307         if engine is not None and engine != 'scipy':

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/backends/api.py in maybe_decode_store(store, lock)
223             store, mask_and_scale=mask_and_scale, decode_times=decode_times,
224             concat_characters=concat_characters, decode_coords=decode_coords,
--> 225             drop_variables=drop_variables)
226
227         _protect_dataset_variables_inplace(ds, cache)

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf(obj, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables)
1153     vars, attrs, coord_names = decode_cf_variables(
1154         vars, attrs, concat_characters, mask_and_scale, decode_times,
-> 1155         decode_coords, drop_variables=drop_variables)
1156     ds = Dataset(vars, attrs=attrs)
1157     ds = ds.set_coords(coord_names.union(extra_coords).intersection(vars))

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables)
1086             k, v, concat_characters=concat_characters,
1087             mask_and_scale=mask_and_scale, decode_times=decode_times,
-> 1088             stack_char_dim=stack_char_dim)
1089         if decode_coords:
1090             var_attrs = new_vars[k].attrs

/Users/stefano/anaconda/lib/python3.6/site-packages/xarray/conventions.py in decode_cf_variable(name, var, concat_characters, mask_and_scale, decode_times, decode_endianness, stack_char_dim)
998         if (has_fill or scale_factor is not None or add_offset is not None):
999             if has_fill and np.array(fill_value).dtype.kind in ['U', 'S', 'O']:
-> 1000                 if string_encoding is not None:
1001                     raise NotImplementedError(
1002                         'variable %r has a _FillValue specified, but '

UnboundLocalError: local variable 'string_encoding' referenced before assignment

# Fixed code
def __init__(self, array):
    self.array = indexing.as_indexable(array)