Fix arg-type, return-value, and assignment mypy errors#651
Fix arg-type, return-value, and assignment mypy errors#651bizfsc wants to merge 2 commits intocanopen-python:masterfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
- nmt.py: Use bytes() instead of list[int] for send_message/send_periodic - emcy.py: Fix wait() return type to Optional[EmcyError] - network.py: Fix add_node/create_node return types and type narrowing - variable.py: Fix missing return in read(), annotate Bits.raw, add type: ignore for inherently dynamic type operations - sdo/base.py: Fix missing return in get_variable(), add type: ignore for SdoArray.__len__ - objectdictionary/__init__.py: Fix __getitem__ indexability, add missing return in get_variable(), annotate ODVariable.parent, fix decode_phys/encode_phys types, add type: ignore for dynamic encode_raw operations
7d78ff8 to
b43cf9f
Compare
acolomb
left a comment
There was a problem hiding this comment.
Some good stuff in here, but also questionable decisions. Especially the # type: ignore stuff makes me want to fix the underlying problem instead of hiding the error. Some of the assertions may already be remedied by #657, and we need to find an overall strategy for handling the general type flexibility inherent in the API (where #652 does give a good overview).
I think we'll pick some of the uncontroversial changes from this, but not all changes here align with my vision of a "good solution". Need a bit more time to pick them apart, though.
| return self[idx][sub] | ||
| parent = self[idx] | ||
| if not isinstance(parent, (ODRecord, ODArray)): | ||
| raise KeyError(f"{pretty_index(index)} was not found in Object Dictionary") |
There was a problem hiding this comment.
Is this really better than a TypeError? I think it's pretty clear to see "not indexable" in a message where a sub-element of some object was requested via dot notation.
This method must be questioned at a deeper level. The So it is in fact desirable to allow passing back unmodified non-numeric values. On the other hand, why should the factor only be applied to integer types, but not if the underlying variable is already a float? As for typing, would generics help us here? If an integer or float is passed, the result is definitely numeric (depending on the factor), otherwise it's the same as the argument type. This could probably be expressed via
This overlaps with #648. Regarding type checks, an assertion after the |
Fix
arg-type,return-value,assignment,return,index, andoperatormypy errors across the codebase.Changes
canopen/nmt.pybytes()instead oflist[int]forsend_message()andsend_periodic()calls.b''instead ofNonefor node guarding remote frame data.canopen/emcy.pywait()return type:Optional[EmcyError](wasEmcyError, but returnsNoneon timeout).canopen/network.pyadd_node()return type toUnion[RemoteNode, LocalNode](accepts both).create_node()parameter type toUnion[int, LocalNode].assert node.id is not Nonefor index safety.type: ignore[arg-type]for node construction where OD may be None.canopen/variable.pyraise ValueErrorat end ofread()to satisfy missing return.Bits.rawwith proper type.isinstancecheck forvalue_descriptionsdict access.type: ignorefor dynamically typed operations (decode_phys,decode_desc,encode_bits).canopen/sdo/base.pyreturn Noneinget_variable().type: ignore[return-value]forSdoArray.__len__().canopen/objectdictionary/__init__.py__getitem__dot-notation access withisinstancecheck for indexability.return Noneinget_variable().ODVariable.parentandODArray.parentwith proper Union types.decode_phys()return type toUnion[int, float].encode_phys()without intermediate division.type: ignoreforencode_rawstring operations and min/max comparisons.type: ignorefordest.close()andsource.namein file handling.Impact
Reduces mypy error count from 95 to 51 (standalone), and combined with PRs #647, #649, #650 will bring the count down significantly further.",
false