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):
"""Add error codes to string messages via class attribute names."""
class ErrorsWithCodes(object):
class ErrorsWithCodes(err_cls):
def __getattribute__(self, code):
if not code.startswith('__'):
msg = getattr(err_cls, code)
return "[{code}] {msg}".format(code=code, msg=msg)
msg = super().__getattribute__(code)
if code.startswith('__'): # python system attributes like __class__
return msg
else:
return super().__getattribute__(code)
return "[{code}] {msg}".format(code=code, msg=msg)
return ErrorsWithCodes()

View File

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