mkorpela / overrides

A decorator to automatically detect mismatch when overriding a method
Apache License 2.0
261 stars 32 forks source link

Name Validation on Positional-Only Parameters #88

Closed erikvanderwerf closed 2 years ago

erikvanderwerf commented 3 years ago

Hello!

I have a generic Serializer class which declares a serialize(...) method. My issue is that the superclass abstract method uses a generic parameter name (o) which is hidden by a positional-only / (PEP570). while I would like to use more descriptive parameter names in my subclasses. All uses of this class will pass-by-position, never by name.

from abc import ABC, abstractmethod
from typing import Generic, TypeVar

from overrides import overrides

T = TypeVar("T")

class Serializer(Generic[T], ABC):
    @abstractmethod
    def serialize(self, o: T, /) -> bytes:
        raise NotImplementedError()

class MyDataSerializer(Serializer[object]):
    @overrides
    def serialize(self, data: object, /) -> bytes:
        pass

The above example raises a TypeError on overrides==6.1.0.

TypeError: MyDataSerializer.serialize: data is not a valid parameter.

I can see the necessity of name-checking when parameter names can be used directly. However, when used with PEP570 I would like to see parameters only validated by the type at each ordinal/position and to ignore the name.

Thanks!

mkorpela commented 2 years ago

Fixed in https://github.com/mkorpela/overrides/commit/eb07ca2a8fdd319fa3ec87233f9149cea078be18