prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  AttributeError in class attribute access for validate_call
  ### Initial Checks
  
  - [X] I confirm that I'm using Pydantic V2 installed directly from the `main` branch, or equivalent
  
  ### Description
  
  https://github.com/pydantic/pydantic/pull/6406 introduced an AttributeError when accessing a method on a class (not instance):
  
  ```
   draft @ pip-run 'git+https://github.com/pydantic/pydantic' -- pyd2.py
  Traceback (most recent call last):
    File "/Users/jaraco/draft/pyd2.py", line 14, in <module>
      assert Thing.c == Thing.c
             ^^^^^^^
    File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-o94_k6_k/pydantic/_internal/_validate_call.py", line 101, in __get__
      setattr(obj, self._name, result)
  AttributeError: 'NoneType' object has no attribute 'c'
  ```
  
  ### Example Code
  
  ```Python
  from pydantic import validate_call
  
  
  class Thing:
      def a(self):
          pass
  
      c = validate_call(a)
  
  
  thing = Thing()
  assert thing.a == thing.a
  assert thing.c == thing.c, f'{thing.c} != {thing.c}'
  assert Thing.c == Thing.c
  ```
  
  
  ### Python, Pydantic & OS Version
  
  ```Text
  draft @ pip-run 'git+https://github.com/pydantic/pydantic' -- -c 'import pydantic.version; print(pydantic.version.version_info())'
               pydantic version: 2.0
          pydantic-core version: 2.0.2 release build profile
                   install path: /private/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-hek15lsq/pydantic
                 python version: 3.11.4 (main, Jun 15 2023, 07:55:38) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                       platform: macOS-13.4.1-arm64-arm-64bit
       optional deps. installed: ['typing-extensions']
  ```
  
  
  Selected Assignee: @Kludex
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pydantic/_internal/_validate_call.py`
  Content:
  ```
  1 from __future__ import annotations as _annotations
  2 
  3 import inspect
  4 from dataclasses import dataclass
  5 from functools import partial
  6 from typing import Any, Callable
  7 
  8 import pydantic_core
  9 
  10 from ..config import ConfigDict
  11 from . import _discriminated_union, _generate_schema, _typing_extra
  12 from ._config import ConfigWrapper
  13 from ._core_utils import flatten_schema_defs, inline_schema_defs
  14 
  15 
  16 @dataclass
  17 class CallMarker:
  18     function: Callable[..., Any]
  19     validate_return: bool
  20 
  21 
  22 class ValidateCallWrapper:
  23     """This is a wrapper around a function that validates the arguments passed to it, and optionally the return value.
  24 
  25     It's partially inspired by `wraps` which in turn uses `partial`, but extended to be a descriptor so
  26     these functions can be applied to instance methods, class methods, static methods, as well as normal functions.
  27     """
  28 
  29     __slots__ = (
  30         'raw_function',
  31         '_config',
  32         '_validate_return',
  33         '__pydantic_core_schema__',
  34         '__pydantic_validator__',
  35         '__signature__',
  36         '__name__',
  37         '__qualname__',
  38         '__annotations__',
  39         '__dict__',  # required for __module__
  40     )
  41 
  42     def __init__(self, function: Callable[..., Any], config: ConfigDict | None, validate_return: bool):
  43         self.raw_function = function
  44         self._config = config
  45         self._validate_return = validate_return
  46         self.__signature__ = inspect.signature(function)
  47         if isinstance(function, partial):
  48             func = function.func
  49             self.__name__ = f'partial({func.__name__})'
  50             self.__qualname__ = f'partial({func.__qualname__})'
  51             self.__annotations__ = func.__annotations__
  52             self.__module__ = func.__module__
  53             self.__doc__ = func.__doc__
  54         else:
  55             self.__name__ = function.__name__
  56             self.__qualname__ = function.__qualname__
  57             self.__annotations__ = function.__annotations__
  58             self.__module__ = function.__module__
  59             self.__doc__ = function.__doc__
  60 
  61         namespace = _typing_extra.add_module_globals(function, None)
  62         config_wrapper = ConfigWrapper(config)
  63         gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)
  64         self.__pydantic_core_schema__ = schema = gen_schema.collect_definitions(gen_schema.generate_schema(function))
  65         core_config = config_wrapper.core_config(self)
  66         schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))
  67         simplified_schema = inline_schema_defs(schema)
  68         self.__pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)
  69 
  70         if self._validate_return:
  71             return_type = (
  72                 self.__signature__.return_annotation
  73                 if self.__signature__.return_annotation is not self.__signature__.empty
  74                 else Any
  75             )
  76             gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)
  77             self.__return_pydantic_core_schema__ = schema = gen_schema.collect_definitions(
  78                 gen_schema.generate_schema(return_type)
  79             )
  80             core_config = config_wrapper.core_config(self)
  81             schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))
  82             simplified_schema = inline_schema_defs(schema)
  83             self.__return_pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)
  84         else:
  85             self.__return_pydantic_core_schema__ = None
  86             self.__return_pydantic_validator__ = None
  87 
  88         self._name: str | None = None  # set by __get__, used to set the instance attribute when decorating methods
  89 
  90     def __call__(self, *args: Any, **kwargs: Any) -> Any:
  91         res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))
  92         if self.__return_pydantic_validator__:
  93             return self.__return_pydantic_validator__.validate_python(res)
  94         return res
  95 
  96     def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:
  97         """Bind the raw function and return another ValidateCallWrapper wrapping that."""
  98         bound_function = self.raw_function.__get__(obj, objtype)
  99         result = self.__class__(bound_function, self._config, self._validate_return)
  100         if self._name is not None:
  101             setattr(obj, self._name, result)
  102         return result
  103 
  104     def __set_name__(self, owner: Any, name: str) -> None:
  105         self._name = name
  106 
  107     def __repr__(self) -> str:
  108         return f'ValidateCallWrapper({self.raw_function})'
  109
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pydantic/_internal/_validate_call.py b/pydantic/_internal/_validate_call.py
  --- a/pydantic/_internal/_validate_call.py
  +++ b/pydantic/_internal/_validate_call.py
  @@ -95,10 +95,21 @@
   
       def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:
           """Bind the raw function and return another ValidateCallWrapper wrapping that."""
  +        if obj is None:
  +            try:
  +                # Handle the case where a method is accessed as a class attribute
  +                return objtype.__getattribute__(objtype, self._name)  # type: ignore
  +            except AttributeError:
  +                # This will happen the first time the attribute is accessed
  +                pass
  +
           bound_function = self.raw_function.__get__(obj, objtype)
           result = self.__class__(bound_function, self._config, self._validate_return)
           if self._name is not None:
  -            setattr(obj, self._name, result)
  +            if obj is not None:
  +                setattr(obj, self._name, result)
  +            else:
  +                setattr(objtype, self._name, result)
           return result
   
       def __set_name__(self, owner: Any, name: str) -> None:
  
verification_info: {"golden_diff": "diff --git a/pydantic/_internal/_validate_call.py b/pydantic/_internal/_validate_call.py\n--- a/pydantic/_internal/_validate_call.py\n+++ b/pydantic/_internal/_validate_call.py\n@@ -95,10 +95,21 @@\n \n     def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:\n         \"\"\"Bind the raw function and return another ValidateCallWrapper wrapping that.\"\"\"\n+        if obj is None:\n+            try:\n+                # Handle the case where a method is accessed as a class attribute\n+                return objtype.__getattribute__(objtype, self._name)  # type: ignore\n+            except AttributeError:\n+                # This will happen the first time the attribute is accessed\n+                pass\n+\n         bound_function = self.raw_function.__get__(obj, objtype)\n         result = self.__class__(bound_function, self._config, self._validate_return)\n         if self._name is not None:\n-            setattr(obj, self._name, result)\n+            if obj is not None:\n+                setattr(obj, self._name, result)\n+            else:\n+                setattr(objtype, self._name, result)\n         return result\n \n     def __set_name__(self, owner: Any, name: str) -> None:\n", "issue": "AttributeError in class attribute access for validate_call\n### Initial Checks\n\n- [X] I confirm that I'm using Pydantic V2 installed directly from the `main` branch, or equivalent\n\n### Description\n\nhttps://github.com/pydantic/pydantic/pull/6406 introduced an AttributeError when accessing a method on a class (not instance):\r\n\r\n```\r\n draft @ pip-run 'git+https://github.com/pydantic/pydantic' -- pyd2.py\r\nTraceback (most recent call last):\r\n  File \"/Users/jaraco/draft/pyd2.py\", line 14, in <module>\r\n    assert Thing.c == Thing.c\r\n           ^^^^^^^\r\n  File \"/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-o94_k6_k/pydantic/_internal/_validate_call.py\", line 101, in __get__\r\n    setattr(obj, self._name, result)\r\nAttributeError: 'NoneType' object has no attribute 'c'\r\n```\n\n### Example Code\n\n```Python\nfrom pydantic import validate_call\r\n\r\n\r\nclass Thing:\r\n    def a(self):\r\n        pass\r\n\r\n    c = validate_call(a)\r\n\r\n\r\nthing = Thing()\r\nassert thing.a == thing.a\r\nassert thing.c == thing.c, f'{thing.c} != {thing.c}'\r\nassert Thing.c == Thing.c\n```\n\n\n### Python, Pydantic & OS Version\n\n```Text\ndraft @ pip-run 'git+https://github.com/pydantic/pydantic' -- -c 'import pydantic.version; print(pydantic.version.version_info())'\r\n             pydantic version: 2.0\r\n        pydantic-core version: 2.0.2 release build profile\r\n                 install path: /private/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-hek15lsq/pydantic\r\n               python version: 3.11.4 (main, Jun 15 2023, 07:55:38) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\n                     platform: macOS-13.4.1-arm64-arm-64bit\r\n     optional deps. installed: ['typing-extensions']\n```\n\n\nSelected Assignee: @Kludex\n", "before_files": [{"content": "from __future__ import annotations as _annotations\n\nimport inspect\nfrom dataclasses import dataclass\nfrom functools import partial\nfrom typing import Any, Callable\n\nimport pydantic_core\n\nfrom ..config import ConfigDict\nfrom . import _discriminated_union, _generate_schema, _typing_extra\nfrom ._config import ConfigWrapper\nfrom ._core_utils import flatten_schema_defs, inline_schema_defs\n\n\n@dataclass\nclass CallMarker:\n    function: Callable[..., Any]\n    validate_return: bool\n\n\nclass ValidateCallWrapper:\n    \"\"\"This is a wrapper around a function that validates the arguments passed to it, and optionally the return value.\n\n    It's partially inspired by `wraps` which in turn uses `partial`, but extended to be a descriptor so\n    these functions can be applied to instance methods, class methods, static methods, as well as normal functions.\n    \"\"\"\n\n    __slots__ = (\n        'raw_function',\n        '_config',\n        '_validate_return',\n        '__pydantic_core_schema__',\n        '__pydantic_validator__',\n        '__signature__',\n        '__name__',\n        '__qualname__',\n        '__annotations__',\n        '__dict__',  # required for __module__\n    )\n\n    def __init__(self, function: Callable[..., Any], config: ConfigDict | None, validate_return: bool):\n        self.raw_function = function\n        self._config = config\n        self._validate_return = validate_return\n        self.__signature__ = inspect.signature(function)\n        if isinstance(function, partial):\n            func = function.func\n            self.__name__ = f'partial({func.__name__})'\n            self.__qualname__ = f'partial({func.__qualname__})'\n            self.__annotations__ = func.__annotations__\n            self.__module__ = func.__module__\n            self.__doc__ = func.__doc__\n        else:\n            self.__name__ = function.__name__\n            self.__qualname__ = function.__qualname__\n            self.__annotations__ = function.__annotations__\n            self.__module__ = function.__module__\n            self.__doc__ = function.__doc__\n\n        namespace = _typing_extra.add_module_globals(function, None)\n        config_wrapper = ConfigWrapper(config)\n        gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)\n        self.__pydantic_core_schema__ = schema = gen_schema.collect_definitions(gen_schema.generate_schema(function))\n        core_config = config_wrapper.core_config(self)\n        schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))\n        simplified_schema = inline_schema_defs(schema)\n        self.__pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)\n\n        if self._validate_return:\n            return_type = (\n                self.__signature__.return_annotation\n                if self.__signature__.return_annotation is not self.__signature__.empty\n                else Any\n            )\n            gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)\n            self.__return_pydantic_core_schema__ = schema = gen_schema.collect_definitions(\n                gen_schema.generate_schema(return_type)\n            )\n            core_config = config_wrapper.core_config(self)\n            schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))\n            simplified_schema = inline_schema_defs(schema)\n            self.__return_pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)\n        else:\n            self.__return_pydantic_core_schema__ = None\n            self.__return_pydantic_validator__ = None\n\n        self._name: str | None = None  # set by __get__, used to set the instance attribute when decorating methods\n\n    def __call__(self, *args: Any, **kwargs: Any) -> Any:\n        res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))\n        if self.__return_pydantic_validator__:\n            return self.__return_pydantic_validator__.validate_python(res)\n        return res\n\n    def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:\n        \"\"\"Bind the raw function and return another ValidateCallWrapper wrapping that.\"\"\"\n        bound_function = self.raw_function.__get__(obj, objtype)\n        result = self.__class__(bound_function, self._config, self._validate_return)\n        if self._name is not None:\n            setattr(obj, self._name, result)\n        return result\n\n    def __set_name__(self, owner: Any, name: str) -> None:\n        self._name = name\n\n    def __repr__(self) -> str:\n        return f'ValidateCallWrapper({self.raw_function})'\n", "path": "pydantic/_internal/_validate_call.py"}], "after_files": [{"content": "from __future__ import annotations as _annotations\n\nimport inspect\nfrom dataclasses import dataclass\nfrom functools import partial\nfrom typing import Any, Callable\n\nimport pydantic_core\n\nfrom ..config import ConfigDict\nfrom . import _discriminated_union, _generate_schema, _typing_extra\nfrom ._config import ConfigWrapper\nfrom ._core_utils import flatten_schema_defs, inline_schema_defs\n\n\n@dataclass\nclass CallMarker:\n    function: Callable[..., Any]\n    validate_return: bool\n\n\nclass ValidateCallWrapper:\n    \"\"\"This is a wrapper around a function that validates the arguments passed to it, and optionally the return value.\n\n    It's partially inspired by `wraps` which in turn uses `partial`, but extended to be a descriptor so\n    these functions can be applied to instance methods, class methods, static methods, as well as normal functions.\n    \"\"\"\n\n    __slots__ = (\n        'raw_function',\n        '_config',\n        '_validate_return',\n        '__pydantic_core_schema__',\n        '__pydantic_validator__',\n        '__signature__',\n        '__name__',\n        '__qualname__',\n        '__annotations__',\n        '__dict__',  # required for __module__\n    )\n\n    def __init__(self, function: Callable[..., Any], config: ConfigDict | None, validate_return: bool):\n        self.raw_function = function\n        self._config = config\n        self._validate_return = validate_return\n        self.__signature__ = inspect.signature(function)\n        if isinstance(function, partial):\n            func = function.func\n            self.__name__ = f'partial({func.__name__})'\n            self.__qualname__ = f'partial({func.__qualname__})'\n            self.__annotations__ = func.__annotations__\n            self.__module__ = func.__module__\n            self.__doc__ = func.__doc__\n        else:\n            self.__name__ = function.__name__\n            self.__qualname__ = function.__qualname__\n            self.__annotations__ = function.__annotations__\n            self.__module__ = function.__module__\n            self.__doc__ = function.__doc__\n\n        namespace = _typing_extra.add_module_globals(function, None)\n        config_wrapper = ConfigWrapper(config)\n        gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)\n        self.__pydantic_core_schema__ = schema = gen_schema.collect_definitions(gen_schema.generate_schema(function))\n        core_config = config_wrapper.core_config(self)\n        schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))\n        simplified_schema = inline_schema_defs(schema)\n        self.__pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)\n\n        if self._validate_return:\n            return_type = (\n                self.__signature__.return_annotation\n                if self.__signature__.return_annotation is not self.__signature__.empty\n                else Any\n            )\n            gen_schema = _generate_schema.GenerateSchema(config_wrapper, namespace)\n            self.__return_pydantic_core_schema__ = schema = gen_schema.collect_definitions(\n                gen_schema.generate_schema(return_type)\n            )\n            core_config = config_wrapper.core_config(self)\n            schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))\n            simplified_schema = inline_schema_defs(schema)\n            self.__return_pydantic_validator__ = pydantic_core.SchemaValidator(simplified_schema, core_config)\n        else:\n            self.__return_pydantic_core_schema__ = None\n            self.__return_pydantic_validator__ = None\n\n        self._name: str | None = None  # set by __get__, used to set the instance attribute when decorating methods\n\n    def __call__(self, *args: Any, **kwargs: Any) -> Any:\n        res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))\n        if self.__return_pydantic_validator__:\n            return self.__return_pydantic_validator__.validate_python(res)\n        return res\n\n    def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:\n        \"\"\"Bind the raw function and return another ValidateCallWrapper wrapping that.\"\"\"\n        if obj is None:\n            try:\n                # Handle the case where a method is accessed as a class attribute\n                return objtype.__getattribute__(objtype, self._name)  # type: ignore\n            except AttributeError:\n                # This will happen the first time the attribute is accessed\n                pass\n\n        bound_function = self.raw_function.__get__(obj, objtype)\n        result = self.__class__(bound_function, self._config, self._validate_return)\n        if self._name is not None:\n            if obj is not None:\n                setattr(obj, self._name, result)\n            else:\n                setattr(objtype, self._name, result)\n        return result\n\n    def __set_name__(self, owner: Any, name: str) -> None:\n        self._name = name\n\n    def __repr__(self) -> str:\n        return f'ValidateCallWrapper({self.raw_function})'\n", "path": "pydantic/_internal/_validate_call.py"}]}