|
13 | 13 | import importlib
|
14 | 14 | import re
|
15 | 15 | import warnings
|
| 16 | +from inspect import Parameter |
16 | 17 | from types import ModuleType
|
17 | 18 | from typing import Any, Callable, Dict, Iterator, List, Sequence, Set, Tuple, Type, Union
|
18 | 19 | from unittest.mock import patch
|
@@ -1108,9 +1109,10 @@ def annotate_to_first_argument(self, func: Callable, typ: Type) -> None:
|
1108 | 1109 | if len(sig.parameters) == 0:
|
1109 | 1110 | return
|
1110 | 1111 |
|
1111 |
| - name = list(sig.parameters)[0] |
1112 |
| - if name not in func.__annotations__: |
1113 |
| - func.__annotations__[name] = typ |
| 1112 | + params = list(sig.parameters.values()) |
| 1113 | + if params[0].annotation is Parameter.empty: |
| 1114 | + params[0] = params[0].replace(annotation=typ) |
| 1115 | + func.__signature__ = sig.replace(parameters=params) # type: ignore |
1114 | 1116 |
|
1115 | 1117 |
|
1116 | 1118 | class DecoratorDocumenter(FunctionDocumenter):
|
@@ -1508,13 +1510,14 @@ def add_directive_header(self, sig: str) -> None:
|
1508 | 1510 |
|
1509 | 1511 | def annotate_to_first_argument(self, func: Callable, typ: Type) -> None:
|
1510 | 1512 | """Annotate type hint to the first argument of function if needed."""
|
1511 |
| - sig = inspect.signature(func, bound_method=True) |
1512 |
| - if len(sig.parameters) == 0: |
| 1513 | + sig = inspect.signature(func) |
| 1514 | + if len(sig.parameters) == 1: |
1513 | 1515 | return
|
1514 | 1516 |
|
1515 |
| - name = list(sig.parameters)[0] |
1516 |
| - if name not in func.__annotations__: |
1517 |
| - func.__annotations__[name] = typ |
| 1517 | + params = list(sig.parameters.values()) |
| 1518 | + if params[1].annotation is Parameter.empty: |
| 1519 | + params[1] = params[1].replace(annotation=typ) |
| 1520 | + func.__signature__ = sig.replace(parameters=params) # type: ignore |
1518 | 1521 |
|
1519 | 1522 |
|
1520 | 1523 | class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): # type: ignore
|
|
0 commit comments