Why doesn't pylint warn me about using nonexistent class members?

476 views Asked by At

I'm coming to python from a C++ background, and I like to use tools like pylint to catch obvious errors that a compiler would normally catch. However, pylint considers this code to be okay:

#!/usr/bin/env python
"""Test module"""

class MyException(Exception):
    """A custom exception"""
    def __init__(self, error_code, message):
        self.error_code = error_code
        super(MyException, self).__init__(message)

try:
    raise MyException(123, 'One Two Three')
except MyException as err:
    print err.errCode
    print err.message

The problem being, of course, that "print err.errCode" results in runtime error.

Is this just an oversight in pylint's checking, or is there something about python that would make referencing the seemingly nonexistent errCode valid?

Obviously you could be catching a subclass of MyException which included that field, but if you're in a scope with an explicit MyException object, I would think you would not want to be referring to stuff that might exist in a subclass. It's certainly not an error (I can think of many scenarios where you might want to might take different actions based on the capabilities of a function's parameter), but it certainly seems warning-worthy.

0

There are 0 answers