Repository navigation
Python 3.14 a6/a7 Compatibility Issue with httpx 0.28.1 and typing.Union #3548
Copy link
Copy link
Closed as duplicate
Closed as duplicate
Copy link
Description
When using httpx 0.28.1 with Python 3.14.0 (alpha 6 and alpha 7), an AttributeError occurs:
7112109dc9 failed: 'typing.Union' object has no attribute '__module__' and no __dict__ for setting new attributes
divine-core-1 | Traceback (most recent call last):
divine-core-1 | File "/app/divine/divine/networking/core.py", line 71, in request
divine-core-1 | async with httpx.AsyncClient(http2=True, verify=TLS_CONTEXT_HTTP2) as session:
divine-core-1 | ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_client.py", line 1402, in __init__
divine-core-1 | self._transport = self._init_transport(
divine-core-1 | ~~~~~~~~~~~~~~~~~~~~^
divine-core-1 | verify=verify,
divine-core-1 | ^^^^^^^^^^^^^^
divine-core-1 | ...<5 lines>...
divine-core-1 | transport=transport,
divine-core-1 | ^^^^^^^^^^^^^^^^^^^^
divine-core-1 | )
divine-core-1 | ^
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_client.py", line 1445, in _init_transport
divine-core-1 | return AsyncHTTPTransport(
divine-core-1 | verify=verify,
divine-core-1 | ...<4 lines>...
divine-core-1 | limits=limits,
divine-core-1 | )
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_transports/default.py", line 294, in __init__
divine-core-1 | import httpcore
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpcore/__init__.py", line 140, in <module>
divine-core-1 | setattr(__locals[__name], "__module__", "httpcore") # noqa
divine-core-1 | ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
divine-core-1 | AttributeError: 'typing.Union' object has no attribute '__module__' and no __dict__ for setting new attributes. Did you mean: '__reduce__'?
Issue Analysis
The problem occurs in the httpcore package (a dependency of httpx) in its init.py file (around line 140). The code attempts to set the module attribute on all exported names:
7112109dc9 failed: 'typing.Union' object has no attribute '__module__' and no __dict__ for setting new attributes
divine-core-1 | Traceback (most recent call last):
divine-core-1 | File "/app/divine/divine/networking/core.py", line 71, in request
divine-core-1 | async with httpx.AsyncClient(http2=True, verify=TLS_CONTEXT_HTTP2) as session:
divine-core-1 | ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_client.py", line 1402, in __init__
divine-core-1 | self._transport = self._init_transport(
divine-core-1 | ~~~~~~~~~~~~~~~~~~~~^
divine-core-1 | verify=verify,
divine-core-1 | ^^^^^^^^^^^^^^
divine-core-1 | ...<5 lines>...
divine-core-1 | transport=transport,
divine-core-1 | ^^^^^^^^^^^^^^^^^^^^
divine-core-1 | )
divine-core-1 | ^
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_client.py", line 1445, in _init_transport
divine-core-1 | return AsyncHTTPTransport(
divine-core-1 | verify=verify,
divine-core-1 | ...<4 lines>...
divine-core-1 | limits=limits,
divine-core-1 | )
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpx/_transports/default.py", line 294, in __init__
divine-core-1 | import httpcore
divine-core-1 | File "/usr/local/lib/python3.14t/site-packages/httpcore/__init__.py", line 140, in <module>
divine-core-1 | setattr(__locals[__name], "__module__", "httpcore") # noqa
divine-core-1 | ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
divine-core-1 | AttributeError: 'typing.Union' object has no attribute '__module__' and no __dict__ for setting new attributes. Did you mean: '__reduce__'?
In Python 3.14, typing.Union objects can be included in the module namespace, but they don't have a module attribute and don't support setting attributes (no dict).
Environment
- Python 3.14.0 alpha 6 and alpha 7
- httpx 0.28.1
- httpcore 1.0.7
Steps to Reproduce
- Install Python 3.14.0 (alpha 6 or 7)
- Install httpx 0.28.1
- Try to use httpx.AsyncClient with http2=True
Suggested Fix
The httpcore module should check if the object supports attribute setting before attempting to set the module attribute. Something like:
__locals = locals()
for __name in __all__:
if not __name.startswith("__"):
setattr(__locals[__name], "__module__", "httpcore") # noqaThis should be a relatively simple fix that maintains backward compatibility with older Python versions while supporting Python 3.14.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
codebydivine commentedon Apr 10, 2025
Hi team,
Following up on the Python 3.14 compatibility issue (
AttributeError: 'typing.Union' object has no attribute '__module__') originating fromhttpcore/__init__.pywhen used withhttpx, we've developed a temporary workaround for our codebase until an upstream fix is available.The issue occurs because
httpcoreattempts to set__module__on objects from its__all__list, which fails for types liketyping.Unionon Python 3.14+.Our workaround uses a
sys.meta_pathimport hook to patchhttpcoreduring the import process. It allows the initial import error to occur but then manually applies the problematicsetattrloop with proper error handling.Workaround Implementation:
Create the patch file (
divine/networking/patch_httpcore.py):Import the patch VERY early: In the main application entry point (e.g.,
cli.pyfor us), add these lines at the absolute top, before any other imports:zanieb commentedon Apr 11, 2025
Thanks for the report.
This is a duplicate of encode/httpcore#995
Update UPnP client library, and dependencies for recent Python versio…