Skip to content

Support PYTHON_LAZY_IMPORTS=all (Python 3.15) - #1367

Merged
Kludex merged 6 commits into
pydantic:mainfrom
mbachry:lazy-import-all
Nov 30, 2026
Merged

Kludex merged 6 commits into
pydantic:mainfrom
mbachry:lazy-import-all

Conversation

@mbachry

@mbachry mbachry commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

After migrating my Django project to Python 3.15 I opted to run it with PYTHON_LAZY_IMPORTS=all, turning lazy imports globally for all dependencies. Everything worked fine out of the box except httpx, httpx2 and truststore (which is fixed in git master).

Easy way to reproduce:

$ PYTHON_LAZY_IMPORTS=all python -c 'import httpcore2 ; dir(httpcore2)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import httpcore2 ; dir(httpcore2)
ImportError: lazy import of 'httpcore2' raised an exception during resolution

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import httpcore2 ; dir(httpcore2)
                           ^^^^^^^^^
  File "/home/ja/httpx2/src/httpcore2/httpcore2/__init__.py", line 141, in <module>
    setattr(__locals[__name], "__module__", "httpcore2")  # noqa
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'lazy_import' object has no attribute '__module__' and no __dict__ for setting new attributes. Did you mean '.__reduce__' instead of '.__module__'?

The proposed solution here is unglamorous, but 1:1 compatible with the existing httpcore behavior. Alternative options:

  1. do isinstance checks for types.LazyImportType and don't overwrite __module__ if true
  2. remove __module__ overwrites completely, as they are purely for aesthetic purposes, if I understand this code correctly (eg. it provides nicer looking type names in tracebacks)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

View guided diff

@veria-ai

veria-ai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@codspeed

codspeed Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing mbachry:lazy-import-all (8e56f51) with main (cf13c4b)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

Comment thread src/httpcore2/httpcore2/_sync/http2.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/httpcore2/test_api.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 1 file (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/httpcore2/test_api.py Resolved
@Kludex
Kludex merged commit 8b97194 into pydantic:main Nov 30, 2026
17 checks passed
@Kludex Kludex mentioned this pull request Dec 2, 2026
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.

2 participants