![](/img/trans.png)
[英]Yellow underlines for libraries due to Pylance, even though they work without problem
[英]Pylance reportUnknownMemberType even though variable type is known
我在 VSCode 上使用 Pylance,我得到這個變量的reportUnknownMemberType警告/錯誤,即使我可以看到它知道類型。
我對課程很陌生,所以如果有更好的方法可以做到這一點,請告訴我。 我的目標是在子類中使用更多鍵來擴展父 class dict。 該程序實際上正在運行,父 dict 獲取新的鍵、值對,但我想知道為什么會出現 Pylance 錯誤。
以下是這些類的 inheritance 鏈,名稱簡化但結構真實。 涉及 3 個類,繼承如 Base > SubClass > SubSubClass。 基礎 class 位於單獨的文件中:
#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 中使用super().exportable
exportable 訪問 SubClass 的self.exportable
,雖然我認為沒有必要,但是當我運行程序時,我得到一個錯誤,說super() 沒有屬性 '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 錯誤靜音,但我擔心我掩蓋了一些我不知道的東西。
謝謝
我想我可能已經通過使用 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
區別似乎在
param0
)和值( param1
、 param2
、 param3
等的子字典)param0
鍵和值,然后在 SubSubClass 中僅分配一個新鍵值我不確定這是否只是一種解決方法,我是否只是設法誘使 Pylance 不抱怨,或者這是否是實際的解決方案。 無論如何,我不再收到 Pylance 錯誤,並且程序(仍然)有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.