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

Private/protected functions inaccessible through list comprehensions #165

Open
LeicaSimile opened this issue Apr 22, 2023 · 0 comments
Open

Comments

@LeicaSimile
Copy link

Calls to functions marked as private/protected throw an error when inside a list comprehension. Example:

class Test:
    def a(self):
        return [self.b(x) for x in "12345"]

    @private
    def b(self, x):
        return f"{x}."

 
t = Test()
t.a()

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in a
  File "<stdin>", line 3, in <listcomp>
  File "/.venvs/test/lib/python3.10/site-packages/accessify/access.py", line 75, in private_wrapper
    raise InaccessibleDueToItsProtectionLevelException(
accessify.errors.InaccessibleDueToItsProtectionLevelException: Test.b() is inaccessible due to its protection level

In accessify, it seems to be raised here:

# access.py
            ...
            method_caller_frame = inspect.currentframe().f_back
            method_caller_class = get_method_class_by_frame(frame=method_caller_frame)

            if instance_class is not method_caller_class:
                raise InaccessibleDueToItsProtectionLevelException(
                    INACCESSIBLE_DUE_TO_ITS_PROTECTION_LEVEL_EXCEPTION_MESSAGE.format(
                        class_name=instance_class.__name__, class_method_name=method.__name__,
                    ),
                )

Removing the private/protected decorator makes the error go away, as does rewriting the list comp to a for-loop assignment. Perhaps related to Python 3's scoping for list comprehensions - can there be a workaround?

Tested on Python 3.10.6

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

No branches or pull requests

1 participant