ronantakizawa/codereview-bench huggingface dataset:
Repository: pandas-dev/pandas
File: pandas/tests/indexing/test_indexers.py (Python)

Code before:
import pytest

from pandas.core.indexers import (
    is_scalar_indexer,
    length_of_indexer,
    validate_indices,
)


def test_length_of_indexer():
    arr = np.zeros(4, dtype=bool)
    arr[0] = 1
    result = length_of_indexer(arr)
    assert result == 1


@pytest.mark.parametrize(
    "start,stop,step",
    [
        (0, 10**30, 2),
        (10**30, 0, -3),
    ],
)
def test_length_of_indexer_range_overflow(start, stop, step):
    indexer = range(start, stop, step)
    result = length_of_indexer(indexer)
    step_ = step
    if step_ > 0:
        low, high = start, stop
    else:
        low, high = stop, start
        step_ = -step_
    expected = 0 if low >= high else (high - low - 1) // step_ + 1
    assert result == expected


def test_is_scalar_indexer():
    indexer = (0, 1)
    assert is_scalar_indexer(indexer, 2)
    assert not is_scalar_indexer(indexer[0], 2)

    indexer = (np.array([2]), 1)
    assert not is_scalar_indexer(indexer, 2)

    indexer = (np.array([2]), np.array([3]))
    assert not is_scalar_indexer(indexer, 2)

    indexer = (np.array([2]), np.array([3, 4]))
    assert not is_scalar_indexer(indexer, 2)

    assert not is_scalar_indexer(slice(None), 1)

Review comment (suggestion):
Can you add a comment referring to this PR.


```suggestion
    # https://github.com/pandas-dev/pandas/pull/63872
    indexer = range(start, stop, step)
```

Code after:
import pytest

from pandas.core.indexers import (
    is_scalar_indexer,
    length_of_indexer,
    validate_indices,
)


def test_length_of_indexer():
    arr = np.zeros(4, dtype=bool)
    arr[0] = 1
    result = length_of_indexer(arr)
    assert result == 1


@pytest.mark.parametrize(
    "start, stop, step, expected",
    [
        (10, 0, -3, 4),
        (0, 1, 5, 1),
    ],
)
def test_length_of_indexer_range_overflow(start, stop, step, expected):
    # https://github.com/pandas-dev/pandas/pull/63872
    indexer = range(start, stop, step)
    result = length_of_indexer(indexer)
    assert result == expected


def test_is_scalar_indexer():
    indexer = (0, 1)
    assert is_scalar_indexer(indexer, 2)
    assert not is_scalar_indexer(indexer[0], 2)

    indexer = (np.array([2]), 1)
    assert not is_scalar_indexer(indexer, 2)

    indexer = (np.array([2]), np.array([3]))
    assert not is_scalar_indexer(indexer, 2)

    indexer = (np.array([2]), np.array([3, 4]))
    assert not is_scalar_indexer(indexer, 2)

    assert not is_scalar_indexer(slice(None), 1)

    indexer = 0
    assert is_scalar_indexer(indexer, 1)

    indexer = (0,)
    assert is_scalar_indexer(indexer, 1)
Repository: pandas-dev/pandas
File: pandas/tests/series/methods/test_map.py (Python)

Code before:
    dti = date_range("2018-01-01 00:00:00", "2018-01-07 00:00:00")
    ser = Series(dti, dtype="timestamp[ns][pyarrow]", name="a")
    if as_td:
        # duration dtype
        ser = ser - ser[0]

    mapper = {date: i for i, date in enumerate(ser)}

    res_series = ser.map(mapper)
    expected = Series(range(len(ser)), name="a", dtype="int64")
    tm.assert_series_equal(res_series, expected)

    res_index = Index(ser).map(mapper)
    # For now (as of 2025-09-06) at least, we do inference on Index.map that
    #  we don't for Series.map
    expected_index = Index(expected).astype("int64[pyarrow]")
    tm.assert_index_equal(res_index, expected_index)


@pytest.mark.parametrize("dtype", ["Int64", "UInt64"])
def test_map_nullable_integer_precision(dtype):
    large_int = 10000000000000001  # above float64 integer precision limit
    ser = Series([large_int, None], dtype=dtype)

    result = ser.map(lambda x: x + 2 if pd.notna(x) else x)
    expected = Series([large_int + 2, pd.NA], dtype=object)
    tm.assert_series_equal(result, expected)

    # Verify exact value preserved (not rounded due to float conversion)
    assert result.iloc[0] == large_int + 2


Review comment (suggestion):
I would expect the result to be the same dtype as `ser`, only upcasting when necessary. This shouldn't be object dtype.

Code after:

@td.skip_if_no("pyarrow")
@pytest.mark.parametrize("as_td", [True, False])
def test_map_pyarrow_timestamp(as_td):
    # GH#61231
    dti = date_range("2018-01-01 00:00:00", "2018-01-07 00:00:00")
    ser = Series(dti, dtype="timestamp[ns][pyarrow]", name="a")
    if as_td:
        # duration dtype
        ser = ser - ser[0]

    mapper = {date: i for i, date in enumerate(ser)}

    res_series = ser.map(mapper)
    expected = Series(range(len(ser)), name="a", dtype="int64")
    tm.assert_series_equal(res_series, expected)

    res_index = Index(ser).map(mapper)
    # For now (as of 2025-09-06) at least, we do inference on Index.map that
    #  we don't for Series.map
    expected_index = Index(expected).astype("int64[pyarrow]")
    tm.assert_index_equal(res_index, expected_index)


@pytest.mark.parametrize("dtype", ["Int64", "UInt64"])
def test_map_nullable_integer_precision(dtype):
    # GH#63903
    large_int = 10000000000000001  # above float64 integer precision limit
    ser = Series([large_int, None], dtype=dtype)

    result = ser.map(lambda x: x + 2 if pd.notna(x) else x)
    expected = Series([large_int + 2, pd.NA], dtype=dtype)
    tm.assert_series_equal(result, expected)
Repository: pandas-dev/pandas
File: pandas/tests/arrays/test_array.py (Python)

Code before:
                np.array([complex(1), complex(2)], dtype=np.complex128)
            ),
        ),
    ],
)
def test_array(data, dtype, expected):
    result = pd.array(data, dtype=dtype)
    tm.assert_equal(result, expected)


def test_array_copy():
    a = np.array([1, 2])
    # default is to copy
    b = pd.array(a, dtype=a.dtype)
    assert not tm.shares_memory(a, b)

    # copy=True
    b = pd.array(a, dtype=a.dtype, copy=True)
    assert not tm.shares_memory(a, b)

    # copy=False
    b = pd.array(a, dtype=a.dtype, copy=False)
    assert tm.shares_memory(a, b)


# GH 64138
@pytest.mark.parametrize(
    "data",
    [
        # string 2D
        [["a"], ["b"]],
        # int 2D
        [[1], [2]],
        # mixed 2D
        [[1, 2], ["a", "b"]],
        # mixed 3D
        [[[1]], [["a"]], [[3.14]]],
    ],
)
def test_array_string_nd(data):
    result = pd.array(data, dtype="str")
    expected = (
        (
            pd.StringDtype(na_value=np.nan)
            .construct_array_type()
            ._from_sequence(data, dtype=pd.StringDtype(na_value=np.nan))
        )
        if using_string_dtype()
        else NumpyExtensionArray(np.array(data, dtype=str))
    )
    tm.assert_equal(result, expected)

Review comment (refactor):
Can you move this to the first line after `def test...`.

Code after:
                np.array([complex(1), complex(2)], dtype=np.complex128)
            ),
        ),
    ],
)
def test_array(data, dtype, expected):
    result = pd.array(data, dtype=dtype)
    tm.assert_equal(result, expected)


def test_array_copy():
    a = np.array([1, 2])
    # default is to copy
    b = pd.array(a, dtype=a.dtype)
    assert not tm.shares_memory(a, b)

    # copy=True
    b = pd.array(a, dtype=a.dtype, copy=True)
    assert not tm.shares_memory(a, b)

    # copy=False
    b = pd.array(a, dtype=a.dtype, copy=False)
    assert tm.shares_memory(a, b)


@pytest.mark.parametrize(
    "data",
    [
        # string 2D
        [["a"], ["b"]],
        # int 2D
        [[1], [2]],
        # mixed 2D
        [[1, 2], ["a", "b"]],
        # mixed 3D
        [[[1]], [["a"]], [[3.14]]],
    ],
)
def test_array_string_nd(data):
    # GH 64138
    result = pd.array(data, dtype="str")

    if using_string_dtype():
        expected = (
            pd.StringDtype(na_value=np.nan)
            .construct_array_type()
            ._from_sequence(data, dtype=pd.StringDtype(na_value=np.nan))
        )
    else:
        expected = NumpyExtensionArray(np.array(data, dtype=str))
Repository: pandas-dev/pandas
File: pandas/tests/arrays/test_array.py (Python)

Code before:

# GH 64138
@pytest.mark.parametrize(
    "data",
    [
        # string 2D
        [["a"], ["b"]],
        # int 2D
        [[1], [2]],
        # mixed 2D
        [[1, 2], ["a", "b"]],
        # mixed 3D
        [[[1]], [["a"]], [[3.14]]],
    ],
)
def test_array_string_nd(data):
    result = pd.array(data, dtype="str")
    expected = (
        (
            pd.StringDtype(na_value=np.nan)
            .construct_array_type()
            ._from_sequence(data, dtype=pd.StringDtype(na_value=np.nan))
        )
        if using_string_dtype()
        else NumpyExtensionArray(np.array(data, dtype=str))
    )
    tm.assert_equal(result, expected)


@pytest.mark.parametrize(
    "data, expected",
    [
        # period
        (
            [pd.Period("2000", "D"), pd.Period("2001", "D")],
            pd.PeriodIndex(["2000", "2001"], freq="D").array,
        ),
        # interval
        ([pd.Interval(0, 1), pd.Interval(1, 2)], IntervalArray.from_breaks([0, 1, 2])),
        # datetime
        (
            [pd.Timestamp("2000").as_unit("s"), pd.Timestamp("2001").as_unit("s")],
            DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[s]"),
        ),
        (
            [datetime.datetime(2000, 1, 1), datetime.datetime(2001, 1, 1)],
            DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[us]"),
        ),
        (
            np.array([1, 2], dtype="M8[ns]"),
            DatetimeArray._from_sequence(np.array([1, 2], dtype="M8[ns]")),

Review comment (suggestion):
This is just slightly hard to read. Can you make this
```python
if ...:
    expected = ...
else:
    expected = ...
```
instead.

Code after:

@pytest.mark.parametrize(
    "data",
    [
        # string 2D
        [["a"], ["b"]],
        # int 2D
        [[1], [2]],
        # mixed 2D
        [[1, 2], ["a", "b"]],
        # mixed 3D
        [[[1]], [["a"]], [[3.14]]],
    ],
)
def test_array_string_nd(data):
    # GH 64138
    result = pd.array(data, dtype="str")

    if using_string_dtype():
        expected = (
            pd.StringDtype(na_value=np.nan)
            .construct_array_type()
            ._from_sequence(data, dtype=pd.StringDtype(na_value=np.nan))
        )
    else:
        expected = NumpyExtensionArray(np.array(data, dtype=str))

    tm.assert_equal(result, expected)


@pytest.mark.parametrize(
    "data, expected",
    [
        # period
        (
            [pd.Period("2000", "D"), pd.Period("2001", "D")],
            pd.PeriodIndex(["2000", "2001"], freq="D").array,
        ),
        # interval
        ([pd.Interval(0, 1), pd.Interval(1, 2)], IntervalArray.from_breaks([0, 1, 2])),
        # datetime
        (
            [pd.Timestamp("2000").as_unit("s"), pd.Timestamp("2001").as_unit("s")],
            DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[s]"),
        ),
        (
            [datetime.datetime(2000, 1, 1), datetime.datetime(2001, 1, 1)],
            DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[us]"),
        ),
        (
            np.array([1, 2], dtype="M8[ns]"),
Repository: pandas-dev/pandas
File: pandas/_libs/src/parser/pd_parser.c (C)

Code before:
  *p_value =
      precise_xstrtod(item, &p_end, decimal, sci, '\0', 1, &error, maybe_int);

  return (error == 0) && (!*p_end);
}

static int floatify(PyObject *str, double *result, int *maybe_int) {
  char *data;
  PyObject *tmp = NULL;
  const char sci = 'E';
  const char dec = '.';

  if (PyBytes_Check(str)) {
    data = PyBytes_AS_STRING(str);
  } else if (PyUnicode_Check(str)) {
    tmp = PyUnicode_AsUTF8String(str);
    if (tmp == NULL) {
      return -1;
    }
    data = PyBytes_AS_STRING(tmp);
  } else {
    PyErr_SetString(PyExc_TypeError, "Invalid object type");
    return -1;
  }

  const int status = to_double(data, result, sci, dec, maybe_int);

  if (!status) {
    /* handle inf/-inf infinity/-infinity */
    if (strlen(data) == 3) {
      if (0 == strcasecmp(data, "inf")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {
        goto parsingerror;
      }
    } else if (strlen(data) == 4) {
      if (0 == strcasecmp(data, "-inf")) {
        *result = -HUGE_VAL;
        *maybe_int = 0;
      } else if (0 == strcasecmp(data, "+inf")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {
        goto parsingerror;
      }
    } else if (strlen(data) == 8) {
      if (0 == strcasecmp(data, "infinity")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {

Review comment (suggestion):
I think we should just change the function to accept a const char * instead of casting (separate PR is fine)

Code after:
  *p_value =
      precise_xstrtod(item, &p_end, decimal, sci, '\0', 1, &error, maybe_int);

  return (error == 0) && (!*p_end);
}

static int floatify(PyObject *str, double *result, int *maybe_int) {
  const char *data;
  const char sci = 'E';
  const char dec = '.';

  if (PyBytes_Check(str)) {
    data = PyBytes_AS_STRING(str);
  } else if (PyUnicode_Check(str)) {
    // PyUnicode_AsUTF8 returns a pointer to the string's internal UTF-8
    // buffer (caching it on the object), avoiding a heap allocation.
    data = PyUnicode_AsUTF8(str);
    if (data == NULL) {
      return -1;
    }
  } else {
    PyErr_SetString(PyExc_TypeError, "Invalid object type");
    return -1;
  }

  const int status = to_double((char *)data, result, sci, dec, maybe_int);

  if (!status) {
    /* handle inf/-inf infinity/-infinity */
    if (strlen(data) == 3) {
      if (0 == strcasecmp(data, "inf")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {
        goto parsingerror;
      }
    } else if (strlen(data) == 4) {
      if (0 == strcasecmp(data, "-inf")) {
        *result = -HUGE_VAL;
        *maybe_int = 0;
      } else if (0 == strcasecmp(data, "+inf")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {
        goto parsingerror;
      }
    } else if (strlen(data) == 8) {
      if (0 == strcasecmp(data, "infinity")) {
        *result = HUGE_VAL;
        *maybe_int = 0;
      } else {
Repository: parcel-bundler/parcel
File: packages/core/parcel-bundler/src/builtins/hmr-runtime.js (JavaScript)

Code before:
  };

  module.bundle.hotData = null;
}

module.bundle.Module = Module;

var parent = module.bundle.parent;
if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
  var hostname = process.env.HMR_HOSTNAME || location.hostname;
  var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
  var ws = new WebSocket(protocol + '://' + hostname + ':' + process.env.HMR_PORT + '/');
  ws.onmessage = function(event) {
    var data = JSON.parse(event.data);

    if (data.type === 'update') {
      console.clear();

      data.assets.forEach(function (asset) {
        hmrApply(global.parcelRequire, asset);
      });

      var handled = false;
      data.assets.forEach(function (asset) {
        if (!asset.isNew) {
          handled = handled || hmrAccept(global.parcelRequire, asset.id);
        }
      });

      if(!handled) {
        window.location.reload();
      }
    }

    if (data.type === 'reload') {
      ws.close();
      ws.onclose = function () {
        location.reload();
      }
    }

    if (data.type === 'error-resolved') {
      console.log('[parcel] ✨ Error resolved');

      removeErrorOverlay();
    }

    if (data.type === 'error') {
      console.error('[parcel] 🚨  ' + data.error.message + '\n' + data.error.stack);

      removeErrorOverlay();

Review comment (suggestion):
I think this will cause updates to stop after the first asset accepts. Then handled will be true and hmrAccept won’t get called on other assets. I think we need to ensure that hmrAccept is still called for all assets, and just keep track of handled separately.

Code after:
  };

  module.bundle.hotData = null;
}

module.bundle.Module = Module;
var checkedAssets, assetsToAccept;

var parent = module.bundle.parent;
if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
  var hostname = process.env.HMR_HOSTNAME || location.hostname;
  var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
  var ws = new WebSocket(protocol + '://' + hostname + ':' + process.env.HMR_PORT + '/');
  ws.onmessage = function(event) {
    checkedAssets = {};
    assetsToAccept = [];

    var data = JSON.parse(event.data);

    if (data.type === 'update') {
      var handled = false;
      data.assets.forEach(function(asset) {
        if (!asset.isNew) {
          var didAccept = hmrAcceptCheck(global.parcelRequire, asset.id);
          if (didAccept) {
            handled = true;
          }
        }
      });

      if(handled){
        console.clear();

        data.assets.forEach(function (asset) {
          hmrApply(global.parcelRequire, asset);
        });

        assetsToAccept.forEach(function (v) {
          hmrAcceptRun(v[0], v[1]);
        });
      } else {
        window.location.reload();
      }
    }

    if (data.type === 'reload') {
      ws.close();
      ws.onclose = function () {
        location.reload();
      }
    }
Repository: parcel-bundler/parcel
File: packages/packagers/html/src/HTMLPackager.js (JavaScript)

Code before:
          bundle =>
            !bundle
              .getEntryAssets()
              .some(asset => asset.id === bundleGroup.entryAssetId)
        );
      return p.concat(bundles);
    }, []);

    let {html} = await posthtml([
      insertBundleReferences.bind(this, bundles),
      replaceInlineAssetContent.bind(
        this,
        getAssetContent.bind(this, bundleGraph, () => {})
      )
    ]).process(code);

    return {contents: html};
  }
});

async function getAssetContent(bundleGraph, getBundleResult, assetId) {
  let inlineBundle: ?Bundle;
  bundleGraph.traverseBundles((bundle, context, {stop}) => {
    if (bundle.id.includes(assetId)) {
      inlineBundle = bundle;
      stop();
    }
  });

  if (inlineBundle) {
    let asset = inlineBundle.getMainEntry();

    return asset.getCode();
  }

  return `unable to find bundle for id - ${assetId}`;
}

async function replaceInlineAssetContent(getAssetContent, tree) {
  const inlineNodes = [];
  tree.walk(node => {
    if (node.attrs && node.attrs['data-parcelId']) {
      inlineNodes.push(node);
    }
    return node;
  });

  for (let node of inlineNodes) {
    node.attrs['is-inline'] = true;
    node.attrs['new-content'] = await getAssetContent(
      node.attrs['data-parcelId']

Review comment (suggestion):
I would appreciate any recommendations on a better way to determine find the bundle based on the `assetId`. 

Code after:
        .getBundlesInBundleGroup(bundleGroup)
        .filter(
          bundle =>
            !bundle
              .getEntryAssets()
              .some(asset => asset.id === bundleGroup.entryAssetId)
        );
      return p.concat(bundles);
    }, []);

    let {html} = await posthtml([
      insertBundleReferences.bind(this, bundles),
      replaceInlineAssetContent.bind(this, bundleGraph, getInlineBundleContents)
    ]).process(code);

    return {contents: html};
  }
});

async function getAssetContent(
  bundleGraph: BundleGraph,
  getInlineBundleContents,
  assetId
): Promise<?Blob> {
  let inlineBundle: ?Bundle;
  bundleGraph.traverseBundles((bundle, context, {stop}) => {
    let mainAsset = bundle.getMainEntry();
    if (mainAsset && mainAsset.uniqueKey === assetId) {
      inlineBundle = bundle;
      stop();
    }
  });

  if (inlineBundle) {
    const bundleResult = await getInlineBundleContents(
      inlineBundle,
      bundleGraph
    );

    return bundleResult.contents;
  }

  return null;
}

async function replaceInlineAssetContent(
  bundleGraph: BundleGraph,
  getInlineBundleContents,
  tree
) {
  const inlineNodes = [];
Repository: parcel-bundler/parcel
File: packages/core/core/src/PackagerRunner.js (JavaScript)

Code before:
    }

    return {
      time: Date.now() - start,
      size
    };
  }

  async package(
    internalBundle: InternalBundle,
    bundleGraph: InternalBundleGraph
  ): Promise<BundleResult> {
    let bundle = new NamedBundle(internalBundle, bundleGraph, this.options);
    report({
      type: 'buildProgress',
      phase: 'packaging',
      bundle
    });

    let packager = await this.config.getPackager(bundle.filePath);
    let packaged = await packager.package({
      bundle,
      bundleGraph: new BundleGraph(bundleGraph, this.options),
      sourceMapPath: path.basename(bundle.filePath) + '.map',
      options: this.pluginOptions,
      getBundleResult: (bundle: BundleType, bundleGraph: BundleGraphType) => {
        return this.getBundleResult(
          bundleToInternalBundle(bundle),
          bundleGraphToInternalBundleGraph(bundleGraph)
        );
      }
    });

    return {
      contents:
        typeof packaged.contents === 'string'
          ? replaceReferences(
              packaged.contents,
              generateDepToBundlePath(internalBundle, bundleGraph)
            )
          : packaged.contents,
      map: packaged.map
    };
  }

  async optimize(
    internalBundle: InternalBundle,
    bundleGraph: InternalBundleGraph,
    contents: Blob,
    map?: ?SourceMap
  ): Promise<BundleResult> {

Review comment (performance):
Not sure about this name. I guess `packageBundle` isn't clear that it also includes optimizing... `packageAndOptimizeBundle(bundle)`? `getInlineBundleContents`?

I also think we should only allow inline bundles here, and should throw an error if not. So maybe a name specific to that would be good?

Code after:
    }

    return {
      time: Date.now() - start,
      size
    };
  }

  async package(
    internalBundle: InternalBundle,
    bundleGraph: InternalBundleGraph
  ): Promise<BundleResult> {
    let bundle = new NamedBundle(internalBundle, bundleGraph, this.options);
    report({
      type: 'buildProgress',
      phase: 'packaging',
      bundle
    });

    let packager = await this.config.getPackager(bundle.filePath);
    let packaged = await packager.package({
      bundle,
      bundleGraph: new BundleGraph(bundleGraph, this.options),
      sourceMapPath: path.basename(bundle.filePath) + '.map',
      options: this.pluginOptions,
      getInlineBundleContents: (
        bundle: BundleType,
        bundleGraph: BundleGraphType
      ) => {
        if (!bundle.isInline) {
          throw new Error(
            'Bundle is not inline and unable to retireve contents'
          );
        }

        return this.getBundleResult(
          bundleToInternalBundle(bundle),
          bundleGraphToInternalBundleGraph(bundleGraph)
        );
      }
    });

    return {
      contents:
        typeof packaged.contents === 'string'
          ? replaceReferences(
              packaged.contents,
              generateDepToBundlePath(internalBundle, bundleGraph)
            )
          : packaged.contents,
      map: packaged.map