Skip to content

Fix remaining ty type errors in baker.py and generators.py#589

Open
benaduo wants to merge 1 commit intomodel-bakers:mainfrom
benaduo:fix/ty-type-errors
Open

Fix remaining ty type errors in baker.py and generators.py#589
benaduo wants to merge 1 commit intomodel-bakers:mainfrom
benaduo:fix/ty-type-errors

Conversation

@benaduo
Copy link
Contributor

@benaduo benaduo commented Mar 19, 2026

Summary

Fixes two remaining ty type checker errors not addressed by PR #584.

Changes

model_bakery/baker.py

  • Added explicit list[type] annotation to other_fields_to_skip to fix invalid-argument-type when .extend([GenericRelation, GenericForeignKey]) is called
  • Wrapped the .extend() call with cast(list[type], [...]) so ty is satisfied with the conditionally-imported types

model_bakery/generators.py

  • Added explicit dict[type, Callable] annotation to default_mapping, replacing the trailing # type: dict[type, Callable] comment that ty does not recognise

Motivation

The CI linter job linters (ty check model_bakery) still fails on these two issues after #584. This PR resolves them completely.

Summary by CodeRabbit

  • Refactor
    • Improved and standardized type annotations across core modules to enhance code clarity and type safety without changing behavior.
  • Chores
    • Minor formatting and declaration cleanups to align code style and annotations.

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7ad80b95-fda3-40b6-b8d0-6e78875e3e66

📥 Commits

Reviewing files that changed from the base of the PR and between 1a178a9 and 6a3932d.

📒 Files selected for processing (2)
  • model_bakery/baker.py
  • model_bakery/generators.py
✅ Files skipped from review due to trivial changes (2)
  • model_bakery/baker.py
  • model_bakery/generators.py

📝 Walkthrough

Walkthrough

Small, non-functional edits adding explicit type annotations: other_fields_to_skip declared as list[type] in model_bakery/baker.py (uses cast(list[type], ...) when extending with contenttypes types), and default_mapping annotated as dict[type, Callable] in model_bakery/generators.py. No logic changes.

Changes

Cohort / File(s) Summary
Type annotation updates
model_bakery/baker.py, model_bakery/generators.py
Added explicit annotations: other_fields_to_skip: list[type] in baker.py and default_mapping: dict[type, Callable] in generators.py. Wrapped contenttypes list with cast(list[type], ...) before extend. No behavioral changes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • PR #572: Similar typing/annotation adjustments in baker.py and related modules.

Poem

🐇 I hopped through code, made types clear and neat,
Casts tucked in places where generics greet,
Mappings labeled, no more hidden lore,
Tiny edits, tidy core,
A rabbit's whisper: "types, complete!"

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing remaining type checker (ty) errors in two specific files (baker.py and generators.py).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@benaduo benaduo force-pushed the fix/ty-type-errors branch from fe66e1e to 1a178a9 Compare March 19, 2026 16:32
- cast list passed to other_fields_to_skip.extend() to list[type]
  to resolve invalid-argument-type error; ty infers GenericRelation
  as type | None due to the conditional import
- cast callable values to Callable[[], list[Any]] before calling
  to resolve call-top-callable error; ty does not narrow Top[...]
  types via callable() guards

Both cast() calls use imports already present in the file.
@benaduo benaduo force-pushed the fix/ty-type-errors branch from 1a178a9 to 6a3932d Compare March 19, 2026 20:39
Copy link
Member

@amureki amureki left a comment

Choose a reason for hiding this comment

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

Thanks for picking up the fixes here! 🙏

Comment on lines +626 to +628
other_fields_to_skip.extend(
cast(list[type], [GenericRelation, GenericForeignKey])
)
Copy link
Member

Choose a reason for hiding this comment

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

Honestly, a bit weird to cast here, I'd be also fine with just excluding it # type: ignore[list-item], but okay.

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