如何检测无效的 kwargs?

How to detect invalid kwargs?

我正在尝试开发一种机制来检测对继承具有鲁棒性的无效 kwargs。我想创建一个 class 和一个检查意外 kwargs 的 init 方法,我想创建另一个 class 继承自第一个 class 并进行相同的 kwarg 检查。这可能比两层更深,但我认为解决两层问题可以进一步推断。这是我的尝试:

class MyClass(object):
    def __init__(self, **kwargs):
        # possible kwargs and default values
        self._kwargs_def = {
            'a':1, 
            'b':2,
            'c':3,
            }
        self.a = kwargs.pop('a', self._kwargs_def['a'])
        self.b = kwargs.pop('b', self._kwargs_def['b'])
        self.c = kwargs.pop('c', self._kwargs_def['c'])

        assert len(kwargs) == 0, f"Invalid kwargs: {kwargs.keys()}"
        
class MyChildClass(MyClass):
    def __init__(self, **kwargs):
        super.__init__() # run once to generate _kwargs_def
        # repeat to override defaults with any appropriate kwargs
        super.__init__(**{key:val for key, val in kwargs.items() if key in super._kwargs_def})
    
        self._kwargs_def.update({'d':4, 'e':5})
        
        self.d = kwargs.pop('d', self._kwargs_def['d'])
        self.e = kwargs.pop('e', self._kwargs_def['e'])
        
        assert len(kwargs) == 0, f"Invalid kwargs: {kwargs.keys()}"
        
my_instance = MyClass(a=7) # works
# I need a way around this: TypeError: descriptor '__init__' of 'super' object needs an argument
my_child_instance = MyChildClass(e=37) # doesn't work but should???
my_instance = MyClass(z=7) # fails and should fail
my_child_instance = MyChildClass(z=37) # fails but for wrong reason

Python 已经检查了无效的关键字参数。只需使用内置机制:

class MyClass:
    def __init__(self, *, a=1, b=2, c=3):
        self.a = a
        self.b = b
        self.c = c

class MyChildClass(MyClass):
    def __init__(self, *, d=4, e=5, **kwargs):
        super().__init__(**kwargs)
        self.d = d
        self.e = e

任何无效的关键字参数都会被 MyClass.__init__ 拒绝。 (位置参数将被拒绝,因为这些参数被定义为仅限关键字。)

你让这比需要的更难了。请注意,我在派生的 class.

中根本没有代码
class MyClass(object):
    kwargs_def = {
        'a':1, 
        'b':2,
        'c':3,
    }
    def __init__(self, **kwargs):
        # possible kwargs and default values
        assert all(k in self.kwargs_def for k in kwargs), f"Invalid kwargs: {kwargs.keys()}"
        self.__dict__.update( self.kwargs_def )
        self.__dict__.update( kwargs )
        
class MyChildClass(MyClass):
    kwargs_def = MyClass.kwargs_def.copy()
    kwargs_def.update({
        'd':4,
        'e':5
    })
        
my_instance = MyClass(a=7) # works
my_child_instance = MyChildClass(e=37) # doesn't work but should???
my_instance = MyClass(z=7) # fails and should fail
my_child_instance = MyChildClass(z=37) # fails but for wrong reason