🎨 fix type hints for optional method parameters #3234
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR updates the type hints for optional method parameters across the codebase. The previous pattern
param: str = None
has been replaced withparam: Optional[str] = None
for improved clarity and type checking.The pattern of saying
param: str = None
is a bit of a code smell, since we're telling the compiler it is a string, when it can be (and is by default) None.Usually not a problem, but type checkers can mistakenly indicate code to be unreachable:
^ Note that the last line is greyed out. It has message:
This is because we've given the type hint
EndpointType
, and the compiler thinks that can never be "falsey".To fix, we should tell the compiler it is an
Optional[EndpointType]
.Apart from type checking, it just makes the code more clear and self-explanatory.
So, I used regex to replace
(\w+): (\w+) = None
with$1: Optional[$2] = None
, and add import to modules that need it. Separately replaced List[..] = None types. Only applied changes to aries_cloudagent folder, not demo.