okpy / ok-client

A Python client for the OK autograding system
https://okpy.org/
Apache License 2.0
57 stars 42 forks source link

Dont recurse on self/im_self #469

Closed pamelafox closed 2 years ago

pamelafox commented 2 years ago

Class decorators are currently causing recursion errors due to the load_tests recursive logic, and the fact that a bound method refers back to the original class in both the self and im_self attributes. This adds a condition to ignore those attributes.

Example problematic code before:

class RandomCat:
    """
    Returns a new instance of a Cat with the given owner,
    a randomly chosen name and a random number of lives.
    >>> randcat = RandomCat.adopt_random_cat("Ifeoma")
    >>> isinstance(randcat, RandomCat)
    True
    >>> randcat.owner
    'Ifeoma'
    """
    cat_names = ["felix", "bugs", "grumpy"]

    def __init__(self, name, owner, lives=9):
        self.is_alive = True
        self.name = name
        self.owner = owner
        self.lives = lives

    @classmethod
    def adopt_random_cat(cls, owner):
        # BEGIN SOLUTION NO PROMPT ALT="___________________________"
        cat_name = RandomCat.cat_names[len(owner) % len(RandomCat.cat_names)]
        num_lives = len(cat_name) + len(owner_name)
        # END SOLUTION
        # BEGIN SOLUTION NO PROMPT ALT="return cls(____, ____, ____)"
        return cls(cat_name, owner, num_lives)
        # END SOLUTION