Repository: samuelcolvin/pydantic (python)

Issue:
Serialization -> de-serialisation fails for small timedelta (< 100 microseconds)
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# Bug

The serialization -> de-serialization of a model with small `timedelta` raises a `ValidationError`. The de-serialization fails only when the `timedelta` is below 100 microseconds, see the following example:

```py
from datetime import timedelta
from pydantic import BaseModel

class Model(BaseModel):
    duration: timedelta


# This works
model = Model(duration=timedelta(microseconds=100))
Model.parse_raw(model.json())
# This Fails
model = Model(duration=timedelta(microseconds=99))
Model.parse_raw(model.json())
```

Last line throws the following error:
```py
pydantic.error_wrappers.ValidationError: 1 validation error for Model
duration
  invalid duration format (type=value_error.duration)
```

I believe the error comes from the `parse_duration` function, and in particular the line where the input value is converted to `str`.
https://github.com/samuelcolvin/pydantic/blob/c256dccbb383a7fd462f62fcb5d55558eb3cb108/pydantic/datetime_parse.py#L226-L231
Indeed `str(0.0001)` gives `"0.0001"` but `str(0.000099)` gives `"9.9e-5"`, thus the `re.match` fails.

Changing `value = str(value)` to `value = "f{value:.6f}"` should fix this. I would be happy to create a PR to solve the issue.

# System information

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: <my-home>/.pyenv/versions/3.7.11/envs/pydantic/lib/python3.7/site-packages/pydantic
               python version: 3.7.11 (default, Aug 31 2021, 20:43:02)  [Clang 10.0.1 (clang-1001.0.46.4)]
                     platform: Darwin-19.6.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']
```

Pull request:
fix parsing of tiny timedelta values
## Related issue number

fix #3315

## Checklist

* [x] Unit tests for the changes exist
* [ ] Documentation reflects the changes where applicable
* [x] `changes/<pull request or issue id>-<github username>.md` file added describing change
* [ ] My PR is ready to review, **please add a comment including the phrase "please review" to assign reviewers**


Patch:
diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md
deleted file mode 100644
index b87a10e70..000000000
--- a/.github/ISSUE_TEMPLATE/bug.md
+++ /dev/null
@@ -1,32 +0,0 @@
----
-name: Bug
-about: Create a bug report to help us improve pydantic
-labels: bug
----
-
-### Checks
-
-* [ ] I added a descriptive title to this issue
-* [ ] I have searched (google, github) for similar issues and couldn't find anything
-* [ ] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug
-
-<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->
-
-# Bug
-
-Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
-```
-...
-```
-<!-- or if you're using pydantic prior to v1.3, manually include: OS, python version and pydantic version -->
-
-<!-- Please read the [docs](https://pydantic-docs.helpmanual.io/) and search through issues to
-confirm your bug hasn't already been reported. -->
-
-<!-- Where possible please include a self-contained code snippet describing your bug: -->
-
-```py
-import pydantic
-
-...
-```
diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml
new file mode 100644
index 000000000..a5e9350a0
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug.yml
@@ -0,0 +1,92 @@
+name: 🐛 Bug
+description: Report a bug or unexpected behavior in pydantic
+labels: [bug, unconfirmed]
+
+body:
+  - type: markdown
+    attributes:
+      value:  Thank you for contributing to pydantic! ✊
+
+  - type: checkboxes
+    id: checks
+    attributes:
+      label: Initial Checks
+      description: |
+        Just a few checks to make sure you need to create a bug report.
+        
+        _Sorry to sound so draconian 👿; but every second spent replying to issues is time not spent improving pydantic 🙇._
+      options:
+        - label: I have searched GitHub for a duplicate issue and I'm sure this is something new
+          required: true
+        - label: I have searched Google & StackOverflow for a solution and couldn't find anything
+          required: true
+        - label: I have read and followed [the docs](https://pydantic-docs.helpmanual.io) and still think this is a bug
+          required: true
+        - label: >
+            I am confident that the issue is with pydantic 
+            (not my code, or another library in the ecosystem like [FastAPI](https://fastapi.tiangolo.com) or
+            [mypy](https://mypy.readthedocs.io/en/stable))
+          required: true
+
+  - type: textarea
+    id: description
+    attributes:
+      label: Description
+      description: |
+        Please explain what you're seeing and what you would expect to see.
+
+        Please provide as much detail as possible to make understanding and solving your problem as quick as possible. 🙏
+    validations:
+      required: true
+
+  - type: textarea
+    id: example
+    attributes:
+      label: Example Code
+      description: >
+        If applicable, please add a self-contained,
+        [minimal, reproducible, example](https://stackoverflow.com/help/minimal-reproducible-example)
+        demonstrating the bug.
+
+      placeholder: |
+        import pydantic
+
+        ...
+      render: Python
+
+  - type: textarea
+    id: version
+    attributes:
+      label: Python, Pydantic & OS Version
+      description: |
+        Which version of Python & Pydantic are you using, and which Operating System?
+        
+        Please run the following command and copy the output below:
+
+        ```bash
+        python -c "import pydantic.utils; print(pydantic.utils.version_info())"
+        ```
+
+      render: Text
+    validations:
+      required: true
+
+  - type: checkboxes
+    id: affected-components
+    attributes:
+      label: Affected Components
+      description: Which of the following parts of pydantic does this bug affect?
+      # keep this lis in sync with feature_request.yml
+      options:
+        - label: '[Compatibility between releases](https://pydantic-docs.helpmanual.io/changelog/)'
+        - label: '[Data validation/parsing](https://pydantic-docs.helpmanual.io/usage/models/#basic-model-usage)'
+        - label: '[Data serialization](https://pydantic-docs.helpmanual.io/usage/exporting_models/) - `.dict()` and `.json()`'
+        - label: '[JSON Schema](https://pydantic-docs.helpmanual.io/usage/schema/)'
+        - label: '[Dataclasses](https://pydantic-docs.helpmanual.io/usage/dataclasses/)'
+        - label: '[Model Config](https://pydantic-docs.helpmanual.io/usage/model_config/)'
+        - label: '[Field Types](https://pydantic-docs.helpmanual.io/usage/types/) - adding or changing a particular data type'
+        - label: '[Function validation decorator](https://pydantic-docs.helpmanual.io/usage/validation_decorator/)'
+        - label: '[Generic Models](https://pydantic-docs.helpmanual.io/usage/models/#generic-models)'
+        - label: '[Other Model behaviour](https://pydantic-docs.helpmanual.io/usage/models/) - `construct()`, pickling, private attributes, ORM mode'
+        - label: '[Settings Management](https://pydantic-docs.helpmanual.io/usage/settings/)'
+        - label: '[Plugins](https://pydantic-docs.helpmanual.io/) and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.'
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
index bf1982bfa..f498e4e2b 100644
--- a/.github/ISSUE_TEMPLATE/config.yml
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -1,11 +1,5 @@
 blank_issues_enabled: true
 contact_links:
-- name: Question
-  url: 'https://github.com/samuelcolvin/pydantic/discussions/new?category=question'
-  about: Ask a question about how to use pydantic using github discussions
-
-- name: Feature Request
-  url: 'https://github.com/samuelcolvin/pydantic/discussions/new?category=feature-request'
-  about: >
-    If you think we should add a new feature to pydantic, please start a discussion, once it attracts
-    wider support, it can be migrated to an issue
+  - name: 🤔 Ask a Question
+    url: 'https://github.com/samuelcolvin/pydantic/discussions/new?category=question'
+    about: Ask a question about how to use pydantic using github discussions
diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml
new file mode 100644
index 000000000..26273cbf4
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/feature_request.yml
@@ -0,0 +1,56 @@
+name: 🚀 Feature request
+description: Suggest a new feature or change to pydantic
+labels: [feature request]
+
+body:
+  - type: markdown
+    attributes:
+      value: Thank you for contributing to pydantic! ✊
+
+  - type: checkboxes
+    id: searched
+    attributes:
+      label: Initial Checks
+      description: |
+        Just a few checks to make sure you need to create a feature request.
+        
+        _Sorry to sound so draconian 👿; but every second spent replying to issues is time not spent improving pydantic 🙇._
+      options:
+        - label: I have searched Google & GitHub for similar requests and couldn't find anything
+          required: true
+        - label: I have read and followed [the docs](https://pydantic-docs.helpmanual.io) and still think this feature is missing
+          required: true
+
+  - type: textarea
+    id: description
+    attributes:
+      label: Description
+      description: |
+        Please give as much detail as possible about the feature you would like to suggest. 🙏
+        
+        You might like to add:
+        * A demo of how code might look when using the feature
+        * Your use case(s) for the feature
+        * Why the feature should be added to pydantic (as opposed to another library or just implemented in your code)
+    validations:
+      required: true
+
+  - type: checkboxes
+    id: affected-components
+    attributes:
+      label: Affected Components
+      description: Which of the following parts of pydantic does this feature affect?
+      # keep this lis in sync with bug.yml
+      options:
+        - label: '[Compatibility between releases](https://pydantic-docs.helpmanual.io/changelog/)'
+        - label: '[Data validation/parsing](https://pydantic-docs.helpmanual.io/usage/models/#basic-model-usage)'
+        - label: '[Data serialization](https://pydantic-docs.helpmanual.io/usage/exporting_models/) - `.dict()` and `.json()`'
+        - label: '[JSON Schema](https://pydantic-docs.helpmanual.io/usage/schema/)'
+        - label: '[Dataclasses](https://pydantic-docs.helpmanual.io/usage/dataclasses/)'
+        - label: '[Model Config](https://pydantic-docs.helpmanual.io/usage/model_config/)'
+        - label: '[Field Types](https://pydantic-docs.helpmanual.io/usage/types/) - adding or changing a particular data type'
+        - label: '[Function validation decorator](https://pydantic-docs.helpmanual.io/usage/validation_decorator/)'
+        - label: '[Generic Models](https://pydantic-docs.helpmanual.io/usage/models/#generic-models)'
+        - label: '[Other Model behaviour](https://pydantic-docs.helpmanual.io/usage/models/) - `construct()`, pickling, private attributes, ORM mode'
+        - label: '[Settings Management](https://pydantic-docs.helpmanual.io/usage/settings/)'
+        - label: '[Plugins](https://pydantic-docs.helpmanual.io/) and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.'
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 998af3f76..508efd759 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -1,6 +1,7 @@
 <!-- Thank you for your contribution! -->
 <!-- Unless your change is trivial, please create an issue to discuss the change before creating a PR -->
 <!-- See https://pydantic-docs.helpmanual.io/contributing/ for help on Contributing -->
+<!-- PLEASE DO **NOT** put issue ids in the PR title! Instead, add a descriptive title and put ids in the body -->
 
 ## Change Summary
 
diff --git a/.gitignore b/.gitignore
index 55c223952..766bf1e03 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,3 +32,4 @@ pydantic/*.so
 /.ghtopdep_cache/
 /fastapi/
 /codecov.sh
+/worktrees/
diff --git a/changes/3112-ojii.md b/changes/3112-ojii.md
new file mode 100644
index 000000000..a4c6e144d
--- /dev/null
+++ b/changes/3112-ojii.md
@@ -0,0 +1,1 @@
+Catch overflow errors in `int_validator`
\ No newline at end of file
diff --git a/changes/3315-samuelcolvin.md b/changes/3315-samuelcolvin.md
new file mode 100644
index 000000000..47f38c5b6
--- /dev/null
+++ b/changes/3315-samuelcolvin.md
@@ -0,0 +1,1 @@
+Fix parsing of very small numeric timedelta values
diff --git a/pydantic/datetime_parse.py b/pydantic/datetime_parse.py
index 024a899d0..b642f17bb 100644
--- a/pydantic/datetime_parse.py
+++ b/pydantic/datetime_parse.py
@@ -223,7 +223,7 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta:
 
     if isinstance(value, (int, float)):
         # below code requires a string
-        value = str(value)
+        value = f'{value:f}'
     elif isinstance(value, bytes):
         value = value.decode()
 
diff --git a/pydantic/validators.py b/pydantic/validators.py
index 2c6565802..897d1bb4a 100644
--- a/pydantic/validators.py
+++ b/pydantic/validators.py
@@ -125,7 +125,7 @@ def int_validator(v: Any) -> int:
 
     try:
         return int(v)
-    except (TypeError, ValueError):
+    except (TypeError, ValueError, OverflowError):
         raise errors.IntegerError()
 
 


Test patch:
diff --git a/tests/test_datetime_parse.py b/tests/test_datetime_parse.py
index f714d6667..004b161c0 100644
--- a/tests/test_datetime_parse.py
+++ b/tests/test_datetime_parse.py
@@ -175,6 +175,7 @@ def test_parse_python_format(delta):
         ('30', timedelta(seconds=30)),
         (30, timedelta(seconds=30)),
         (30.1, timedelta(seconds=30, milliseconds=100)),
+        (9.9e-05, timedelta(microseconds=99)),
         # minutes seconds
         ('15:30', timedelta(minutes=15, seconds=30)),
         ('5:30', timedelta(minutes=5, seconds=30)),
diff --git a/tests/test_validators.py b/tests/test_validators.py
index f7d7016f1..e7007ad2a 100644
--- a/tests/test_validators.py
+++ b/tests/test_validators.py
@@ -43,6 +43,18 @@ def test_int_validation():
     assert Model(a=4.5).a == 4
 
 
+@pytest.mark.parametrize('value', [2.2250738585072011e308, float('nan'), float('inf')])
+def test_int_overflow_validation(value):
+    class Model(BaseModel):
+        a: int
+
+    with pytest.raises(ValidationError) as exc_info:
+        Model(a=value)
+    assert exc_info.value.errors() == [
+        {'loc': ('a',), 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}
+    ]
+
+
 def test_frozenset_validation():
     class Model(BaseModel):
         a: frozenset