marshmallow-code / marshmallow-code/marshmallow

Option to have strict Float field

Open
#1,601 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback welcome
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

I [don't see](https://marshmallow.readthedocs.io/en/stable/api_reference.html) any kwarg to make Float field strict, similar to Int and Boolean fields:

```python
import pytest
from marshmallow import fields, Schema, ValidationError

class ParamsSchema(Schema):
integer = fields.Int(strict=True)
number = fields.Float() # need an option to make it strict
boolean = fields.Boolean(truthy=[True], falsy=[False])

@pytest.fixture
def schema():
return ParamsSchema()

def test_strict(schema):
schema.load({
'integer': 0,
'number': 3.142,
'boolean': True
})

def test_int_str(schema):
with pytest.raises(ValidationError):
schema.load({'integer': '0'})

def test_float_str(schema):
with pytest.raises(ValidationError):
schema.load({'number': '3.142'})

def test_boolean_str(schema):
with pytest.raises(ValidationError):
schema.load({'boolean': 'true'})
```

Is there plan to add that in in future? Or I have to create my own field class?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing fields.Float implementation and the strict=True behavior of fields.Int. Use the examples in the issue as the behavioral cases, including rejecting a string for the Float field, and add tests alongside the existing field validation tests. Done means Float offers an equivalent strict option and the documented examples pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.