Skip to content

Guard against import machinery masking out attributes#171

Open
stefanv wants to merge 2 commits into
scientific-python:mainfrom
stefanv:guard-against-module-overwrite
Open

Guard against import machinery masking out attributes#171
stefanv wants to merge 2 commits into
scientific-python:mainfrom
stefanv:guard-against-module-overwrite

Conversation

@stefanv

@stefanv stefanv commented Jun 3, 2026

Copy link
Copy Markdown
Member

Thank you to @matthew-brett for uncovering and analyzing this issue.

This deals with the case where a function my_func is defined in my_func.py. In some cases where the my_func submodule is imported (e.g. from pkg.my_func import ..., import pkg.my_func, or import * accessing a sibling attribute in the same file) the import machinery updates the package __dict__ with the my_func (the submodule) overriding access to my_func (the function).

This wraps the package __setattr__ to intercept those (and only those) problematic assignments.

@stefanv stefanv added the type: Bug fix Something isn't working label Jun 3, 2026
@codecov

codecov Bot commented Jun 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.92308% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.54%. Comparing base (af0adfc) to head (e788bb1).

Files with missing lines Patch % Lines
src/lazy_loader/__init__.py 76.92% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main     #171      +/-   ##
===========================================
- Coverage   100.00%   97.54%   -2.46%     
===========================================
  Files            1        1              
  Lines          109      122      +13     
  Branches        19       23       +4     
===========================================
+ Hits           109      119      +10     
- Misses           0        1       +1     
- Partials         0        2       +2     

☔ 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.

@effigies effigies left a comment

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.

Well that's a gnarly bug. This fix definitely looks right. One question:

#
# Record affected cases and, only in those cases, swap in the
# guarding module type.
shadowed = {attr for attr, mod in attr_to_modules.items() if attr == mod}

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.

Do we need to worry about submodules? This handles the case of from .x import x, but what about from .w.x import x or something like that?

stefanv referenced this pull request in scikit-image/scikit-image Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: Bug fix Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants