Skip to content

feat add overloads for field types to improve type safety - #1491

Merged
collerek merged 8 commits into
ormar-orm:masterfrom
MaximSrour:update-stub-types-to-improve-type-safety
Jan 28, 2026
Merged

feat add overloads for field types to improve type safety#1491
collerek merged 8 commits into
ormar-orm:masterfrom
MaximSrour:update-stub-types-to-improve-type-safety

Conversation

@MaximSrour

Copy link
Copy Markdown
Contributor

This PR is intended to provide appropriate PyRight type overloads so that models provide the correct typing.

This will resolve errors related to this issue.

Sample model I made to validate this:
all_types_model.py

Sample of the errors highlighted in the IDE:
sample_of_errors

Output after running a migration:
migration_without_new_stubs.py

Sample of the errors being gone with the new stubs:
sample_without_errors

Output after running a new migration with the stubs present:
migration_with_new_stubs.py

Diff between the two migrations (only difference being the revision hash):

6c6
< revision = "cb05bff645c7"
---
> revision = "dce364e4199c"

All this goes to show that the stub types do not impact the generation of the migration, only affecting the types used by the PyRight, preventing the need to add many #type: ignore or otherwise being forced to deal with red squigglies across the models.

@codecov

codecov Bot commented Jan 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (02e6d49) to head (f71d08a).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1491   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          207       207           
  Lines        14872     14878    +6     
=========================================
+ Hits         14872     14878    +6     
Files with missing lines Coverage Δ
ormar/fields/model_fields.py 100.00% <100.00%> (ø)
tests/test_encryption/test_encrypted_columns.py 100.00% <100.00%> (ø)
...exclude_include_dict/test_dumping_model_to_dict.py 100.00% <100.00%> (ø)
...test_exclude_include_dict/test_excludable_items.py 100.00% <100.00%> (ø)
...e_include_dict/test_excluding_fields_in_fastapi.py 100.00% <100.00%> (ø)
...include_dict/test_excluding_fields_with_default.py 100.00% <100.00%> (ø)
...e_include_dict/test_excluding_subset_of_columns.py 100.00% <100.00%> (ø)
..._exclude_include_dict/test_pydantic_dict_params.py 100.00% <100.00%> (ø)
tests/test_fastapi/test_json_field_fastapi.py 100.00% <100.00%> (ø)
tests/test_fastapi/test_recursion_error.py 100.00% <100.00%> (ø)
... and 31 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented Jan 22, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 21.68%

⚡ 6 improved benchmarks
❌ 7 (👁 7) regressed benchmarks
✅ 71 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_avg[500] 3.5 ms 2.9 ms +24.17%
👁 test_count[1000] 5.1 ms 6.1 ms -17.36%
👁 test_saving_models_individually[10] 12.6 ms 14.3 ms -11.98%
👁 test_get_one[1000] 3.7 ms 4.8 ms -21.68%
👁 test_get_or_create_when_get[1000] 3.9 ms 4.8 ms -19.29%
👁 test_max[1000] 3.5 ms 4.4 ms -20.03%
test_sum[250] 3.9 ms 2.9 ms +31.03%
test_making_and_inserting_models_in_bulk[10] 5.3 ms 4.2 ms +27.5%
test_sum[500] 3.2 ms 2.8 ms +12.19%
test_sum[1000] 4.8 ms 4.1 ms +17.98%
test_deleting_all[1000] 5.1 ms 3.5 ms +47.25%
👁 test_exists[1000] 4.2 ms 5.1 ms -17.97%
👁 test_first[1000] 4.1 ms 4.7 ms -11.58%

Comparing MaximSrour:update-stub-types-to-improve-type-safety (f71d08a) with master (02e6d49)

Open in CodSpeed

@MaximSrour

Copy link
Copy Markdown
Contributor Author

Hi @collerek, I'd be interested in having this PR merged in soon as this issue has been affecting me since 2024 - what do we need to do to make sure that this suitable for merging?

@collerek collerek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the pr, one small comment

Comment thread ormar/fields/model_fields.pyi Outdated
@collerek
collerek merged commit 0d164ba into ormar-orm:master Jan 28, 2026
15 checks passed
@collerek

Copy link
Copy Markdown
Collaborator

Awesome, thanks for the contribution <3

@MaximSrour
MaximSrour deleted the update-stub-types-to-improve-type-safety branch January 29, 2026 02:39
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