1

I encountered with the following error:

NameError: name 'JsonCleaner' is not defined

The line, causing the error is the first pair in dictionary ERROR_CODES_MAPPING_DICT (see code below):

from sshop.engine.models import WrongJsonError

class JsonCleaner:


    class NormalizeError(Exception):

        ERROR_NO_CODE = 0
        ERROR_TYPE_MISMATCH = 1
        ERROR_WRONG_VALUE = 2
        ERROR_LACK_OF_FIELD = 3

        def __init__(self, error_desc, error_code=ERROR_NO_CODE):
            self.error_desc = error_desc
            self.error_code = error_code

        def __unicode__(self):
            return repr(self.error_desc)


    # .... some methods ........


    ERROR_CODES_MAPPING_DICT = {
        # Line where exception is raised:
        JsonCleaner.NormalizeError.ERROR_NO_CODE:       WrongJsonError.NO_ERROR, 
        JsonCleaner.NormalizeError.ERROR_LACK_OF_FIELD: WrongJsonError.ERROR_LACK_OF_FIELD,
        JsonCleaner.NormalizeError.ERROR_TYPE_MISMATCH: WrongJsonError.ERROR_TYPE_MISMATCH,
        JsonCleaner.NormalizeError.ERROR_WRONG_VALUE:   WrongJsonError.ERROR_WRONG_VALUE, 
    }

What am I doing wrong?

3 Answers 3

5

The class name cannot be used within the class scope since the name isn't actually bound until the class is completely defined. Move the dict literal outside the class scope.

JsonCleaner.ERROR_CODES_MAPPING_DICT = ...
Sign up to request clarification or add additional context in comments.

Comments

3

JsonCleaner ist not completely defined at that point and therefor unknown. Just remove it and use NormalizeError.ERROR_NO_CODE and it should work.

Comments

-1

Try replacing "JsonCleaner" with "self".

 ERROR_CODES_MAPPING_DICT = {
    # Line where exception is raised:
    self.NormalizeError.ERROR_NO_CODE:       WrongJsonError.NO_ERROR, 
    self.NormalizeError.ERROR_LACK_OF_FIELD: WrongJsonError.ERROR_LACK_OF_FIELD,
    self.NormalizeError.ERROR_TYPE_MISMATCH: WrongJsonError.ERROR_TYPE_MISMATCH,
    self.NormalizeError.ERROR_WRONG_VALUE:   WrongJsonError.ERROR_WRONG_VALUE, 
}

1 Comment

self doesn't exist either; it only exists within a method since it's passed as one of the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.