Repository: pydantic/pydantic

Issue:
Pydantic 1.X doesn't work with Python 3.12.4
### Initial Checks

- [X] I have searched GitHub for a duplicate issue and I'm sure this is something new
- [X] I have searched Google & StackOverflow for a solution and couldn't find anything
- [X] I have read and followed [the docs](https://docs.pydantic.dev) and still think this is a bug
- [X] 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))


### Description

Using Pydantic 1.10.15 with Python 3.12.4 throws the following error:
```
TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
```

This looks very similar to [this issue ](https://github.com/agronholm/typeguard/issues/466) fixed recently in typeguard by this PR: https://github.com/agronholm/typeguard/pull/460.

### Example Code

I initially discovered the error when trying to import `fastapi` (it was failing [here](https://github.com/tiangolo/fastapi/blob/ceaed0a447ed27a1e8049b7158d7d18249741090/fastapi/openapi/models.py#L124)), but I was able to reproduce by defining a model containing a type which doesn't exist yet:

```Python
Python 3.12.4 (main, Jun  7 2024, 19:37:37) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydantic
>>> class Foo(pydantic.BaseModel):
...     foo: "Nope"
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/pydantic/main.py", line 286, in __new__
    cls.__try_update_forward_refs__()
  File "/usr/local/lib/python3.12/site-packages/pydantic/main.py", line 807, in __try_update_forward_refs__
    update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
  File "/usr/local/lib/python3.12/site-packages/pydantic/typing.py", line 554, in update_model_forward_refs
    update_field_forward_refs(f, globalns=globalns, localns=localns)
  File "/usr/local/lib/python3.12/site-packages/pydantic/typing.py", line 520, in update_field_forward_refs
    field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pydantic/typing.py", line 66, in evaluate_forwardref
    return cast(Any, type_)._evaluate(globalns, localns, set())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
>>>
```

### Python, Pydantic & OS Version

```Text
pydantic version: 1.10.15
            pydantic compiled: False
                 install path: /usr/local/lib/python3.12/site-packages/pydantic
               python version: 3.12.4 (main, Jun  7 2024, 19:37:37) [GCC 10.2.1 20210110]
                     platform: Linux-6.9.3-arch1-1-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']
```


### Affected Components

- [X] [Compatibility between releases](https://docs.pydantic.dev/changelog/)
- [ ] [Data validation/parsing](https://docs.pydantic.dev/concepts/models/#basic-model-usage)
- [ ] [Data serialization](https://docs.pydantic.dev/concepts/serialization/) - `.model_dump()` and `.model_dump_json()`
- [ ] [JSON Schema](https://docs.pydantic.dev/concepts/json_schema/)
- [ ] [Dataclasses](https://docs.pydantic.dev/concepts/dataclasses/)
- [ ] [Model Config](https://docs.pydantic.dev/concepts/config/)
- [ ] [Field Types](https://docs.pydantic.dev/api/types/) - adding or changing a particular data type
- [ ] [Function validation decorator](https://docs.pydantic.dev/concepts/validation_decorator/)
- [X] [Generic Models](https://docs.pydantic.dev/concepts/models/#generic-models)
- [ ] [Other Model behaviour](https://docs.pydantic.dev/concepts/models/) - `model_construct()`, pickling, private attributes, ORM mode
- [ ] [Plugins](https://docs.pydantic.dev/) and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.


Discussion:
Note: I don't reproduce with Pydantic 2. Too bad it happens right when I was working on the migration of my project :sweat_smile: .
Seeing the same issue. In our case we are using pydantic 2.7.1, but are importing mergepythonclient which relies on V1.
Seconded, this is an issue that specifically cropped up moving from python 3.12.3 to python 3.12.4. It also affects pydantic.v1 within pydantic==2.7.3
My solution was to downgrade python to version 3.12.3 and it worked properly


Resolving patch:
diff --git a/pydantic/typing.py b/pydantic/typing.py
--- a/pydantic/typing.py
+++ b/pydantic/typing.py
@@ -63,7 +63,9 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
     def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
         # Even though it is the right signature for python 3.9, mypy complains with
         # `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
-        return cast(Any, type_)._evaluate(globalns, localns, set())
+        # Python 3.13/3.12.4+ made `recursive_guard` a kwarg, so name it explicitly to avoid:
+        # TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
+        return cast(Any, type_)._evaluate(globalns, localns, recursive_guard=set())
 
 
 if sys.version_info < (3, 9):


Test patch: