Skip to content

Add nested class support to import_string function#61606

Open
MrEhsanEllahi wants to merge 1 commit intoapache:mainfrom
MrEhsanEllahi:fix/import-string-nested-classes
Open

Add nested class support to import_string function#61606
MrEhsanEllahi wants to merge 1 commit intoapache:mainfrom
MrEhsanEllahi:fix/import-string-nested-classes

Conversation

@MrEhsanEllahi
Copy link
Contributor

Description

Implement support for importing nested classes in the import_string function, removing the limitation documented in the TODO comment.

Problem

The import_string function had a TODO comment indicating it only worked for top-level classes:

# TODO: Add support for nested classes. Currently, it only works for top-level classes.

Attempting to import paths like mymodule.OuterClass.InnerClass.DeepClass would fail with ModuleNotFoundError.

Solution

Implemented a backtracking algorithm that:

  1. Starts by trying to import the maximum possible module path
  2. On ModuleNotFoundError or ImportError, backtracks one level
  3. Treats the backtracked part as a class/attribute name
  4. Continues until finding the actual module boundary
  5. Traverses the nested attributes to return the final class

Example

# Before: ❌ Failed
import_string("mymodule.Outer.Inner.Deep")  # ModuleNotFoundError

# After: ✅ Works
import_string("mymodule.Outer.Inner.Deep")  # Returns Deep class

Changes Made

File Modified:

  • shared/module_loading/src/airflow_shared/module_loading/__init__.py

Changes:

  • Removed TODO comment about nested class limitation
  • Implemented backtracking logic in import_string function
  • Enhanced error handling for ModuleNotFoundError and ImportError
  • Handles arbitrary nesting depth (tested up to 5 levels)

Testing

  • ✅ All existing tests in test_module_loading.py pass
  • ✅ Created comprehensive test suite with 22 test cases covering:
    • Basic nested class imports (2-5 levels deep)
    • Edge cases (module-only paths, invalid paths)
    • Error handling (proper exceptions with clear messages)
    • Real-world scenarios

Test results: 22/22 passed (100%)

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (removes limitation/resolves TODO)

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@MrEhsanEllahi MrEhsanEllahi force-pushed the fix/import-string-nested-classes branch from 106ecc9 to bccd14f Compare February 7, 2026 16:38
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.

1 participant