Skip to content

Dynamically handle future scikit-learn metadata routing deprecation#22723

Merged
hertschuh merged 3 commits intokeras-team:masterfrom
andersendsa:sklearn_metadata_routing
Apr 24, 2026
Merged

Dynamically handle future scikit-learn metadata routing deprecation#22723
hertschuh merged 3 commits intokeras-team:masterfrom
andersendsa:sklearn_metadata_routing

Conversation

@andersendsa
Copy link
Copy Markdown
Contributor

Description

This PR resolves a code health TODO by future-proofing the scikit-learn metadata routing check in keras/src/wrappers/fixes.py. It dynamically falls back to checking sklearn.version if the config key is removed, ensuring metadata routing correctly defaults to enabled in future scikit-learn versions while preserving backward compatibility.

Contributor Agreement

Please check all boxes below before submitting your PR for review:

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the _routing_enabled utility to handle missing sklearn installations and introduces version-based defaults for metadata routing, specifically enabling it for scikit-learn 1.3+. The review feedback highlights that the docstring is now outdated regarding default values and suggests a more concise implementation for the configuration and version checks.

Comment on lines 48 to 49
Whether metadata routing is enabled. If the config is not set, it
defaults to False.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The docstring is now inaccurate. It states that metadata routing defaults to False if the config is not set, but the new implementation defaults to True for scikit-learn versions >= 1.3 when the config key is missing. Please update the docstring to reflect this dynamic behavior.

Comment on lines +55 to +66
if "enable_metadata_routing" in config:
return config.get("enable_metadata_routing", False)

try:
from packaging.version import parse as parse_version

if parse_version(sklearn.__version__) >= parse_version("1.3"):
return True
except ImportError:
pass

return False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The implementation can be simplified. Since you've already checked if "enable_metadata_routing" is in the config, using .get(..., False) is redundant. The version check can also be more concise while catching potential parsing errors for robustness.

Suggested change
if "enable_metadata_routing" in config:
return config.get("enable_metadata_routing", False)
try:
from packaging.version import parse as parse_version
if parse_version(sklearn.__version__) >= parse_version("1.3"):
return True
except ImportError:
pass
return False
if "enable_metadata_routing" in config:
return config["enable_metadata_routing"]
try:
from packaging.version import parse
return parse(sklearn.__version__) >= parse("1.3")
except Exception:
return False

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 19, 2026

Codecov Report

❌ Patch coverage is 11.11111% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.04%. Comparing base (75d7bea) to head (5946b33).
⚠️ Report is 48 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/wrappers/fixes.py 11.11% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22723      +/-   ##
==========================================
- Coverage   83.15%   83.04%   -0.12%     
==========================================
  Files         596      596              
  Lines       68858    69889    +1031     
  Branches    10774    10894     +120     
==========================================
+ Hits        57259    58037     +778     
- Misses       8783     9004     +221     
- Partials     2816     2848      +32     
Flag Coverage Δ
keras 82.86% <11.11%> (-0.12%) ⬇️
keras-jax 58.34% <11.11%> (-0.71%) ⬇️
keras-numpy 54.22% <11.11%> (-0.66%) ⬇️
keras-openvino 59.77% <11.11%> (+0.34%) ⬆️
keras-tensorflow 59.89% <11.11%> (-0.72%) ⬇️
keras-torch 58.70% <11.11%> (-0.70%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@andersendsa
Copy link
Copy Markdown
Contributor Author

Hi @hertschuh this pr has passes all the tests and is ready for review

Comment thread keras/src/wrappers/fixes.py Outdated
Comment on lines +61 to +62
if parse_version(sklearn.__version__) >= parse_version("1.3"):
return True
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be on all the time for sklearn >= 1.3 ?

Copy link
Copy Markdown
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this!

@andersendsa
Copy link
Copy Markdown
Contributor Author

Hi @hertschuh i believe that meta data routing wasnt available until version 1.3

@hertschuh
Copy link
Copy Markdown
Collaborator

Hi @hertschuh i believe that meta data routing wasnt available until version 1.3

If that's the case, shouldn't the logic be this?

    if parse_version(sklearn.__version__) < parse_version("1.3"):
        return False

    config = sklearn.get_config()
    if "enable_metadata_routing" in config:
        return config.get("enable_metadata_routing", False)

    return False

@andersendsa
Copy link
Copy Markdown
Contributor Author

Hi @hertschuh i believe that meta data routing wasnt available until version 1.3

If that's the case, shouldn't the logic be this?

    if parse_version(sklearn.__version__) < parse_version("1.3"):
        return False

    config = sklearn.get_config()
    if "enable_metadata_routing" in config:
        return config.get("enable_metadata_routing", False)

    return False

If we use that logic and a future version of scikit-learn (e.g., version 2.0) completely removes the enable_metadata_routing config key, your suggested logic would fall through to the final return False.

@andersendsa
Copy link
Copy Markdown
Contributor Author

Hi @hertschuh i believe that meta data routing wasnt available until version 1.3

If that's the case, shouldn't the logic be this?

    if parse_version(sklearn.__version__) < parse_version("1.3"):
        return False

    config = sklearn.get_config()
    if "enable_metadata_routing" in config:
        return config.get("enable_metadata_routing", False)

    return False

@hertschuh If we use that logic and a future version of scikit-learn (e.g., version 2.0) completely removes the enable_metadata_routing config key, your suggested logic would fall through to the final return False.

Comment thread keras/src/wrappers/fixes.py Outdated
Comment on lines +59 to +60
from packaging.version import parse as parse_version

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move import to the top and remove try / except

packaging is a required dependency of the pypi wheel.

@hertschuh
Copy link
Copy Markdown
Collaborator

Hi @hertschuh i believe that meta data routing wasnt available until version 1.3

If that's the case, shouldn't the logic be this?

    if parse_version(sklearn.__version__) < parse_version("1.3"):
        return False

    config = sklearn.get_config()
    if "enable_metadata_routing" in config:
        return config.get("enable_metadata_routing", False)

    return False

@hertschuh If we use that logic and a future version of scikit-learn (e.g., version 2.0) completely removes the enable_metadata_routing config key, your suggested logic would fall through to the final return False.

Oh, I see. I misunderstood what the deprecation was about.

@andersendsa
Copy link
Copy Markdown
Contributor Author

Hi @hertschuh I have done the changes this pr is ready for review

Copy link
Copy Markdown
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@google-ml-butler google-ml-butler Bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Apr 24, 2026
@hertschuh hertschuh merged commit 9eb6394 into keras-team:master Apr 24, 2026
11 checks passed
@google-ml-butler google-ml-butler Bot removed ready to pull Ready to be merged into the codebase kokoro:force-run labels Apr 24, 2026
@andersendsa andersendsa deleted the sklearn_metadata_routing branch April 25, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants