cs50 / problems

Checks for check50
145 stars 238 forks source link

CS50x Problem Set 9 - check50 tests fail due to `Error` being undefined. #278

Closed Thanasis09 closed 3 months ago

Thanasis09 commented 3 months ago

Hello, I am currently encountering a NameError while running the tests for problem set 9's finance. The error seems to occur at the validate_form method, by the usage of Error, which is not defined.

Error message:

NameError: name 'Error' is not defined
     File "/usr/local/lib/python3.12/site-packages/check50/runner.py", line 148, in wrapper state = check(*args) ^^^^^^^^^^^^
     File "/home/ubuntu/.local/share/check50/cs50/problems/finance/__init__.py", line 28, in register_page Finance().validate_form("/register", ["username", "password", "confirmation"])
     File "/home/ubuntu/.local/share/check50/cs50/problems/finance/__init__.py", line 228, in validate_form raise Error('found more than one field called "{}"'.format(name)) ^^^^^

validate_form method:

def validate_form(self, route, fields, field_tag="input"):
    """Make sure HTML form at `route` has input fields given by `fields`"""
    if not isinstance(fields, list):
        fields = [fields]

    content = self.get(route).content()
    required = {field: False for field in fields}
    for tag in content.find_all(field_tag):
        try:
            name = tag.attrs["name"]
            if required[name]:
                raise Error('found more than one field called "{}"'.format(name))
        except KeyError:
            pass
        else:
            check50.log('found required "{}" field'.format(name))
            required[name] = True

    try:
        missing = next(name for name, found in required.items() if not found)
    except StopIteration:
        pass
    else:
        raise check50.Failure(
            f'expected to find {field_tag} field with name "{missing}", but none found'
        )

    if content.find("button", type="submit") is None:
        raise check50.Failure("expected button to submit form, but none was found")

    return self

To fix, the method should instead return a valid error (in the case the error is simply not imported then it would need to be imported). We could change the raise Error('found more than one field called "{}"'.format(name)) line to raise ValueError('found more than one field called "{}"'.format(name)) for example, or to raise Exception('found more than one field called "{}"'.format(name))

I can submit a PR if need be.

Here is additionally a link to my submission of this problem, that also shows the error: https://submit.cs50.io/check50/c299ed8d503e638e0c2f2be1ed37042555c4b42b

curiouskiwi commented 3 months ago

Thanks for the report! This has now been fixed.

duplicate field