ronantakizawa/codereview-bench huggingface dataset:
Repository: onnx/onnx
File: .github/workflows/manylinux/entrypoint.sh (Shell)

Code before:
#!/bin/bash

# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

set -e -x

# CLI arguments
PY_VERSION=$1
PLAT=$2
BUILD_MODE=$3  # New argument for build type (release or preview)

echo "Build mode: $BUILD_MODE"


export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

# Compile wheels
# Need to be updated if there is a new Python Version
if [ "$(uname -m)" == "aarch64" ]; then
 PIP_INSTALL_COMMAND="$PY_VERSION -m pip install --only-binary google-re2 --no-cache-dir -q"
 PYTHON_COMMAND="$PY_VERSION"
else
 declare -A python_map=(["3.9"]="cp39-cp39" ["3.10"]="cp310-cp310" ["3.11"]="cp311-cp311" ["3.12"]="cp312-cp312" ["3.13"]="cp313-cp313")
 PY_VER=${python_map[$PY_VERSION]}
 PIP_INSTALL_COMMAND="/opt/python/${PY_VER}/bin/pip install --only-binary google-re2 --no-cache-dir -q"
 PYTHON_COMMAND="/opt/python/${PY_VER}/bin/python"
fi

# Update pip
$PIP_INSTALL_COMMAND --upgrade pip
$PIP_INSTALL_COMMAND cmake

# Build protobuf from source
yum install -y wget
source workflow_scripts/protobuf/build_protobuf_unix.sh "$(nproc)" "$(pwd)"/protobuf/protobuf_install

Review comment (suggestion):
```suggestion
BUILD_MODE=$3  # build type (release or preview)
```

Code after:
#!/bin/bash

# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

set -e -x

# CLI arguments
PY_VERSION=$1
PLAT=$2
BUILD_MODE=$3  # build mode (release or preview)

echo "Build mode: $BUILD_MODE"


export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

# Compile wheels
# Need to be updated if there is a new Python Version
if [ "$(uname -m)" == "aarch64" ]; then
 PIP_INSTALL_COMMAND="$PY_VERSION -m pip install --only-binary google-re2 --no-cache-dir -q"
 PYTHON_COMMAND="$PY_VERSION"
else
 declare -A python_map=(["3.9"]="cp39-cp39" ["3.10"]="cp310-cp310" ["3.11"]="cp311-cp311" ["3.12"]="cp312-cp312" ["3.13"]="cp313-cp313")
 PY_VER=${python_map[$PY_VERSION]}
 PIP_INSTALL_COMMAND="/opt/python/${PY_VER}/bin/pip install --only-binary google-re2 --no-cache-dir -q"
 PYTHON_COMMAND="/opt/python/${PY_VER}/bin/python"
fi

# Update pip
$PIP_INSTALL_COMMAND --upgrade pip
$PIP_INSTALL_COMMAND cmake

# Build protobuf from source
yum install -y wget
source workflow_scripts/protobuf/build_protobuf_unix.sh "$(nproc)" "$(pwd)"/protobuf/protobuf_install
Repository: onnx/onnx
File: onnx/defs/__init__.py (Python)

Code before:
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
    type_constraints={self.type_constraints!r},

Review comment (suggestion):
```suggestion
def _non_deterministic(self) -> bool:
```

Code after:
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore[misc]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic  # type: ignore[no-any-return]


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
    type_constraints={self.type_constraints!r},
Repository: onnx/onnx
File: onnx/defs/__init__.py (Python)

Code before:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


def _non_deterministic(self) -> bool:
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
    type_constraints={self.type_constraints!r},

Review comment (suggestion):
```suggestion
# type: ignore[no-any-return]
def _non_deterministic(self):
```

Code after:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore[misc]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic  # type: ignore[no-any-return]


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
Repository: onnx/onnx
File: onnx/defs/__init__.py (Python)

Code before:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


# type: ignore[no-any-return]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},

Review comment (suggestion):
```suggestion
@property  # type: ignore[no-any-return]
```

Code after:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore[misc]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic  # type: ignore[no-any-return]


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
Repository: onnx/onnx
File: onnx/defs/__init__.py (Python)

Code before:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore[no-any-return]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},

Review comment (suggestion):
```suggestion
@property  # type: ignore[no-any-return,misc]
```

Code after:
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema


def onnx_opset_version() -> int:
    """Return current opset for domain `ai.onnx`."""
    return C.schema_version_map()[ONNX_DOMAIN][1]


def onnx_ml_opset_version() -> int:
    """Return current opset for domain `ai.onnx.ml`."""
    return C.schema_version_map()[ONNX_ML_DOMAIN][1]


@property  # type: ignore
def _function_proto(self):
    func_proto = onnx.FunctionProto()
    func_proto.ParseFromString(self._function_body)
    return func_proto


OpSchema = C.OpSchema
OpSchema.function_body = _function_proto  # type: ignore


@property  # type: ignore[misc]
def _non_deterministic(self):
    """Check if the operator is non-deterministic."""
    return self.node_determinism != OpSchema.NodeDeterminism.Deterministic  # type: ignore[no-any-return]


OpSchema.non_deterministic = _non_deterministic  # type: ignore


@property  # type: ignore
def _attribute_default_value(self):
    attr = onnx.AttributeProto()
    attr.ParseFromString(self._default_value)
    return attr


OpSchema.Attribute.default_value = _attribute_default_value  # type: ignore


def _op_schema_repr(self) -> str:
    return f"""\
OpSchema(
    name={self.name!r},
    domain={self.domain!r},
    since_version={self.since_version!r},
    doc={self.doc!r},
Repository: onnx/onnx
File: onnx/reference/ops/op_constant_of_shape.py (Python)

Code before:
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

from onnx.reference.op_run import OpRun


class ConstantOfShape(OpRun):
    def _run(self, data, value: np.array | None = None):
        if self.value is None:
            value = np.array(0, dtype=np.float32)
        else:
            value = self.value.flat[0]

        try:
            res = np.full(tuple(data), value)
        except TypeError as e:
            raise RuntimeError(
                f"Unable to create a constant of shape {data!r} with value {self.cst!r} "
                f"(raw value={value!r})."
            ) from e
        return (res,)


Review comment (refactor):
If this is that tricky, maybe it is worth making a utility that is called uniformly from all ops where we need to handle a scalar value?

Code after:
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

from onnx.reference.op_run import OpRun


class ConstantOfShape(OpRun):
    def _run(self, data, value: np.array | None = None):
        if self.value is None:
            value = np.array(0, dtype=np.float32)
        else:
            if self.value.size != 1:
                raise ValueError(
                    f"Operator ConstantOfShape ({self.onnx_node.name!r}) expects a single element tensor as value, but the size of 'value' is {len(self.value)}."
                )
            value = self.value.flat[0]

        try:
            res = np.full(tuple(data), value)
        except TypeError as e:
            raise RuntimeError(
                f"Unable to create a constant of shape {data!r} with value {value!r} "
                f"(raw value={self.value!r})."
            ) from e
        return (res,)
Repository: onnx/onnx
File: onnx/backend/test/case/node/topk.py (Python)

Code before:
        # print(indices_ref)
        # [[0 1 2]
        # [0 1 2]
        # [2 3 0]]

        expect(
            node,
            inputs=[X, K],
            outputs=[values_ref, indices_ref],
            name="test_top_k_same_values_2d",
        )

    @staticmethod
    def export_top_k_smallest() -> None:
        axis = 1
        largest = 0
        _sorted = 1
        k = 3

        node = onnx.helper.make_node(
            "TopK",
            inputs=["x", "k"],
            outputs=["values", "indices"],
            axis=axis,
            largest=largest,
            sorted=_sorted,
        )

        X = np.array(
            [
                [0, 1, 2, 3],
                [4, 5, 6, 7],
                [11, 10, 9, 8],
            ],
            dtype=np.float32,
        )
        K = np.array([k], dtype=np.int64)
        values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)

        # print(values_ref)
        # [[ 0.  1.  2.]
        # [ 4.  5.  6.]
        # [ 8.  9. 10.]]
        # print(indices_ref)
        # [[0 1 2]
        # [0 1 2]
        # [3 2 1]]

        expect(
            node,
            inputs=[X, K],

Review comment (suggestion):
Since conventionally ruff treats the prefixed names as unused names, I suggest using a suffix, here and elsewhere 


```suggestion
            sorted=sorted_,
```

Code after:
        # print(indices_ref)
        # [[0 1 2]
        # [0 1 2]
        # [2 3 0]]

        expect(
            node,
            inputs=[X, K],
            outputs=[values_ref, indices_ref],
            name="test_top_k_same_values_2d",
        )

    @staticmethod
    def export_top_k_smallest() -> None:
        axis = 1
        largest = 0
        sorted_ = 1
        k = 3

        node = onnx.helper.make_node(
            "TopK",
            inputs=["x", "k"],
            outputs=["values", "indices"],
            axis=axis,
            largest=largest,
            sorted=sorted_,
        )

        X = np.array(
            [
                [0, 1, 2, 3],
                [4, 5, 6, 7],
                [11, 10, 9, 8],
            ],
            dtype=np.float32,
        )
        K = np.array([k], dtype=np.int64)
        values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)

        # print(values_ref)
        # [[ 0.  1.  2.]
        # [ 4.  5.  6.]
        # [ 8.  9. 10.]]
        # print(indices_ref)
        # [[0 1 2]
        # [0 1 2]
        # [3 2 1]]

        expect(
            node,
            inputs=[X, K],
Repository: open-telemetry/opentelemetry-java
File: api/src/main/java/io/opentelemetry/trace/BlankSpan.java (Java)

Code before:

/**
 * The {@code BlankSpan} is a singleton class, which is the default {@link Span} that is used when
 * no {@code Span} implementation is available. All operations are no-op.
 *
 * <p>Used also to stop tracing, see {@link Tracer#withSpan}.
 *
 * @since 0.1.0
 */
@Immutable
public final class BlankSpan implements Span {
  /**
   * Singleton instance of this class.
   *
   * @since 0.1.0
   */
  public static final Span INSTANCE = new BlankSpan();

  private BlankSpan() {}

  @Override
  public void setAttribute(String key, String value) {
    Utils.checkNotNull(key, "key");
    Utils.checkNotNull(value, "value");
  }

  @Override
  public void setAttribute(String key, long value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
  public void setAttribute(String key, double value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
  public void setAttribute(String key, boolean value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
  public void setAttribute(String key, AttributeValue value) {
    Utils.checkNotNull(key, "key");
    Utils.checkNotNull(value, "value");
  }

  @Override
  public void addEvent(String name) {}

  @Override

Review comment (suggestion):
@bogdandrutu you had a comment on this in my previous PR https://github.com/open-telemetry/opentelemetry-java/pull/258#discussion_r282696358.

This class is exclusively used by `NoopTrace` https://pastebin.com/wTzBX4ve, therefore it makes sense to make it hold the context when necessary. In the cases when there is no context we can use the same instance.

Code after:

/**
 * The {@code BlankSpan} is the default {@link Span} that is used when no {@code Span}
 * implementation is available. All operations are no-op except context propagation.
 *
 * <p>When no valid context ({@code null} or {@link SpanContext#BLANK}) is available then
 * implementation propagates {@link BlankSpan#INSTANCE} which encapsulates {@link
 * SpanContext#BLANK}.
 *
 * <p>Used also to stop tracing, see {@link Tracer#withSpan}.
 *
 * @since 0.1.0
 */
@Immutable
public final class BlankSpan implements Span {
  /**
   * An instance of this class. If there is no {@code SpanContext} or {@link SpanContext#BLANK} to
   * propagate this instance is used.
   *
   * @since 0.1.0
   */
  public static final Span INSTANCE = new BlankSpan(SpanContext.BLANK);

  private final SpanContext spanContext;

  BlankSpan(SpanContext spanContext) {
    this.spanContext = spanContext;
  }

  @Override
  public void setAttribute(String key, String value) {
    Utils.checkNotNull(key, "key");
    Utils.checkNotNull(value, "value");
  }

  @Override
  public void setAttribute(String key, long value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
  public void setAttribute(String key, double value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
  public void setAttribute(String key, boolean value) {
    Utils.checkNotNull(key, "key");
  }

  @Override
Repository: open-telemetry/opentelemetry-java
File: sdk/src/main/java/io/opentelemetry/sdk/trace/SpanProcessor.java (Java)

Code before:
   * <p>This method is called synchronously on the execution thread, should not throw or block the
   * execution thread.
   *
   * @param span the {@code ReadableSpan} that just started.
   */
  void onStart(ReadableSpan span);

  /**
   * Called when a {@link io.opentelemetry.trace.Span} is ended, if the {@link Span#isRecording()}
   * returns true.
   *
   * <p>This method is called synchronously on the execution thread, should not throw or block the
   * execution thread.
   *
   * @param span the {@code ReadableSpan} that just ended.
   */
  // TODO: Consider checking whether the given span is processed with onStart().
  void onEnd(ReadableSpan span);

  /** Called when {@link TracerSdkRegistry#shutdown()} is called. */
  void shutdown();

  /**
   * Exports all ended spans that have not yet been exported.
   *
   * <p>This method is called synchronously on the execution thread, should not throw or block the
   * execution thread.
   */
  void forceFlush();
}


Review comment (suggestion):
```suggestion
   * <p>This method is called synchronously on the execution thread, and should not throw exceptions 
             or block the execution thread.
```

Code after:
   * <p>This method is called synchronously on the execution thread, should not throw or block the
   * execution thread.
   *
   * @param span the {@code ReadableSpan} that just started.
   */
  void onStart(ReadableSpan span);

  /**
   * Called when a {@link io.opentelemetry.trace.Span} is ended, if the {@link Span#isRecording()}
   * returns true.
   *
   * <p>This method is called synchronously on the execution thread, should not throw or block the
   * execution thread.
   *
   * @param span the {@code ReadableSpan} that just ended.
   */
  // TODO: Consider checking whether the given span is processed with onStart().
  void onEnd(ReadableSpan span);

  /** Called when {@link TracerSdkRegistry#shutdown()} is called. */
  void shutdown();

  /**
   * Exports all ended spans that have not yet been exported.
   *
   * <p>This method is called synchronously on the execution thread, and should not throw
   * exceptions.
   */
  void forceFlush();
}