Skip to content
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

Use deepcopy instead of a shallow copy for middlewares, #2349 #2354

Merged
merged 2 commits into from
Aug 5, 2024

Conversation

coder2020official
Copy link
Collaborator

@Badiboy
Copy link
Collaborator

Badiboy commented Aug 4, 2024

I'm not sure that is the best way.

As far as I understand - you want this to allow middleware developer to change values of data in HIS handler. For other cases that is not needed and will just make overhead.

Is it more reasonable to "say" developers who want to change data values in HIS handler to make deepcopy BEFORE make changes?...

I'm not against it. Deepcopy is more safe way, but I'm in a humble because I'm not sure that LIBRARY should care about it...

@coder2020official
Copy link
Collaborator Author

Well, suppose the dev is adding some data, for post_process.
And this data is not being used in handler.
This will cause the data to be lost. However, if deep copy is used - changes made in the deep copy dictionary in _run_middlewares_and_handlers won't affect original data

@Badiboy Badiboy merged commit 96761f8 into eternnoir:master Aug 5, 2024
7 checks passed
@@ -558,7 +558,7 @@ async def _run_middlewares_and_handlers(self, message, handlers, middlewares, up
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
return
else:
data_copy = data.copy()
data_copy = copy.deepcopy(data)
Copy link

@Tishka17 Tishka17 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should no copy values inside data. It is required to pass original objects retrieved from middlewares to handlers. They can be mutated and it is expected behavior. That is how you pass objectes to handlers instead of global variables

@coder2020official
Copy link
Collaborator Author

@Badiboy sorry for the inconvenience, we might need to revert this PR
Could you please do this, since I currently can't

@coder2020official
Copy link
Collaborator Author

Or maybe let's wait for me to get back to work, I'll be available to check disadvantages of this again on Sunday
Discard previous comment

@Badiboy
Copy link
Collaborator

Badiboy commented Aug 8, 2024

I think it may wait for Sunday.

@Badiboy
Copy link
Collaborator

Badiboy commented Aug 8, 2024

As I said from the very beginnig: you are trying to think about all developer's needs yourself. It not looks like a good practice, because in most cases you cannot even imagine all cases how the library is used by all hundreds of thousands of developers.

That's why I always propose as much flexibility for developers as it possible. In this case I still think it's better to leave decision about making deepcopy to developer of middleware...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants