Skip to content

Create a safer abstraction for TypeAdapter validation#1762

Open
Copilot wants to merge 25 commits intomainfrom
copilot/create-safer-abstraction-for-typeadapter-validatio
Open

Create a safer abstraction for TypeAdapter validation#1762
Copilot wants to merge 25 commits intomainfrom
copilot/create-safer-abstraction-for-typeadapter-validatio

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

  • Create safer TypeAdapter validation wrappers (validate_or_raise, validate_or_default)
  • Add validated_get and ensure_get convenience methods on KeyValueFileManager
  • Replace all direct TypeAdapter.validate_python usages with safe wrappers
  • Ban direct pydantic.TypeAdapter import via TID251
  • Add @overload to validate_or_raise for better type narrowing
  • Refactor validate_or_raise to accept err arg (instantiated error)
  • Resolve merge conflicts with main
  • Fix docstring double backticks (check-docstring-substrings hook)
  • Fix inaccurate comments in toml/yaml io_.py ("not present" → "missing or invalid")
  • Restore get_project_dict() call chain and match main's error propagation
  • Create ticket for warn_print on invalid config (io: Report warning via warn_print when silently ignoring invalid config in toml/yaml remove_from_list #1841)
  • Add warn_msg parameter to validate_or_default — emits warn_print on validation failure
  • Add warn_msg parameter to validated_get — passes through to validate_or_default
  • Convert deps.py to use validated_get with warn_msg (warns on invalid type instead of raising)
  • Clean up dead PyprojectTOMLDepsError / DepGroupError catch blocks in _deps.py
  • Remove unused PyprojectTOMLDepsError class
  • Update tests to verify warnings instead of errors for invalid dep sections
  • Re-enable TID251 — use inline # noqa: TID251 comments instead of per-file ignores
  • Move TestGetValidated and TestEnsureGet to test_manager.py under TestKeyValueFileManager
  • Remove docstrings from test classes
  • Pass all static checks (prek, basedpyright)
  • Create lesson issues for test placement (Lesson: Tests for a class method should live in the test file for that class #1895) and docstring conventions (Lesson: Test classes should not have docstrings #1896)
  • Inject pydantic ValidationError details into err message in validate_or_raise
  • Inject pydantic ValidationError details into warn_msg in validate_or_default
  • Remove unused InvalidSonarQubeProjectKeyError subclass (no longer raised); restore SonarQubeError base class
  • Add warn_print for invalid values in yaml/io_.py and toml/io_.py getter methods

Skills used: usethis-qa-static-checks, usethis-github-issue-create

Copilot AI and others added 4 commits April 2, 2026 01:48
…r_default wrappers

Migrate all direct TypeAdapter usage in business logic files to the new
validate_or_raise and validate_or_default wrappers from usethis._validate.

Updated files:
- _file/pyproject_toml/name.py, project.py, deps.py, requires_python.py
- _core/status.py
- _tool/impl/base/ruff.py, deptry.py
- _integrations/sonarqube/config.py
- _backend/uv/deps.py

Also added TID251 per-file-ignores for the I/O infrastructure files
(ini/io_.py, toml/io_.py, yaml/io_.py) which use TypeAdapter extensively
in their core dict-traversal logic.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
…n file I/O managers

Replace all direct pydantic TypeAdapter/ValidationError usage in toml/io_.py,
yaml/io_.py, and ini/io_.py with the validate_or_raise wrapper from
usethis._validate.

- toml/io_.py: Replace TypeAdapter calls, catch TOMLValueInvalidError
- yaml/io_.py: Replace TypeAdapter calls, catch UnexpectedYAMLValueError
- ini/io_.py: Replace TypeAdapter calls with validate_or_raise
- ruff.toml: Remove TID251 per-file-ignores for the three files

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI changed the title [WIP] Create a safer abstraction for TypeAdapter validation Create safer abstraction for TypeAdapter validation Apr 2, 2026
Copilot AI requested a review from nathanjmcdougall April 2, 2026 02:17
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 2, 2026 21:01
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 98.29545% with 3 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/usethis/_file/yaml/io_.py 89.65% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 2, 2026

Merging this PR will not alter performance

✅ 3 untouched benchmarks


Comparing copilot/create-safer-abstraction-for-typeadapter-validatio (6f13a89) with main (fe3b8a6)

Open in CodSpeed

Comment thread src/usethis/_tool/impl/base/ruff.py Outdated
…to KeyValueFileManager

Per review feedback:
- Move src/usethis/_validate.py -> src/usethis/_file/validate.py
- Add get_validated(keys, *, default, validate=None) method
- Add ensure_get(keys, *, err, validate) method
- Replace direct validate_or_default usage in tool/core/backend files
  with the new file manager methods
- Update .importlinter, tests, and generated docs

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/ae5b883a-a133-47ce-a8c8-86903ff6c49e

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Comment thread src/usethis/_backend/uv/deps.py Outdated
Comment thread src/usethis/_file/manager.py Outdated
Comment thread src/usethis/_file/ini/io_.py
Copilot AI and others added 2 commits April 3, 2026 20:18
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1564655d-dd00-4582-9f0f-e08225210e9e

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
… ensure_get/validated_get in pyproject_toml and sonarqube modules

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1564655d-dd00-4582-9f0f-e08225210e9e

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
@nathanjmcdougall
Copy link
Copy Markdown
Collaborator

@copilot please pass static checks

…-substrings)

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/07093d4e-1e91-49fb-9686-b3e141b83d29

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 7, 2026

@copilot please pass static checks

Fixed in 9bda459 — the check-docstring-substrings hook was catching a double backtick in hooks/export-functions.py:147. Changed @overload to `@overload`. All prek hooks and basedpyright now pass cleanly.

Comment thread ruff.toml Outdated
Comment thread tests/usethis/_file/test_validate.py Outdated
…move test class docstrings

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/690b9dc0-c97a-46d2-b952-6bf84cfe9131

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Comment thread src/usethis/_file/validate.py Outdated
Comment thread src/usethis/_file/validate.py Outdated
Comment thread src/usethis/_integrations/sonarqube/config.py
Comment thread src/usethis/_file/yaml/io_.py Outdated
Copilot AI and others added 2 commits April 9, 2026 20:45
…t for invalid configs, remove unused SonarQubeError base class

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/c1bb2774-9802-4976-9c81-851b72ce5735

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
…s still actively raised

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/2d2991c3-9149-42d5-962f-11a9b2a29d3a

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants