即使变量类型已知,Pylance 也会报告UnknownMemberType

问题描述

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

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

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

#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,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 中使用 self.exportable 访问 SubClass 的 super().exportable,虽然我认为这不是必要的,但是当我运行程序时,我收到一个错误说 {{3 }}。值得一提的是,当不运行程序时,此 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 错误静音,但我担心我会掩盖一些我不知道的东西。

谢谢

解决方法

我想我可能已经通过使用 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,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 等的子字典)
  • 将初始 exportable 设置为空字典,分配 param0 键和值,然后在 SubSubClass 中仅分配一个新的键值

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...