Skip to content

Commit

Permalink
Add exception inference for UnicodeDecodeError (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed May 29, 2020
1 parent 340649c commit 765c67f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Release Date: TBA

* Added more supported parameters to ``subprocess.check_output``

* Added exception inference for `UnicodeDecodeError`

Close PyCQA/pylint#3639


What's New in astroid 2.4.2?
============================
Expand Down
7 changes: 7 additions & 0 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,16 @@ def attr_path(self):
return node_classes.Const("")


class UnicodeDecodeErrorInstanceModel(ExceptionInstanceModel):
@property
def attr_object(self):
return node_classes.Const("")


BUILTIN_EXCEPTIONS = {
"builtins.SyntaxError": SyntaxErrorInstanceModel,
"builtins.ImportError": ImportErrorInstanceModel,
"builtins.UnicodeDecodeError": UnicodeDecodeErrorInstanceModel,
# These are all similar to OSError in terms of attributes
"builtins.OSError": OSErrorInstanceModel,
"builtins.BlockingIOError": OSErrorInstanceModel,
Expand Down
11 changes: 11 additions & 0 deletions tests/unittest_object_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,17 @@ def test_oserror(self):
assert isinstance(inferred, astroid.Const)
assert inferred.value == value

def test_unicodedecodeerror(self):
code = """
try:
raise UnicodeDecodeError("utf-8", "blob", 0, 1, "reason")
except UnicodeDecodeError as error:
error.object[:1] #@
"""
node = builder.extract_node(code)
inferred = next(node.infer())
assert isinstance(inferred, astroid.Const)

def test_import_error(self):
ast_nodes = builder.extract_node(
"""
Expand Down

0 comments on commit 765c67f

Please sign in to comment.