Skip to content

Conversation

@samialfattani
Copy link
Contributor

run-time error is raised if Select2Field did'nt include coerce=int in form_args

Here is how to replicate the error

  class Model2(db.Model):  
      def __init__(self,int_field=None):
          self.int_field = int_field

      int_field = Column(Integer)

  class MyModelView(ModelView):
      form_columns = ["int_field"]
      form_choices = {"int_field": [("101", "101"), ("150", "150")]}
      form_args = {
          "int_field": {"validators": [NumberRange(min=100, max=199)]},
      }

This PR adds a default coerce function if it is not provided explicitly based on the Column's type

the added tests won't pass without changes in this PR

@ElLorans
Copy link
Contributor

ElLorans commented Feb 7, 2026

Why do you need to change typing? Do we need to change all other orm libraries?

@samialfattani
Copy link
Contributor Author

Why do you need to change typing? Do we need to change all other orm libraries?

what do you mean? i'm not changing the typing. This PR just extract the right coerce function that match the Column's type.



class T_FIELD_ARGS_VALIDATORS_SELECTABLE(T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK):
coerce: NotRequired[t.Callable[[t.Any], t.Any]]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can simply add coerce to T_FIELD_ARGS_VALIDATORS and remove T_FIELD_ARGS_VALIDATORS_SELECTABLE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can simply add coerce to T_FIELD_ARGS_VALIDATORS and remove T_FIELD_ARGS_VALIDATORS_SELECTABLE

I've tried your suggestion but unfortunately it all fields that don't include coerce attr. will be affected and raise typing issues. I believe T_FIELD_ARGS_VALIDATORS_SELECTABLE is more clear and dedicated to SELECT fields

@ElLorans
Copy link
Contributor

ElLorans commented Feb 8, 2026

Sorry, let me be more specific. Why not editing T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK

@samialfattani
Copy link
Contributor Author

Sorry, let me be more specific. Why not editing T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK

the T_FIELD_ARGS_VALIDATORS_ALLOW_BLANK is being used in other 2 functions _get_description and _convert_relation and i don't want those funcitons to be harmed if just edit it. but any i can have a try if you say so.

return value if value else None


def coerce_factory(type_: TypeEngine[t.Any]) -> t.Callable[[t.Any], t.Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this PR only make it work for sqlalchemy? Or other libraries do not have this issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is tested only in Sqlalchemy, however, i've tried to add a Peewee test but it seems like there is no way to use coerce function in Peewee or Pymongo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants