Skip to content

Commit 3bf5d4b

Browse files
lesnik512claude
andcommitted
fix(pydantic): unconditional TypeAdapter import — eliminate NameError window
_get_adapter and the TypeError fallback inside PydanticDecoder.decode both referenced TypeAdapter as a bare name. The name was bound only under `if import_checker.is_pydantic_installed:`. If a test reloaded the module with the flag patched to False, TypeAdapter was undefined and subsequent calls raised NameError instead of the documented ImportError. Hoist `from pydantic import TypeAdapter` unconditionally. The module is gated upstream by client.py:_default_pydantic_decoder(), which raises ImportError before this module is loaded when pydantic is absent — there is no real-world path that loads pydantic.py without pydantic available. Update the module docstring to reflect the new contract. Closes audit Low finding (pydantic.py:15-16, 27, 43) from planning/audit/2026-06-07-deep-audit.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e879eb8 commit 3bf5d4b

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/httpware/decoders/pydantic.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
"""PydanticDecoder — module-level cached TypeAdapter adapter for ResponseDecoder.
22
3-
Requires the `pydantic` extra: `pip install httpware[pydantic]`. Importing this
4-
module without the extra works (the `pydantic` import is guarded by a
5-
`find_spec` check), but instantiating the decoder raises `ImportError` with the
3+
Requires the `pydantic` extra: `pip install httpware[pydantic]`. The optional-extras
4+
gate is enforced upstream — `client.py:_default_pydantic_decoder()` raises
5+
ImportError when pydantic is absent, so this module is never imported in that
6+
path. Tests simulating "pydantic not installed" patch
7+
`import_checker.is_pydantic_installed=False` at runtime, after this module is
8+
already loaded; `PydanticDecoder.__init__` then raises ImportError with the
69
install hint.
710
"""
811

912
import functools
1013
from typing import TypeVar
1114

12-
from httpware._internal import import_checker
13-
15+
from pydantic import TypeAdapter
1416

15-
if import_checker.is_pydantic_installed:
16-
from pydantic import TypeAdapter
17+
from httpware._internal import import_checker
1718

1819

1920
MISSING_DEPENDENCY_MESSAGE = (

0 commit comments

Comments
 (0)