0

我在 VSCode 上使用 Pylance,我得到这个变量的reportUnknownMemberType警告/错误,即使我可以看到它知道类型。

我对课程很陌生,所以如果有更好的方法可以做到这一点,请告诉我。我的目标是在子类中使用更多键来扩展父类字典。该程序实际上正在运行,父 dict 获取新的键、值对,但我想知道为什么会出现 Pylance 错误。

以下是这些类的继承链,名称简化但结构真实。涉及 3 个类,继承如 Base > SubClass > SubSubClass。Base 类位于一个单独的文件中:

#base.py
     
class Base:
    def __init__(self, param0: str) -> None:
        self.param0 = param0
        self.exportable = dict[str, object]()        
 

其他 2 个类位于第二个文件中:

# subclasses.py

class SubClass(Base):
    def __init__(self, param0: str, param1: str, param2: str, 
                     param3: str, param4: str, 
                     param5: bool=True) -> None:
        super().__init__(param0)
    
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3
        self.param4 = param4
        self.param5 = param5

        self.exportable = {
            self.param0: {
                'param1': self.param1,
                'param2': self.param2,
                'param3': self.param3,            
                'param4': self.param4,
                'param5': self.param5
            }
        }


class SubSubClass(SubClass):
    def __init__(self, param1: str, param3: str) -> None:        
        super().__init__(name='hardcodedparam0', param1=param1, type='hardcodedparam2', param3=param3, 
                param4='hardcodedparam4')

    self.properties = {
        'name': {
            'type': "string"
        },
        'contentBytes': {
            'type': 'string',
            'format': 'byte'
        }
    }

    # this is where I get the error
    new_exportable = self.exportable.copy()
    self.exportable = {**new_exportable, **self.properties}

我最初的计划是只使用self.exportable[self.name]['properties'] = self.properties而不是那个 dict 合并,但我得到这个不能分配错误。

我也尝试在 SubSubClass 中使用 访问 SubClass self.exportablesuper().exportable虽然我认为没有必要,但是当我运行程序时,我得到一个错误,说super() has no attribute 'exportable'。对于它的价值,此super()尝试在不运行程序时也会从上面给出相同的“无法分配”错误。

使用第一个选项(dict 合并)和第二个选项(分配新的属性键和值),程序可以工作并且self.propertiesdict 成功地附加到继承的self.exportabledict 上。但我想知道我是否做错了什么,或者 Pylance 是否只是感到困惑。我认为它很困惑,因为在 SubClass 中它看到 dict 只是dict[str,Union[str,bool]]然后 SubClass B,而不是那个Union[str,bool]值,试图添加propertiesdict,它是一组 dict 本身?

当然,我可以让 Pylance 配置中的 reportUnkownMemberType 错误静音,但我担心我掩盖了一些我不知道的东西。

谢谢

4

1 回答 1

0

我想我可能已经通过使用 SubClass 的可导出来更灵活地解决了这个问题。也就是说,创建一个空字典,添加初始属性,并在 SubSubClass 中添加任何添加:

# subclasses.py

class SubClass(Base):
    def __init__(self, param0: str, param1: str, param2: str, 
                     param3: str, param4: str, 
                     param5: bool=True) -> None:
        super().__init__(param0)
    
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3
        self.param4 = param4
        self.param5 = param5

        # The following two lines are a bit different
        self.exportable = {}
        self.exportable[self.param0] = {
            'param1': self.param1,
            'param2': self.param2,
            'param3': self.param3,            
            'param4': self.param4,
            'param5': self.param5
        }
 


class SubSubClass(SubClass):
    def __init__(self, param1: str, param3: str) -> None:        
        super().__init__(name='hardcodedparam0', param1=param1, type='hardcodedparam2', param3=param3, 
                param4='hardcodedparam4')
        self.properties = {
            'name': {
                'type': "string"
            },
            'contentBytes': {
                'type': 'string',
                'format': 'byte'
            }
        }

        # Now I can assign a key and value with no Pylance error
        self.exportable[self.name]['properties'] = self.properties

区别似乎在

  • 将初始可导出 dict 设置为具有固定键 ( param0) 和值 ( param1param2param3等的子字典)
  • 将初始可导出设置为空字典,分配param0键和值,然后在 SubSubClass 中仅分配一个新的键值

我不确定这是否只是一种解决方法,我是否只是设法诱使 Pylance 不抱怨,或者这是否是实际的解决方案。无论如何,我不再收到 Pylance 错误,并且程序(仍然)有效。

于 2021-02-24T09:23:24.857 回答