JoydeepG
JoydeepG

Reputation: 1

While importing methods from another file - AttributeError: 'TestCase' object has no attribute 'assertRaisesRegexp'Cell Execution Error

Problem Summary: I am trying to import two python methods mentioned in another file and receiving the following error,

In Jupyter Notebook (another file named verify.ipynb), I have imported two methods mention within 'common.py' file

from xyzClassifier.constants import *
from xyzClassifier.utils.common import read_yaml, create_directories

Line 1, executed successfully, but when I try to execute both the line, following error shows,

AttributeError: 'TestCase' object has no attribute 'assertRaisesRegexp'Cell Execution Error

IDE, OS and Python version: VS Code in macOS and python 3.12.5

Problem Steps and Code snippet:

  1. I have a file e.g common.py inside folder structure, like, 'xyzClassifier.utils.common'
  2. This file has multiple methods, among them two methods are read_yaml() and create_directories()
  3. I have a .ipynb file, in another directory, which I am using to test (NO UnitTesing) those methods,

but when I try to import those methods in one of the cell of Jupyter notebook (the .ipynb file) by


from xyzClassifier.utils.common import read_yaml, create_directories, 
it is throwing the error, 'AttributeError: 'TestCase' object has no attribute 'assertRaisesRegexp'Cell Execution Error'
  1. I am not doing any UnitTesting

What have I done:

I am expecting these two methods will import successfully. I tried to put those two methods inside try, except block and throwing and catching the exception like, but nothing worked.

except assertRaisesRegexp as arr:
       raise arr
except Exception as e:
        raise e

Detailed error Trace:

{
    "name": "AttributeError",
    "message": "'TestCase' object has no attribute 'assertRaisesRegexp'",
    "stack": "---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[29], line 3

      2 from xyzClassifier.constants import *
----> 3 from xyzClassifier.utils.common import read_yaml, create_directories

      5 import json
      6 import joblib
----> 7 from ensure import ensure_annotations
      8 from box import ConfigBox
      9 from pathlib import Path

      1 from __future__ import absolute_import, division, print_function, unicode_literals
      3 from ._types import NumericString, NumericByteString, IntegerString, IntegerByteString
----> 4 from .main import EnsureError, Ensure, Check, ensure, check, ensure_raises, ensure_raises_regex, ensure_annotations

venv/lib/python3.12/site-packages/ensure/main.py:922
    919 check = Check()
    921 ensure_raises = unittest_case.assertRaises
--> 922 ensure_raises_regex = unittest_case.assertRaisesRegexp

AttributeError: 'TestCase' object has no attribute 'assertRaisesRegexp'"
}

Any help will be highly appreciated.

Upvotes: 0

Views: 57

Answers (1)

CrazyChucky
CrazyChucky

Reputation: 3518

You may not be doing any unit testing yourself, but the ensure library is. It needs to be updated to version 1.0.4 in order to support Python 3.12.

Python 3 has long kept assertRaisesRegexp (which was the name in Python 2) as an alias to assertRaisesRegex, for backwards compatibility. In 3.12 it was finally removed, but prior versions of ensure were still using it.

Upvotes: 1

Related Questions