Repository: pydantic/pydantic

Issue:
schema_json() fails for optional namedtuple fields with default value
### 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

# Bug

When trying to generate a schema for a model with an optional `NamedTuple` field which have a default value, `schema_json()` fails when trying to unpack parameters for the tuple:

```
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "pydantic/main.py", line 715, in pydantic.main.BaseModel.schema_json
  File "pydantic/main.py", line 704, in pydantic.main.BaseModel.schema
  File "pydantic/schema.py", line 167, in pydantic.schema.model_schema
  File "pydantic/schema.py", line 548, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 589, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 234, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 202, in pydantic.schema.get_field_info_schema
  File "pydantic/schema.py", line 892, in genexpr
TypeError: <lambda>() missing 1 required positional argument: 'y'
```

```py
 def encode_default(dft: Any) -> Any:
     if isinstance(dft, (int, float, str)):
         return dft
     elif sequence_like(dft):
         t = dft.__class__
>        return t(encode_default(v) for v in dft)
   ...
```

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.0+ (default, Oct 20 2020, 08:43:38)  [GCC 9.3.0]
                     platform: Linux-5.4.0-72-generic-x86_64-with-glibc2.31
     optional deps. installed: ['dotenv', 'typing-extensions']
```

```py
from typing import List, Optional, NamedTuple
from pydantic import BaseModel


class Coordinates(NamedTuple):
    x: float
    y: float


class LocationBase(BaseModel):
    name: str
    coords: Optional[Coordinates] = Coordinates(0, 0)
    zoom: Optional[int] = 10


LocationBase.schema_json()
```



Discussion:

Resolving patch:
diff --git a/pydantic/schema.py b/pydantic/schema.py
--- a/pydantic/schema.py
+++ b/pydantic/schema.py
@@ -895,7 +895,8 @@ def encode_default(dft: Any) -> Any:
         return dft
     elif sequence_like(dft):
         t = dft.__class__
-        return t(encode_default(v) for v in dft)
+        seq_args = (encode_default(v) for v in dft)
+        return t(*seq_args) if is_namedtuple(t) else t(seq_args)
     elif isinstance(dft, dict):
         return {encode_default(k): encode_default(v) for k, v in dft.items()}
     elif dft is None:


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -15,6 +15,7 @@
     Generic,
     Iterable,
     List,
+    NamedTuple,
     NewType,
     Optional,
     Set,
@@ -2294,6 +2295,28 @@ class ModelModified(BaseModel):
     }
 
 
+def test_namedtuple_default():
+    class Coordinates(NamedTuple):
+        x: float
+        y: float
+
+    class LocationBase(BaseModel):
+        coords: Coordinates = Coordinates(0, 0)
+
+    assert LocationBase.schema() == {
+        'title': 'LocationBase',
+        'type': 'object',
+        'properties': {
+            'coords': {
+                'title': 'Coords',
+                'default': Coordinates(x=0, y=0),
+                'type': 'array',
+                'items': [{'title': 'X', 'type': 'number'}, {'title': 'Y', 'type': 'number'}],
+            }
+        },
+    }
+
+
 @pytest.mark.skipif(
     sys.version_info < (3, 7), reason='schema generation for generic fields is not available in python < 3.7'
 )