Skip to content

Coerce function example#789

Merged
azmeuk merged 5 commits intopallets-eco:masterfrom
Jeroendevr:coerce_function
Jul 22, 2023
Merged

Coerce function example#789
azmeuk merged 5 commits intopallets-eco:masterfrom
Jeroendevr:coerce_function

Conversation

@Jeroendevr
Copy link
Copy Markdown

Some form functions provide a Coerce argument. In order to make it clear what is possible, I have added a function which I use in my own product.
I did not found a solution for my problem using the docs, that is the reason to add this example.

Is this a valuable addition and is this the right place?

Jeroendevr added 3 commits June 7, 2023 11:08
@Jeroendevr
Copy link
Copy Markdown
Author

Seems the test is not failing because of this pr, so I'll leave it until it is fixed.

@azmeuk
Copy link
Copy Markdown
Member

azmeuk commented Jun 7, 2023

The failing test is related to this PR. It seems there are lines ending with spaces.

 diff --git a/docs/fields.rst b/docs/fields.rst
index 2c034e5..960e23f 100644
--- a/docs/fields.rst
+++ b/docs/fields.rst
@@ -313,9 +313,9 @@ refer to a single input from the form.
     `coerce` keyword arg to :class:`~wtforms.fields.SelectField` says that we
     use :func:`int()` to coerce form data.  The default coerce is
     :func:`str()`.
-    
+
     **Coerce function example**::
-    
+
         def coerce_none(value):
             if value == 'None':
                 return None
@@ -324,7 +324,7 @@ refer to a single input from the form.
         class NonePossible(From):
             choices = [('1', 'Option 1'), ('2', 'Option 2'), ('None', 'No option')]
             my_select_field = SelectField('Select an option', choices=choices, coerce=coerce_none)
-            
+
     Note when the option None is selected a 'None' str will be passed. By using a coerce
     function the 'None' str will be converted to None.
 
style: exit 1 (22.38 seconds) /home/runner/work/wtforms/wtforms> pre-commit run --all-files --show-diff-on-failure pid=1771
  style: FAIL code 1 (25.67=setup[3.29]+cmd[22.38] seconds)
  evaluation failed :( (25.74 seconds)
Error: Process completed with exit code 1.

docs/fields.rst Outdated
return None
return value

class NonePossible(From):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo, this should be Form

docs/fields.rst Outdated
return value

class NonePossible(From):
choices = [('1', 'Option 1'), ('2', 'Option 2'), ('None', 'No option')]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This shouldn't be a class variable, inline this in the SelectField call.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I thought it would improve readability. Thanks for the feedback, I have adjusted this in the new commit.

@Jeroendevr
Copy link
Copy Markdown
Author

I did a
pre-commit run --all-files --show-diff-on-failure I get multiple flake8 failings but not related to this fields.rst Do you have any feedback?

@azmeuk azmeuk added the documentation Related to the documentation label Jul 22, 2023
@azmeuk azmeuk merged commit b9a689f into pallets-eco:master Jul 22, 2023
@azmeuk
Copy link
Copy Markdown
Member

azmeuk commented Jul 22, 2023

Thank you!

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

Labels

documentation Related to the documentation

Development

Successfully merging this pull request may close these issues.

3 participants