1

Python doesn't allow raising functions as exceptions but PyLint still has a check for it here. Under what conditions can the above line of code be hit?

I tried raising an exception like:

def test_func():
    """Test-only function for checking function objects can be raised."""
    pass
raise test_func

But Python raises an error: "exceptions must be old-style classes or derived from BaseException, not type"

Rishav
  • 11
  • 2
  • 2
    What are you trying to achieve here? – Devesh Kumar Singh Jul 17 '19 at 17:15
  • I am trying to test that line but it seems unreachable. How can I reach that line? – Rishav Jul 17 '19 at 17:22
  • Read up on https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python and see if it helps you, you cannot raise a function which is defined like that – Devesh Kumar Singh Jul 17 '19 at 17:24
  • Yes I read that answer. My question is then why does Pylint has a check for raising functions as exceptions and under what condition can that line be reached? – Rishav Jul 17 '19 at 17:27
  • 1
    This is the commit referered to that change https://github.com/PyCQA/pylint/commit/aad5ed6b519d9c8a941182bcd44fef992e193553 See if it makes sense – Devesh Kumar Singh Jul 17 '19 at 19:15

0 Answers0