Typing overloaded function signature with non-optional keyword-only argument

Hiya, mypy has been telling me that the first overloaded function signature here is not covered by the implementation. Am I missing something, as I believe it is. Is this just a current shortcoming of the type-checking that mypy is able to do, or is it my mistake?

from collections.abc import Sequence
from typing import overload

class Foo:
    @overload
    def __init__(self, *, pragma_type: str) -> None: ...
    @overload
    def __init__(
        self, raw_rule_codes: Sequence[int], /, pragma_type: str
    ) -> None: ...

    @override
    def __init__(
        self,
        raw_rule_codes: Sequence[int] = (),
        /,
        pragma_type: str | None = None,  # NOTE: Needs a default because comes after another default
    ) -> None:
        if pragma_type is None:
            raise RuntimeError
        ...

Surely the pragma_type argument being keyword-only in the first overload is handled by the positional-or-keyword-only version in the implementation?

I’ve resorted to unfortunately using a #type: ignore[misc] comment on the @override line for now.

Any support would be appreciated!

this overload should be

def __init__(self, /, *, pragma_type: str) -> None: ...
3 Likes

Of course! Thanks, that is what I was missing​:green_heart:

2 Likes

I opened a PR to make the mypy diagnostic more helpful: Improve diagnostic for positional-only related overload error by hauntsaninja · Pull Request #22210 · python/mypy · GitHub

2 Likes