Fix typing errors with set_extenion methods

eg:
```
Argument of type "(span: Span, tag: str) -> str" cannot be assigned to parameter "method" of type "SpanMethod | None" in function "set_extension"
  Type "(span: Span, tag: str) -> str" is incompatible with type "SpanMethod | None"
    "function" is incompatible with protocol "SpanMethod"
      "__call__" is not present
    "function" is incompatible with "None"PylancereportArgumentType
```

Rationale:
`Callable` only accepts a single parameter, optional parameters don't work
... isn't recognized as variadic arguments (... not valid here)
SomeMethod(Protocol) __call__ approach doesn't support mapping non variadic arguments to variadic arguments.
Therefore, to not break the currently documented interface, implement a union of Callable overloads.
This commit is contained in:
Mike Ribbons 2024-09-02 20:54:21 +10:00
parent 319e02545c
commit 8a45a9fb89
3 changed files with 18 additions and 6 deletions

View File

@ -28,8 +28,12 @@ from .underscore import Underscore
DOCBIN_ALL_ATTRS: Tuple[str, ...]
class DocMethod(Protocol):
def __call__(self: Doc, *args: Any, **kwargs: Any) -> Any: ... # type: ignore[misc]
DocMethod = Union[
Callable[[Doc], Any],
Callable[[Doc, Any], Any],
Callable[[Doc, Any, Any], Any],
Callable[[Doc, Any, Any, Any], Any],
]
class Doc:
vocab: Vocab

View File

@ -8,8 +8,12 @@ from .doc import Doc
from .token import Token
from .underscore import Underscore
class SpanMethod(Protocol):
def __call__(self: Span, *args: Any, **kwargs: Any) -> Any: ... # type: ignore[misc]
SpanMethod = Union[
Callable[[Span], Any],
Callable[[Span, Any], Any],
Callable[[Span, Any, Any], Any],
Callable[[Span, Any, Any, Any], Any],
]
class Span:
@classmethod

View File

@ -9,8 +9,12 @@ from .morphanalysis import MorphAnalysis
from .span import Span
from .underscore import Underscore
class TokenMethod(Protocol):
def __call__(self: Token, *args: Any, **kwargs: Any) -> Any: ... # type: ignore[misc]
TokenMethod = Union[
Callable[[Token], Any],
Callable[[Token, Any], Any],
Callable[[Token, Any, Any], Any],
Callable[[Token, Any, Any, Any], Any],
]
class Token:
i: int