fixup! Fix ErrorsWithCodes().__class__ return value

This commit is contained in:
Ilia Ivanov 2020-05-14 15:45:58 +02:00
parent a987e9e45d
commit 712d9d4820
2 changed files with 9 additions and 5 deletions

View File

@ -5,13 +5,13 @@ from __future__ import unicode_literals
def add_codes(err_cls): def add_codes(err_cls):
"""Add error codes to string messages via class attribute names.""" """Add error codes to string messages via class attribute names."""
class ErrorsWithCodes(object): class ErrorsWithCodes(err_cls):
def __getattribute__(self, code): def __getattribute__(self, code):
if not code.startswith('__'): msg = super().__getattribute__(code)
msg = getattr(err_cls, code) if code.startswith('__'): # python system attributes like __class__
return "[{code}] {msg}".format(code=code, msg=msg) return msg
else: else:
return super().__getattribute__(code) return "[{code}] {msg}".format(code=code, msg=msg)
return ErrorsWithCodes() return ErrorsWithCodes()

View File

@ -1,5 +1,7 @@
from inspect import isclass from inspect import isclass
import pytest
from spacy.errors import add_codes from spacy.errors import add_codes
@ -10,4 +12,6 @@ class Errors(object):
def test_add_codes(): def test_add_codes():
assert Errors.E001 == "[E001] error description" assert Errors.E001 == "[E001] error description"
with pytest.raises(AttributeError):
Errors.E002
assert isclass(Errors.__class__) assert isclass(Errors.__class__)