-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Extract Duplicate Code
into Helper Function in ivy\func_wrapper.py
#26572
refactor: Extract Duplicate Code
into Helper Function in ivy\func_wrapper.py
#26572
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Compliance Checks Passed!
Thank you for this PR, here is the CI results: This pull request does not result in any additional test failures. Congratulations! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good to me, but I'll request @RickSanchezStoic's review just in case this breaks the casting modes. Thanks @Sai-Suraj-27 😄
ivy/func_wrapper.py
Outdated
return downcast_helper("complex", dtype, "complex", intersect) | ||
|
||
|
||
def downcast_helper(arg0, dtype, arg2, intersect): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's change this to arg1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RickSanchezStoic sir, I made the requested changes. Please check now, Thank you.
ivy/func_wrapper.py
Outdated
|
||
|
||
def downcast_helper(arg0, dtype, arg2, intersect): | ||
index = casting_modes_dict[arg0]().index(dtype) - 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
@RickSanchezStoic sir, its been a week since I made the requested changes. Please look into the PR when you are free, Thank you. |
Hey @Sai-Suraj-27, can we refactor this further to have a single helper which receives a flag about whether to use the upcast or downcast logic? Seems like the |
@vedpatwardhan sir, After observing carefully I see that only in the following case of https://github.com/unifyai/ivy/blob/19cb1a48f27cf4b3ed27b07fb81b7d7a10d52fba/ivy/func_wrapper.py#L139 |
I think that's just a mistake, can't really think of a reason why it would be |
@vedpatwardhan Sorry for the late response, I have refactored further to use only a single helper function. Please check now. |
ivy/func_wrapper.py
Outdated
index = casting_modes_dict[arg]().index(dtype) + step | ||
result = "" | ||
if is_upcast: | ||
while index < len(casting_modes_dict[arg]()): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ig we could replace this with a single loop instead of 2 where the condition would change based on the value of is_upcast
. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of the same but I wasn't sure if index
will be always >= 0. If the index value is always >= 0, then we can remove the if-else
condition and merge the 2 loops to the following:
result = ""
while 0 <= index < len(casting_modes_dict[arg]()):
if casting_modes_dict[arg]()[index] not in intersect:
result = casting_modes_dict[arg]()[index]
break
index += step
return result
Please tell me your view, Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's safe to assume that index
will always be >=0
😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the changes. Can you please look into it now, Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! Feel free to merge the PR @Dsantra92, thanks @Sai-Suraj-27 😄
Hey, @vedpatwardhan You approved this PR almost a month back but no one merged it. Can you please merge this, thank you. |
Yep merging the PR now, thanks @Sai-Suraj-27 😄 |
PR Description
I identified duplicate sections of code in the following functions:
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L94
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L137
So, I extracted these into their own method/function to increase the readability and decrease the amount of duplicate/repeating code in the file. This refactoring simplifies the code, and reduces redundancy.
Related Issue
Close #26573
Checklist
Socials
https://twitter.com/Sai_Suraj_27