You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running mypy on the codes provided via pydicom.sr.codedict.codes, I get the following error:
mypy -c 'from pydicom.sr.codedict import codes; codes.SCT.Tissue'
<string>:1: error: Item "Code" of "Union[_CodesDict, _CID_Dict, Code]" has no attribute "Tissue"
Since the attributes are dynamically fetched from the underlying dict via _CodesDict.getattr(), mypy doesn't know about the individual attributes.
In addition, the type annotation Union[_CodesDict, _CID_Dict, Code] causes several other issues in application code. I tend argue that the type of codes.SCT.Tissue should just be Code rather than a complex Union.
hackermd
changed the title
Item "Code" of "Union[_CodesDict, _CID_Dict, Code]" has no attribute "XXX"
Mypy: Item "Code" of "Union[_CodesDict, _CID_Dict, Code]" has no attribute "XXX"
Aug 3, 2021
Thanks for your feedback @scaramallion. We are using this abstraction very frequently in highdicom and I don't consider casting or ignoring viable options. The cast is quite ugly and really defeats the purpose of having this nice abstraction in the first place. Ignoring is a) not ideal and b) doesn't solve the problem entirely, because the Union type is viral and affects other calls downstream, where Union[Code, ...] is expected.
I think we should refactor this and generally avoid return values of Union type. Would it make sense to implement a different class for each hierarchical "level" (_CodesDict, _CID_Dict, Code) so that each __getattr__ would return a value of the appropriate type?
When running mypy on the codes provided via
pydicom.sr.codedict.codes
, I get the following error:Since the attributes are dynamically fetched from the underlying dict via _CodesDict.getattr(), mypy doesn't know about the individual attributes.
In addition, the type annotation
Union[_CodesDict, _CID_Dict, Code]
causes several other issues in application code. I tend argue that the type ofcodes.SCT.Tissue
should just beCode
rather than a complexUnion
.Related to #1251.
The text was updated successfully, but these errors were encountered: