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

Option to ignore errors when resolving pending registrations while accessing methods attribute #139

Closed
sylvorg opened this issue Apr 9, 2024 · 4 comments

Comments

@sylvorg
Copy link

sylvorg commented Apr 9, 2024

Hello!

Sorry for bothering you so many times! 😅

I was wondering if it would be possible to have the option to ignore errors such as NameErrors from forward references while accessing the methods attribute of a dispatched function. I'm using the autoslot package which takes all the assignments to self variables in a class' __init__ method and and adds them to __slots__; however, if I use a forward reference to the same class I'm defining the __init__ method for, I get a NameError. For example:

# Adapted From:
# Answer: https://stackoverflow.com/a/829729/10827766
# User: https://stackoverflow.com/users/37984/saffsd
class Counter(metaclass=plumeta):
    def __init__(self, count: "Counter"):
        self.count = count.count

    def __init__(self, count: Int):
        self.count = count

    def __init__(self, count: Iterable):
        self.count = len(count)

    def __nonzero__(self):
        self.count -= 1
        return self.count >= 0

    def __str__(self):
        return str(self.count)

    def __repr__(self):
        return repr(self.count)

    def __rich_repr__(self):
        yield self.__str__()

    __bool__ = __nonzero__

Since I'm using Counter as a forward reference in __init__, it errors out when accessing __init__'s methods attribute. Would it be possible to implement a method that will ignore NameErrors due to forward references at the user's peril, and move on to the next method in the methods' list (like just the last two __init__ methods in my example instead of all three), or is that beyond the scope of the project?

Thank you kindly for the help!

@sylvorg
Copy link
Author

sylvorg commented Apr 10, 2024

I realize now that the _pending attribute may also work, as the collected methods seem not to be resolved during class initialization. I'll go ahead and close this, then.

@sylvorg sylvorg closed this as completed Apr 10, 2024
@wesselb
Copy link
Member

wesselb commented Apr 21, 2024

Hey @sylvorg! You're indeed right that _pending could be used for that: _pending collects methods without registering them.

Let me know if you run into any issues using _pending. :)

@sylvorg
Copy link
Author

sylvorg commented Apr 27, 2024

Combining _pending and _resolved worked perfectly, though perhaps a property _all might be helpful to combine the two, just to access all the function details in the current dispatcher!

@wesselb
Copy link
Member

wesselb commented Apr 27, 2024

@sylvorg That's great to hear :)

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

2 participants