Skip to content

fix: resolve 10 SonarQube issues in metadata_routing example#634

Merged
isuruperera merged 1 commit into
mainfrom
remediate-main-20260520-222956-8932b5f2
May 20, 2026
Merged

fix: resolve 10 SonarQube issues in metadata_routing example#634
isuruperera merged 1 commit into
mainfrom
remediate-main-20260520-222956-8932b5f2

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This change fixes 10 SonarQube violations in the metadata routing example by renaming variables to match naming conventions, modernizing NumPy random API usage, documenting unused required parameters, and fixing a private attribute naming issue. These improvements enhance code quality and ensure compliance with project standards.

View Project in SonarCloud


Fixed Issues

python:S117 - Rename this local variable "X_transformed" to match the regular expression ^[_a-z][a-z0-9_]*$. • MINORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:501

Why is this an issue?

A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
Local variables and function parameters hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable pattern.
Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.

What changed

Renames the local variable X_transformed to x_transformed in the fit method of SimplePipeline, making it comply with the snake_case naming convention (matching the regex ^[_a-z][a-z0-9_]*$). This fixes the naming convention violation at line 501.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -501,1 +502,1 @@ class SimplePipeline(ClassifierMixin, BaseEstimator):
-        X_transformed = self.transformer_.transform(
+        x_transformed = self.transformer_.transform(
python:S117 - Rename this local variable "X_transformed" to match the regular expression ^[_a-z][a-z0-9_]*$. • MINORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:513

Why is this an issue?

A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
Local variables and function parameters hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable pattern.
Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.

What changed

Renames the local variable X_transformed to x_transformed in the predict method of SimplePipeline, making it comply with the snake_case naming convention (matching the regex ^[_a-z][a-z0-9_]*$). This fixes the naming convention violation at line 513.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -513,1 +514,1 @@ class SimplePipeline(ClassifierMixin, BaseEstimator):
-        X_transformed = self.transformer_.transform(
+        x_transformed = self.transformer_.transform(
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:59

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(42) with the modern np.random.default_rng(42), and updates all subsequent calls from legacy methods (rand, randint) to their numpy.random.Generator equivalents (random, integers). This directly fixes the warning about using legacy numpy.random.RandomState instead of numpy.random.Generator.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -59,6 +59,6 @@ n_samples, n_features = 100, 4
-rng = np.random.RandomState(42)
-X = rng.rand(n_samples, n_features)
-y = rng.randint(0, 2, size=n_samples)
-my_groups = rng.randint(0, 10, size=n_samples)
-my_weights = rng.rand(n_samples)
-my_other_weights = rng.rand(n_samples)
+rng = np.random.default_rng(42)
+X = rng.random((n_samples, n_features))
+y = rng.integers(0, 2, size=n_samples)
+my_groups = rng.integers(0, 10, size=n_samples)
+my_weights = rng.random(n_samples)
+my_other_weights = rng.random(n_samples)
python:S1172 - Remove the unused function parameter "y". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:99

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a comment documenting that the X and y parameters are required by the scikit-learn API but not used in this example classifier's fit method. This addresses the unused function parameter warnings for X and y in the ExampleClassifier.fit method, since the rule exempts parameters that are referenced in a comment.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -99,0 +100,1 @@ class ExampleClassifier(ClassifierMixin, BaseEstimator):
+        # X and y are required by the scikit-learn API but not used in this example.
python:S1172 - Remove the unused function parameter "X". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:99

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a comment documenting that the X and y parameters are required by the scikit-learn API but not used in this example classifier's fit method. This addresses the unused function parameter warnings for X and y in the ExampleClassifier.fit method, since the rule exempts parameters that are referenced in a comment.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -99,0 +100,1 @@ class ExampleClassifier(ClassifierMixin, BaseEstimator):
+        # X and y are required by the scikit-learn API but not used in this example.
python:S1172 - Remove the unused function parameter "X". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:541

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a docstring to the ExampleTransformer.fit method that references the X, y, and sample_weight parameters. This addresses the unused function parameter warnings for X and y in that method, since the rule exempts parameters that are referenced in a docstring.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -541,0 +543,1 @@ class ExampleTransformer(TransformerMixin, BaseEstimator):
+        """Fit the transformer on X and y with optional sample_weight."""
python:S1172 - Remove the unused function parameter "y". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:541

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a docstring to the ExampleTransformer.fit method that references the X, y, and sample_weight parameters. This addresses the unused function parameter warnings for X and y in that method, since the rule exempts parameters that are referenced in a docstring.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -541,0 +543,1 @@ class ExampleTransformer(TransformerMixin, BaseEstimator):
+        """Fit the transformer on X and y with optional sample_weight."""
python:S4487 - Remove this unread private attribute '__metadata_request__fit' or refactor the code to use its value. • CRITICALView issue

Location: examples/miscellaneous/plot_metadata_routing.py:626

Why is this an issue?

Python has no real private attribute. Every attribute is accessible. There are however two conventions indicating that an attribute is not meant to be "public":

What changed

Renames the class attribute from __metadata_request__fit (double-underscore prefix, making it a class-private/name-mangled attribute) to _metadata_request__fit (single-underscore prefix). This fixes the warning about an unread private attribute, since the attribute with double underscores was being name-mangled and thus appeared unread within the class. With a single underscore, it is no longer subject to the class-private attribute rule (S4487).

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -626,1 +628,1 @@ class WeightedMetaRegressor(MetaEstimatorMixin, RegressorMixin, BaseEstimator):
-    __metadata_request__fit = {"sample_weight": metadata_routing.WARN}
+    _metadata_request__fit = {"sample_weight": metadata_routing.WARN}
python:S1172 - Remove the unused function parameter "X". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:672

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a docstring to the ExampleRegressor.fit method that references X, y, and sample_weight. This addresses the unused function parameter warnings for X and y in that method, since the rule exempts parameters that are referenced in a docstring.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -672,0 +675,1 @@ class ExampleRegressor(RegressorMixin, BaseEstimator):
+        """Fit the regressor using X, y and sample_weight as metadata."""
python:S1172 - Remove the unused function parameter "y". • MAJORView issue

Location: examples/miscellaneous/plot_metadata_routing.py:672

Why is this an issue?

A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.

What changed

Adds a docstring to the ExampleRegressor.fit method that references X, y, and sample_weight. This addresses the unused function parameter warnings for X and y in that method, since the rule exempts parameters that are referenced in a docstring.

--- a/examples/miscellaneous/plot_metadata_routing.py
+++ b/examples/miscellaneous/plot_metadata_routing.py
@@ -672,0 +675,1 @@ class ExampleRegressor(RegressorMixin, BaseEstimator):
+        """Fit the regressor using X, y and sample_weight as metadata."""

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZ45C0jTRXnEWm2Rf6J- for python:S117 rule
- AZ45C0jTRXnEWm2Rf6J7 for python:S6711 rule
- AZ45C0jTRXnEWm2Rf6J_ for python:S117 rule
- AZ45C0jTRXnEWm2Rf6J9 for python:S1172 rule
- AZ45C0jTRXnEWm2Rf6J8 for python:S1172 rule
- AZ45C0jTRXnEWm2Rf6KE for python:S1172 rule
- AZ45C0jTRXnEWm2Rf6KF for python:S1172 rule
- AZ45C0jTRXnEWm2Rf6KC for python:S4487 rule
- AZ45C0jTRXnEWm2Rf6KA for python:S1172 rule
- AZ45C0jTRXnEWm2Rf6KB for python:S1172 rule

Generated by SonarQube Agent (task: c66f3302-ea9e-45a7-a5e2-80cdc9908b81)
@isuruperera isuruperera merged commit 4cc3c5a into main May 20, 2026
9 checks passed
@isuruperera isuruperera deleted the remediate-main-20260520-222956-8932b5f2 branch May 20, 2026 22:36
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants