Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pythonfinder for improved efficiency and PEP 514 support #6360

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

matteius
Copy link
Member

@matteius matteius commented Mar 20, 2025

Refactor pythonfinder for improved efficiency and PEP 514 support

Summary

This PR completely refactors the pythonfinder module to improve efficiency, reduce logical errors, and fix support for PEP 514 (Python registration in the Windows registry). The refactoring replaces the complex object hierarchy with a more modular, composition-based approach that is easier to maintain and extend.

Motivation

The original pythonfinder implementation had several issues:

  • Complex object wrapping with paths as objects, leading to excessive recursion
  • Tight coupling between classes making the code difficult to follow and maintain
  • Broken Windows registry support (PEP 514)
  • Performance issues due to redundant path scanning and inefficient caching

Changes

  • Architecture: Replaced inheritance-heavy design with a composition-based approach using specialized finders
  • Data Model: Simplified the data model with a clean PythonInfo dataclass
  • Windows Support: Implemented proper PEP 514 support for Windows registry
  • Performance: Improved caching and reduced redundant operations
  • Error Handling: Added more specific exceptions and better error handling

Features

The refactored implementation continues to support all required features:

  • System and user PATH searches
  • pyenv installations
  • asdf installations
  • Windows registry (PEP 514) - now working correctly

Implementation Details

The new implementation is organized into three main components:

  1. Finders: Specialized classes for finding Python in different locations

    • SystemFinder: Searches in the system PATH
    • PyenvFinder: Searches in pyenv installations
    • AsdfFinder: Searches in asdf installations
    • WindowsRegistryFinder: Implements PEP 514 for Windows registry
  2. Models: Simple data classes for storing Python information

    • PythonInfo: Stores information about a Python installation
  3. Utils: Utility functions for path and version handling

    • path_utils.py: Path-related utility functions
    • version_utils.py: Version-related utility functions

Testing

Basic functionality has been tested with a test script that successfully finds Python installations and retrieves their version information. More comprehensive testing, especially on Windows, is recommended before merging.

Future Work

  • Add comprehensive unit tests for each component
  • Update documentation to reflect the new API
  • Consider adding support for additional Python installation methods

Related Issues

Fixes #XXX (PEP 514 support)
Addresses #YYY (Performance issues)

Breaking Changes

This is a complete refactoring of the pythonfinder module, so it includes breaking changes to the internal API. However, the public API (Finder class) maintains compatibility with the original implementation.

The checklist

Fixes #5893
Fixes #6147
Fixes #6026
Fixes #5662 (possibly)

  • Associated issue
  • A news fragment in the news/ directory to describe this fix with the extension .bugfix.rst, .feature.rst, .behavior.rst, .doc.rst. .vendor.rst. or .trivial.rst (this will appear in the release changelog). Use semantic line breaks and name the file after the issue number or the PR #.

@matteius matteius force-pushed the python-finder-rewrite branch from 42da4d1 to 2468175 Compare March 20, 2025 20:36
@matteius matteius marked this pull request as ready for review March 20, 2025 21:47
@matteius
Copy link
Member Author

My plan is to port over to pythonfinder and release as version 3.0.0

@matteius matteius changed the title Rewriting python finder to eventually fix a number of issue reports. Rewriting python finder to fix a number of issue reports. Mar 20, 2025
@matteius matteius changed the title Rewriting python finder to fix a number of issue reports. Refactor pythonfinder for improved efficiency and PEP 514 support Mar 20, 2025
@matteius matteius mentioned this pull request Mar 21, 2025
2 tasks
@matteius matteius force-pushed the python-finder-rewrite branch from e886363 to 22a9496 Compare April 5, 2025 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment