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

Replace Rollback with Scope #444

Merged
merged 12 commits into from
Sep 6, 2021
Merged

Replace Rollback with Scope #444

merged 12 commits into from
Sep 6, 2021

Conversation

zhiltsov-max
Copy link
Contributor

Summary

Replaces error rollback utilities with scope utilities. The new class extends the previous situation to be like try-except-finally and allows to enter context managers. This helps to maintain flat code indentation and helps with the objects that require closing / disposal in any cases (i.e. resource managers). The Project class is going to be such a manager - this is needed to resolve problems with file removal on Windows.

How to test

Checklist

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.
  • I have updated the license header for each file (see an example below)
# Copyright (C) 2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

@zhiltsov-max
Copy link
Contributor Author

zhiltsov-max commented Sep 3, 2021

A draft for automatic cm closing:

def test_cm_closing(self):
        import inspect
        this_f = inspect.currentframe()
        begin_values = set(id(v) for v in this_f.f_locals.values())

        f2 = inspect.getframeinfo(this_f)
        q = 5

        end_values = set(id(v) for v in this_f.f_locals.values())
        ids_to_release = end_values - begin_values
        for v in this_f.f_locals.values():
            if id(v) in ids_to_release:
                if hasattr(v, 'close'):
                    v.close()
                elif hasattr(v, '__exit__'):
                    v.__exit__()

Unfortunately, it looks extremely fragile and restricted.

datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
datumaro/util/scope.py Outdated Show resolved Hide resolved
@zhiltsov-max zhiltsov-max mentioned this pull request Sep 3, 2021
7 tasks
tests/test_util.py Outdated Show resolved Hide resolved
tests/test_util.py Outdated Show resolved Hide resolved
tests/test_util.py Outdated Show resolved Hide resolved
@zhiltsov-max
Copy link
Contributor Author

@IRDonch, I refactored and simplified the class.


def on_error_do(self, callback: Callable,
*args, kwargs: Optional[Dict[str, Any]] = None,
ignore_errors: bool = False):
Copy link

Choose a reason for hiding this comment

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

Suggested change
ignore_errors: bool = False):
ignore_errors: bool = False) -> None:

(and ditto for the other None-returning functions)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the point in this?

Copy link

Choose a reason for hiding this comment

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

To help typecheckers find errors.

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Sep 3, 2021

Choose a reason for hiding this comment

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

So you suggest to add it to all functions without return value?

Copy link

Choose a reason for hiding this comment

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

I would say yes (if there are annotations on the function's arguments). Otherwise, the return type defaults to Any.

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Sep 6, 2021

Choose a reason for hiding this comment

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

I'm not convinced it is worthy investment of efforts in most cases. I'm open for a PR on this, but I don't think it is needed for the functions, which names don't not suppose any return value.

datumaro/util/scope.py Outdated Show resolved Hide resolved
@zhiltsov-max zhiltsov-max merged commit 7f2ca57 into develop Sep 6, 2021
@zhiltsov-max zhiltsov-max deleted the zm/add-scope branch September 6, 2021 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants