Type hinting and casts #1402
Kircheneer
started this conversation in
Ideas
Replies: 2 comments 4 replies
-
@Kircheneer Can you post the mypy message about this line that is failing? |
Beta Was this translation helpful? Give feedback.
3 replies
-
@ktbyers I have found another case where this is interesting. Lots of methods in the NXOS code like the below method. I can't really go and def _send_command(
self, command: str, raw_text: bool = False
) -> Dict[str, Union[str, Dict[str, Any]]]:
"""
Wrapper for NX-API show method.
Allows more code sharing between NX-API and SSH.
"""
return self.device.show(command, raw_text=raw_text) The error Mypy throws for this is the following:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm working on type hinting the NXOS drivers at the moment and I'm now faced with a decision. In
load_merge_candidate
for example there is a piece of code (see below) in which both parameters are Optional (can be None). The first if statement (logically) makes sure, that one of them is not None. Mypy itself does not realize this. Now there's a couple of ways to go about this:typing.cast
as a hint to the type checker (used as a statement in the actual code, isn't run at runtime)assert isinstance(...)
# type: ignore
on the affected lines.I wanted to clarify as to which would be preferred. 2 has actual runtime consequences, while 1 and 3 don't. 3 is the least "invasive" to the look of the code in my opinion, but 1 makes for a much easier time in writing the type hints, because it "sticks" to the variable (as opposed to
#type: ignore
)What's everyone's opinion on this?
Beta Was this translation helpful? Give feedback.
All reactions