Repository: pydantic/pydantic

Issue:
BaseSettings descendants call default_factory on import
### 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

When importing `BaseSettings` descendant from another file, `default_factory` functors are always called for uninstantiated models. See the code snippet attached below. `def factory()` is called once on import, even though `B` is not even instantiated. 

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /usr/local/anaconda3/envs/dirty_3.9/lib/python3.9/site-packages/pydantic
               python version: 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:20)  [Clang 11.0.1 ]
                     platform: macOS-11.1-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']
```

<!-- Where possible please include a self-contained code snippet describing your bug: -->

The first file (`kek.py`):

```py
from pydantic import BaseSettings, Field

v = 0

def factory():
    print('factory called')
    global v
    v += 1
    return v


class A(BaseSettings):
    pass


class B(A):
    id: int = Field(default_factory=factory)

        
class C(A):
    pass
```

The second file:

```py
from kek import C
```

Expected output: empty.

Actual output: `factory called`.

It's also worth mentioning that `BaseModel` does not have the described behavior, importing the `BaseModel` descendants doesn't call `default_factory` functors. 


Comments:
I co-discovered this issue. 100% True
Hi and thanks for reporting
`BaseSettings` inherits from `BaseModel` with other defaults for the config including `validate_all` which is set to True by default.
As explained in the [warning section in the doc](https://pydantic-docs.helpmanual.io/usage/models/#field-with-dynamic-default-value), to validate the default value of a field, we actually need to call the `default_factory`.
I guess you could find a workaround in your default factory or set `validate_all = False`

```py
class A(BaseSettings, validate_all=False):
    pass
```

We could also allow people to not validate values with `default_factory`
Hope it helps in the meantime
@PrettyWood 

Thank you, that works for now. It's still kind of strange though, I believe it should be possible to validate all values (including the default ones) on initialization. Might be better to hide this functionality under some option (`validate_on_init`), too.

Patch:
diff --git a/pydantic/fields.py b/pydantic/fields.py
--- a/pydantic/fields.py
+++ b/pydantic/fields.py
@@ -458,17 +458,13 @@ def prepare(self) -> None:
     def _set_default_and_type(self) -> None:
         """
         Set the default value, infer the type if needed and check if `None` value is valid.
-
-        Note: to prevent side effects by calling the `default_factory` for nothing, we only call it
-        when we want to validate the default value i.e. when `validate_all` is set to True.
         """
         if self.default_factory is not None:
             if self.type_ is Undefined:
                 raise errors_.ConfigError(
                     f'you need to set the type of field {self.name!r} when using `default_factory`'
                 )
-            if not self.model_config.validate_all:
-                return
+            return
 
         default_value = self.get_default()
 


Test patch:
diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py
--- a/tests/test_edge_cases.py
+++ b/tests/test_edge_cases.py
@@ -1695,12 +1695,12 @@ class Model(BaseModel):
     Model()
 
 
-def test_default_factory_side_effect():
-    """It may call `default_factory` more than once when `validate_all` is set"""
+def test_default_factory_called_once():
+    """It should never call `default_factory` more than once even when `validate_all` is set"""
 
     v = 0
 
-    def factory():
+    def factory() -> int:
         nonlocal v
         v += 1
         return v
@@ -1712,7 +1712,20 @@ class Config:
             validate_all = True
 
     m1 = MyModel()
-    assert m1.id == 2  # instead of 1
+    assert m1.id == 1
+
+    class MyBadModel(BaseModel):
+        id: List[str] = Field(default_factory=factory)
+
+        class Config:
+            validate_all = True
+
+    with pytest.raises(ValidationError) as exc_info:
+        MyBadModel()
+    assert v == 2  # `factory` has been called to run validation
+    assert exc_info.value.errors() == [
+        {'loc': ('id',), 'msg': 'value is not a valid list', 'type': 'type_error.list'},
+    ]
 
 
 def test_default_factory_validator_child():