Skip to content

Fix crash in type_with_config for parametrized generics when pydantic is installed - #1194

Open
sophiecarreras wants to merge 2 commits into
Backblaze:masterfrom
sophiecarreras:fix-type-with-config-generic-alias
Open

sophiecarreras wants to merge 2 commits into
Backblaze:masterfrom
sophiecarreras:fix-type-with-config-generic-alias

Conversation

@sophiecarreras

Copy link
Copy Markdown
Contributor

list[X] is a types.GenericAlias and cannot take a __pydantic_config__ attribute, so every JSON option typed list[...] (e.g. --lifecycle-rules) raised AttributeError: 'types.GenericAlias' object has no attribute '__pydantic_config__' when pydantic (the full extra) is installed. type_with_config now catches the AttributeError and uses the type unchanged; pydantic applies its defaults in that case.

No upstream issue exists for this; its text is below instead.

Reproduce on v5.0.2 / master 9ea898a (Python 3.12, pydantic 2.12.5):

pip install -e . pydantic==2.12.5 pytest ...
pytest test/unit/test_console_tool.py -k lifecycle_rules

Fails: test_create_bucket__with_lifecycle_rules, test_create_bucket__mutually_exclusive_lifecycle_rules_options. The default nox -s unit does not install the full extra, so CI does not see it.

Verification (Python 3.12, pydantic 2.12.5):

  • Before the fix the two lifecycle-rule tests and two new tests in test/unit/_cli/test_obj_loads.py fail; after it all pass.
  • The rest of test/unit gives the same results as before. 8 autocomplete/shell-install tests fail identically with and without the change in my environment.
  • ruff check and ruff format --check pass. The new pydantic-specific test is skipped when pydantic is not installed, like the existing one next to it.

Includes changelog fragment changelog.d/+pydantic-generic-alias.fixed.md.

🤖 Generated with Claude Code

sophiecarreras and others added 2 commits October 30, 2026 19:08
… is installed

`list[X]` is a `types.GenericAlias` and cannot take a `__pydantic_config__`
attribute, so every JSON option typed `list[...]` (e.g. `--lifecycle-rules`)
raised AttributeError when pydantic is installed. The type is now used
unchanged in that case.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sophiecarreras

Copy link
Copy Markdown
Contributor Author

Code review (independent pass).

Important (fixed in the follow-up commit)

  • obj_loads.py: the first version of the fix dropped extra='allow' for list[X], so --lifecycle-rules silently discarded unknown keys while --lifecycle-rule kept them. The config is now passed to TypeAdapter(..., config=...) when the type cannot carry it as an attribute.
  • test_obj_loads.py: the generic test did not cover extra keys. It now includes one and expects it preserved.
  • Changelog fragment named --lifecycle-rule; the affected option is --lifecycle-rules (create and update). Corrected.

Nit (not changed)

  • The new pydantic tests are skipped in the default nox -s unit (no full extra), so CI does not exercise them. Running a unit session with the full extra would close this gap.
  • The legacy __config__ branch in type_with_config is untested and untouched by this PR.
  • Commit subject is longer than 72 characters.

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.

1 participant