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

[Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises) #2275

Open
dan9731 opened this issue Nov 27, 2020 · 8 comments
Assignees
Labels
maintainer action required❕ A maintainer needs to take action on this. maintainer chore 🔧 smolder🍲 Lower-priority and/or future idea

Comments

@dan9731
Copy link

dan9731 commented Nov 27, 2020

Good day. I first offer the obligatory "I'm not GitHub-savvy" apology. I did look for an answer as prescribed here, following steps in subheading Opening an Issue. But I have not seen anything. I've seen some fixes on Stack Overflow. But they make no sense to me. Please don't crush me if I'm doing this wrong.

I'm working on a new linux install. On my previous installation, I ran pytest successfully many times (28 to be precise). With this new installation, I've run it once successfully on exercise Twelve Days of Christmas. It's giving me this error now:

$ /home/dan/.local/bin/pytest meetup_test.py 
============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/dan/exercism/python/meetup
collected 0 items / 1 error                                                    

==================================== ERRORS ====================================
_______________________ ERROR collecting meetup_test.py ________________________
ImportError while importing test module '/home/dan/exercism/python/meetup/meetup_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
meetup_test.py:4: in <module>
    from meetup import meetup, MeetupDayException
E   ImportError: cannot import name 'MeetupDayException' from 'meetup' (/home/dan/exercism/python/meetup/meetup.py)
=========================== short test summary info ============================
ERROR meetup_test.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.09s ===============================
$

I tried removing the meetup directory and re-downloading from the CLI. I get this error no matter what the contents of meetup.py are. So, I'm guessing that I have some issue either with they way pytest is configured or with my meetup_pytest file.

Any assistance greatly appreciated.

Thank you so much.
Dan.

@NobbZ
Copy link
Member

NobbZ commented Nov 27, 2020

Please also show us the content of your meetup.py. But currently I'd say it is missing a definition for the MeetupDayException.

\cc @exercism/python

@dan9731
Copy link
Author

dan9731 commented Nov 28, 2020

Hi, Norbert. Thank you for the reply. Here is my code.

from calendar import day_name, Calendar
from datetime import date

def meetup(year, month, week, day_of_week):
    desired_day = [day for day in day_name].index(day_of_week)
    desired_week = [which for which in ['last', '1st', '2nd', '3rd', '4th', '5th', 'teenth']].index(week)

    cal = Calendar()
    cal = list(cal.monthdays2calendar(year, month))

    if desired_week == 6:
        for week in cal:
            for day in week:
                if day[0] in range(13, 20) and day[1] == desired_day:
                    return date(year, month, week[desired_day][0])
                    

    if desired_week == 0:
        cal = cal[::-1] # Reverse the calendar weeks if last __day desired

    i = 1
    for week in cal:
        if week[desired_day][0] != 0 and i == desired_week or desired_week == 0:
            return date(year, month, week[desired_day][0])
        elif week[desired_day][0] != 0:
            i = i + 1

I see the line you're referring to for the import error. Normally, I fill in the function definitions as provided in the .py file for the exercise. Am I supposed to create a MeetupDayException?

Thank you,
Dan.

@BethanyG
Copy link
Member

BethanyG commented Nov 28, 2020

Hi @dan9731 👋

Since @NobbZ is in CET, I'm picking this up. (I'm in PST). Apologies - the test file for this exercise appears to have been updated since you last worked on/ran it, and it is indeed expecting you to define a MeetupDayException class.

To get around this (temporarily) you can add the following to the top of your meetup_test.py file, directly below the line that says from meetup import meetup, MeetupDayException, and remove MeetupDayException from the import line, so that the two look like this:

from meetup import meetup

try:  
 from meetup import MeetupDayException  
except ImportError:  
 MeetupDayException = Exception

That way, you can run and work on any tests that might be failing right now for you. When I ran your code, it looks as though 7 of the 90 tests have failed -- mostly for ValueErrors where the day is out of range for the month.

To fully pass all the tests without the test file modification, you will need to make a custom MeetupDayException Class by subclassing Exception. Something along the lines of this:

class MeetupDayException(Exception):
    """Exception raised when the meetup day is not valid or in range..

    Attributes:
        day -- the day that is causing the error
        message -- explanation of the error
    """

    def __init__(self, day, message="Isn't valid as a proposed meetup date.  Please try another."):
        self.day = day
        self.message = message
        super().__init__(self.message)



def meetup(year, month, week, day_of_week):
    try:
        meetup = meetup_check(year, month, week, day_of_week)
    except Exception as err:
        raise MeetupDayException(f'{day_of_week}, week {week}, in {month}, of {year}')

As you can see, I've made a function that is named meetup() that calls a function called meetup_check() (which is what I renamed your meetup() function to). You probably want to be more targeted in the errors you catch/raise -- so you can raise the exception specifically/directly as ValueErrors or IndexErrors get caught within your code -- but this was quicker for me to demonstrate.

For more detail on how to create/customize and handle errors, take a look at the docs here.

Please let us know if this works for you, and if you have any additional questions or issues.

Best,
Bethany

@dan9731
Copy link
Author

dan9731 commented Nov 28, 2020

OK... I think I get it. I was able to mend my code to get pytest working with both the modifications to the pytest file you suggested and also after adding the MeetupDayException class. And I was able to weed out those ValueErrors too. =)

This was very helpful.

Thank you so much, @BethanyG !

@cmccandless
Copy link
Contributor

@iHiD this issue may be moved to exercism/python

@iHiD iHiD transferred this issue from exercism/exercism Nov 30, 2020
@cmccandless
Copy link
Contributor

@BethanyG I would like to leave this open as a reference issue until we've made the changes we talked about.

@github-actions
Copy link
Contributor

This issue has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@BethanyG
Copy link
Member

Going to comment here since we still have changes that require this to be open. Removing the [abandoned] tag.

@BethanyG BethanyG removed the pinned label Jan 29, 2021
@BethanyG BethanyG changed the title ImportError while importing test module [V2] ImportError while importing test module Jan 29, 2021
@BethanyG BethanyG added maintainer action required❕ A maintainer needs to take action on this. maintainer chore 🔧 labels Jun 16, 2021
@exercism exercism deleted a comment from github-actions bot Mar 1, 2022
@BethanyG BethanyG added the smolder🍲 Lower-priority and/or future idea label May 21, 2022
@BethanyG BethanyG changed the title [V2] ImportError while importing test module [Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises) May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintainer action required❕ A maintainer needs to take action on this. maintainer chore 🔧 smolder🍲 Lower-priority and/or future idea
Projects
None yet
Development

No branches or pull requests

4 participants