Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntField: add MinMaxValidator #1868

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

markus-96
Copy link
Contributor

Description

Before sending anything to the DB, IntField values are checked beforehand now. As far as I could test, the added overhead is not much, lets see what Codspeed says.

Will add documentation later.

Motivation and Context

Using sqlite, you could send values greater than defined in IntField.constraints.get('le') to the DB and nothing was raised. If you for example want to migrate to another DBMS, you have a problem.

Also, have a look at the Issue I raised: #1853

How Has This Been Tested?

Existent tests and new tests

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

subclassing IntField and setting GE and/or LE will directly be reflected in the MinMaxValidator and the constraints property
MyISAM does not support transactions
Copy link

codspeed-hq bot commented Jan 30, 2025

CodSpeed Performance Report

Merging #1868 will not alter performance

Comparing markus-96:intfield-validator (8f317cf) with develop (960b1c1)

Summary

✅ 12 untouched benchmarks

@coveralls
Copy link

coveralls commented Jan 30, 2025

Pull Request Test Coverage Report for Build 13053921297

Details

  • 22 of 28 (78.57%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.09%) to 89.146%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/fields/data.py 20 26 76.92%
Totals Coverage Status
Change from base Build 13012319268: -0.09%
Covered Lines: 6500
Relevant Lines: 7105

💛 - Coveralls

maybe entering the loop in validate(value) causes those performance issues...
tortoise/fields/data.py Show resolved Hide resolved
def __init__(
self,
primary_key: Optional[bool] = None,
ge: Optional[int] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Extending the field configuration with ge and le introduces the second way of setting up validation. I think we should have a single way of doing it to keep things simple - through validators. If some one needs specific validation, they can add a validator. So I suggest to remove these options from the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ge and le are currently baked in in the constraints property. Instead of that I wanted them to be customizable, a bit like with max_char in CharField where this is reflected during validation and also by the constraints property.

Copy link
Contributor

Choose a reason for hiding this comment

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

ge and le are currently baked in in the constraints property. Instead of that I wanted them to be customizable, a bit like with max_char

The int field constraints are related to the nature of the data type. I think it is different from business logic validation and we already have a way of doing it. We shouldn't have two ways of doing the same thing.

tortoise/fields/base.py Show resolved Hide resolved
@waketzheng
Copy link
Contributor

@markus-96 I push another solution: #1872

@markus-96
Copy link
Contributor Author

@markus-96 I push another solution: #1872

Sorry if I ask, but where is this different from mine? I put much effort into optimizing my solution, could you tell what is wrong about that? I think we all have an interest in tortoise-orm being the fastest orm for python, so something that crucial like an int field has to be as fast as possible as it can be, so I think hard-coding default validators is a completly valid solution.

def to_db_value(self, value: Any, instance: "Union[Type[Model], Model]") -> Any:
if value is not None:
if not isinstance(value, self.field_type):
value = self.field_type(value) # pylint: disable=E1102
Copy link
Contributor

Choose a reason for hiding this comment

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

In which cases is it required?

@henadzit
Copy link
Contributor

henadzit commented Feb 5, 2025

I think we should be able to merge it if we remove the ability to customize constraints.

@waketzheng
Copy link
Contributor

Sorry if I ask, but where is this different from mine? I put much effort into optimizing my solution, could you tell what is wrong about that? I think we all have an interest in tortoise-orm being the fastest orm for python, so something that crucial like an int field has to be as fast as possible as it can be, so I think hard-coding default validators is a completly valid solution.

@markus-96 Your issue #1853 request a feature to check constraints for int fields before saving to db. There are several ways to do that. We can compare the two PRs and choose one or merge both. Code speed is important, and compatibility also counts. I use Django for a long time before migrating to tortoise-orm, and do not want it to be too much different from Django.

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.

4 participants