Skip to content

Conversation

matteius
Copy link
Contributor

@matteius matteius commented Mar 21, 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

  • 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 #158

@matteius matteius requested a review from oz123 March 21, 2025 02:42
@matteius matteius force-pushed the python-finder-rewrite branch from f72f08b to 156d432 Compare March 21, 2025 02:49
@matteius matteius force-pushed the python-finder-rewrite branch from 156d432 to d18a7a5 Compare March 21, 2025 02:50
@matteius matteius requested a review from oz123 April 8, 2025 20:35
@oz123 oz123 merged commit 64e7fa9 into master Apr 8, 2025
18 checks passed
Copy link
Contributor

@oz123 oz123 left a comment

Choose a reason for hiding this comment

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

Merged.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PEP-514 support was removed in version 2.0 but is still documented

2 participants