typing a Callable with *args and keword-only args

I am trying to make a system that records the methods calls of a class. (I already encountered some issues for which I posted yesterday and they were solved).

The idea is the following:

from collections.abc import Callable
from typing import Concatenate, ParamSpec, TypeVar

RetType = TypeVar("RetType")
Param = ParamSpec("Param")
CountType = TypeVar("CountType", bound="FunctionCount")


class FunctionCount:
    def __init__(self, count_dict: dict[str, int]) -> None:
        self.count_dict = count_dict


def count(
    func: Callable[Concatenate[CountType, Param], RetType],
) -> Callable[Concatenate[CountType, str, Param], RetType]:
    def wrapper(
        self: CountType,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType:
        function_name = log_name or f"{self.__class__.__name__}.{func.__name__}"
        if function_name not in self.count_dict:
            self.count_dict[function_name] = 0
        self.count_dict[function_name] += 1

        return func(self, *args, **kwargs)

    return wrapper


class A(FunctionCount):
    @count
    def test(self, multiplier: float) -> float:
        return multiplier


count_dict = dict[str, int]()
A(count_dict).test(2.0)
A(count_dict).test(multiplier=2.0, log_name="testing")

print(count_dict)
assert count_dict == {"A.test": 1, "testing": 1}

The script is executed without any problem but mypy has problems
It signals me that wrapper does not have the correct type:

Incompatible return value type (got "Callable[[CountType, VarArg(Any), DefaultNamedArg(str | None, 'log_name'), KwArg(Any)], RetType]", expected "Callable[[CountType, str, **Param], RetType]")

I guess this is because I wanted log_name to be a keyword-only argument so understanding the call A(count_dict).test(2.0, log_name="testing") is quite easier than if we had A(count_dict).test(2.0, "testing")

I tried to use Protocol for the typing but then I had other problems

from collections.abc import Callable
from typing import Concatenate, ParamSpec, Protocol, TypeVar

RetType_co = TypeVar("RetType_co", covariant=True)
Param = ParamSpec("Param")
CountType_contra = TypeVar(
    "CountType_contra", bound="FunctionCount", contravariant=True
)


class FunctionCount:
    def __init__(self, count_dict: dict[str, int]) -> None:
        self.count_dict = count_dict


class CountableCallable(Protocol[CountType_contra, Param, RetType_co]):
    def __call__(
        _self,  # noqa: N805
        self: CountType_contra,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType_co: ...


def count(
    func: Callable[Concatenate[CountType_contra, Param], RetType_co],
) -> CountableCallable[CountType_contra, Param, RetType_co]:
    def wrapper(
        self: CountType_contra,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType_co:
        function_name = log_name or f"{self.__class__.__name__}.{func.__name__}"
        if function_name not in self.count_dict:
            self.count_dict[function_name] = 0
        self.count_dict[function_name] += 1

        return func(self, *args, **kwargs)

    return wrapper


class A(FunctionCount):
    @count
    def test(self, multiplier: float) -> float:
        return multiplier


count_dict = dict[str, int]()
A(count_dict).test(2.0)
A(count_dict).test(multiplier=2.0, log_name="testing")

print(count_dict)
assert count_dict == {"A.test": 1, "testing": 1}

In this case, the A(count_dict).test(2.0) lines raise the following error:

Argument 1 to "__call__" of "CountableCallable" has incompatible type "float"; expected "A"

Calling A(count_dict).test(multiplier=2.0, log_name="testing") raises this error:

Missing positional argument "self" in call to "__call__" of "CountableCallable"

The only calling working is A.test(A(count_dict), 2.0) which is not the way people call methods.

I also tried to use a decorator instead of a wrapper but I would get the same alerts
I tried to intervert the names of the self and __self__ arguments for CountableCallable.__call__ but it did not do anything

So I was wondering if there was a Protocol variation that would allow me to specify that CountableCallable is a class method (so maybe we wouldn’t have confusion)
Or if there was a way to use typing.Callable while precising that log_name is a keyword-only argument

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

typing a Callable with *args and keword-only args

I am trying to make a system that records the methods calls of a class. (I already encountered some issues for which I posted yesterday and they were solved).

The idea is the following:

from collections.abc import Callable
from typing import Concatenate, ParamSpec, TypeVar

RetType = TypeVar("RetType")
Param = ParamSpec("Param")
CountType = TypeVar("CountType", bound="FunctionCount")


class FunctionCount:
    def __init__(self, count_dict: dict[str, int]) -> None:
        self.count_dict = count_dict


def count(
    func: Callable[Concatenate[CountType, Param], RetType],
) -> Callable[Concatenate[CountType, str, Param], RetType]:
    def wrapper(
        self: CountType,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType:
        function_name = log_name or f"{self.__class__.__name__}.{func.__name__}"
        if function_name not in self.count_dict:
            self.count_dict[function_name] = 0
        self.count_dict[function_name] += 1

        return func(self, *args, **kwargs)

    return wrapper


class A(FunctionCount):
    @count
    def test(self, multiplier: float) -> float:
        return multiplier


count_dict = dict[str, int]()
A(count_dict).test(2.0)
A(count_dict).test(multiplier=2.0, log_name="testing")

print(count_dict)
assert count_dict == {"A.test": 1, "testing": 1}

The script is executed without any problem but mypy has problems
It signals me that wrapper does not have the correct type:

Incompatible return value type (got "Callable[[CountType, VarArg(Any), DefaultNamedArg(str | None, 'log_name'), KwArg(Any)], RetType]", expected "Callable[[CountType, str, **Param], RetType]")

I guess this is because I wanted log_name to be a keyword-only argument so understanding the call A(count_dict).test(2.0, log_name="testing") is quite easier than if we had A(count_dict).test(2.0, "testing")

I tried to use Protocol for the typing but then I had other problems

from collections.abc import Callable
from typing import Concatenate, ParamSpec, Protocol, TypeVar

RetType_co = TypeVar("RetType_co", covariant=True)
Param = ParamSpec("Param")
CountType_contra = TypeVar(
    "CountType_contra", bound="FunctionCount", contravariant=True
)


class FunctionCount:
    def __init__(self, count_dict: dict[str, int]) -> None:
        self.count_dict = count_dict


class CountableCallable(Protocol[CountType_contra, Param, RetType_co]):
    def __call__(
        _self,  # noqa: N805
        self: CountType_contra,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType_co: ...


def count(
    func: Callable[Concatenate[CountType_contra, Param], RetType_co],
) -> CountableCallable[CountType_contra, Param, RetType_co]:
    def wrapper(
        self: CountType_contra,
        /,
        *args: Param.args,
        log_name: str | None = None,
        **kwargs: Param.kwargs,
    ) -> RetType_co:
        function_name = log_name or f"{self.__class__.__name__}.{func.__name__}"
        if function_name not in self.count_dict:
            self.count_dict[function_name] = 0
        self.count_dict[function_name] += 1

        return func(self, *args, **kwargs)

    return wrapper


class A(FunctionCount):
    @count
    def test(self, multiplier: float) -> float:
        return multiplier


count_dict = dict[str, int]()
A(count_dict).test(2.0)
A(count_dict).test(multiplier=2.0, log_name="testing")

print(count_dict)
assert count_dict == {"A.test": 1, "testing": 1}

In this case, the A(count_dict).test(2.0) lines raise the following error:

Argument 1 to "__call__" of "CountableCallable" has incompatible type "float"; expected "A"

Calling A(count_dict).test(multiplier=2.0, log_name="testing") raises this error:

Missing positional argument "self" in call to "__call__" of "CountableCallable"

The only calling working is A.test(A(count_dict), 2.0) which is not the way people call methods.

I also tried to use a decorator instead of a wrapper but I would get the same alerts
I tried to intervert the names of the self and __self__ arguments for CountableCallable.__call__ but it did not do anything

So I was wondering if there was a Protocol variation that would allow me to specify that CountableCallable is a class method (so maybe we wouldn’t have confusion)
Or if there was a way to use typing.Callable while precising that log_name is a keyword-only argument

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật