【问题标题】:Pylance reportUnknownMemberType even though variable type is knownPylance 报告UnknownMemberType,即使变量类型已知
【发布时间】:2021-02-23 09:19:33
【问题描述】:

我在 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 合并,但我得到了这个 cannot assign 错误。

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

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

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

谢谢

【问题讨论】:

    标签: python class dictionary pylance


    【解决方案1】:

    我想我可以通过使用 SubClass 的可导出更灵活一点来解决这个问题。也就是说,创建一个空 dict,添加初始属性,并在 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
    

    区别似乎在

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 2019-11-11
      • 2017-09-21
      • 2023-01-11
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多