Skip to content

Don't require __file__ when installing the lazy module - #2744

Open
Cherith1222 wants to merge 2 commits into
celery:mainfrom
Cherith1222:fix/1121-optional-dunder-file
Open

Cherith1222 wants to merge 2 commits into
celery:mainfrom
Cherith1222:fix/1121-optional-dunder-file

Conversation

@Cherith1222

Copy link
Copy Markdown

Fixes #1121.

Importing kombu raised NameError: name '__file__' is not defined when the importer did not define __file__. Issue #1121 shows that failure while celery imports kombu, at the point where the package module is replaced. __path__ is optional in the same way.

kombu/__init__.py copied both names unconditionally. They are copied only when the name is present in the module globals. An empty string and an explicit None are still present, so they are still copied. A normal import still has __file__ and __path__. The temporary copy dict stays on the replaced module and is not a public attribute of kombu. Lazy attributes such as Connection still load.

t/unit/test_module.py covers both names missing, only __path__, only __file__, both present, an empty string, explicit None, and that loading this file leaves the real module object in place.

No changelog entry was added. The section at the top of the changelog is the already released 5.7.0b1.

On this commit (335cc60605d3a652d69cddf8ebcba70aeea8a05e), Windows, Python 3.13: pytest t/unit/test_module.py was 8 passed, and the same run with t/unit/test_log.py was 28 passed. flake8 7.3.0, mypy 2.3.1, and pydocstyle 6.3.0 reported no issues on the changed module. Integration tests, tox, and a real PyOxidizer build were not run.

AI-assisted (Cursor).

Cherith reviewed this diff and confirmed it with the person who helped draft it.

Frozen importers omit __file__ and __path__. Reading them while replacing the package module raised NameError.

AI-assisted (Cursor).
@auvipy
auvipy self-requested a review October 10, 2026 18:38
@auvipy auvipy added this to the 5.7.0 milestone Oct 10, 2026
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.12%. Comparing base (4a8f695) to head (7081c49).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2744   +/-   ##
=======================================
  Coverage   85.11%   85.12%           
=======================================
  Files          80       80           
  Lines       11357    11362    +5     
  Branches     1362     1364    +2     
=======================================
+ Hits         9667     9672    +5     
  Misses       1470     1470           
  Partials      220      220           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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.

Avoid use of __file__

2 participants