Skip to content

Support nested classes in import_string - #71016

Closed
KushagraB424 wants to merge 2 commits into
apache:mainfrom
KushagraB424:fix-import-string-nested-classes
Closed

Support nested classes in import_string#71016
KushagraB424 wants to merge 2 commits into
apache:mainfrom
KushagraB424:fix-import-string-nested-classes

Conversation

@KushagraB424

Copy link
Copy Markdown
Contributor

This PR adds support for importing nested classes using the import_string utility, resolving an existing TODO.

Currently, import_string strictly splits at the last dot and attempts to import the first part as a module. This immediately throws an ImportError if the dotted path points to a nested class inside another class (e.g., my_module.TopLevelClass.NestedClass).

The updated logic introduces a safe fallback mechanism. If the original split throws an ImportError, it iteratively searches backwards to find the longest valid base module path. Once the module is successfully imported, it recursively resolves the rest of the path using getattr().

This introduces no breaking changes to the default behavior. Unit tests have been added to verify that nested classes load properly.

closes: #71014


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

Generated-by: [Antigravity] following the guidelines


  • 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, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

This adds a fallback mechanism to iteratively resolve nested class boundaries when the standard module import fails, fully addressing an existing TODO. This ensures nested classes can be dynamically loaded without modifying existing top-level behavior.
@kaxil

kaxil commented Aug 3, 2026

Copy link
Copy Markdown
Member

We can remove the TODO, I don't think we need to support that use-case unless we need it

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.

Support for nested classes in import_string

2 participants