taichino / croniter

croniter is a python module to provide iteration for datetime object.
http://github.com/taichino/croniter
387 stars 105 forks source link

Can't import base exception #136

Closed NicolasCaous closed 4 years ago

NicolasCaous commented 4 years ago

I can't import the croniter base exception on the latest version of croniter (croniter==0.3.31):

>>> from croniter import CroniterError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'CroniterError' from 'croniter' (...\.venv\lib\site-packages\croniter\__init__.py)

python info: Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32

kiorky commented 4 years ago

your error is elsewhere, i think, in croniter related imports that may fail, and then croniter is not loaded.

NicolasCaous commented 4 years ago

Having a quick peek at the __init__.py shows the problem actually:

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .croniter import (
    croniter,
    CroniterBadDateError,  # noqa
    CroniterBadCronError,  # noqa
    CroniterNotAlphaError  # noqa
)  # noqa
croniter.__name__  # make flake8 happy

The base exception CroniterError isn't being exported.

Just to clarify, I want to create a try-catch block that looks like this:

try:
    ...
except CroniterError as ex:
    ...

But since the base class isn't exported, I have to do something ugly like this:

try:
    ...
except (CroniterBadCronError, CroniterBadDateError, CroniterNotAlphaError) as ex:
    ...

Or like this:

try:
    ...
except CroniterBadCronError.__bases__[0] as ex:
    ...

If you could export the base error, that would make exception handling a lot cleaner! 😄