Repository navigation
[Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises) #2275
Description
Activity
Please also show us the content of your
meetup.py. But currently I'd say it is missing a definition for theMeetupDayException.\cc @exercism/python
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 + 1I 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.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
MeetupDayExceptionclass.To get around this (temporarily) you can add the following to the top of your
meetup_test.pyfile, directly below the line that saysfrom meetup import meetup, MeetupDayException, and removeMeetupDayExceptionfrom 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
ValueErrorswhere 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
MeetupDayExceptionClass by subclassingException. 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 calledmeetup_check()(which is what I renamed yourmeetup()function to). You probably want to be more targeted in the errors you catch/raise -- so you can raise the exception specifically/directly asValueErrorsorIndexErrorsget 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,
BethanyReacted by DanOK... 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 !
@iHiD this issue may be moved to exercism/python
@BethanyG I would like to leave this open as a reference issue until we've made the changes we talked about.
Reacted by BethanyGThis issue has been automatically marked as
abandonedbecause it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.Going to comment here since we still have changes that require this to be open. Removing the [abandoned] tag.
- changed the title
[-]ImportError while importing test module[/-][+][V2] ImportError while importing test module[/+]on Jan 29, 2021 - addedmaintainer action required❕A maintainer needs to take action on this.A maintainer needs to take action on this.
on Jun 16, 2021 - addedsmolder🍲Lower-priority and/or future ideaLower-priority and/or future idea
on May 21, 2022 - changed the title
[-][V2] ImportError while importing test module[/-][+][Error Handling]: ImportError while importing test module (Error-Handling Messaging & Exercises)[/+]on May 21, 2022
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:
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.