Skip to content

Commit 73afab5

Browse files
authored
Fix UnicodeDecodeError.object inferred as str instead of bytes (#2358)
1 parent 32bbc02 commit 73afab5

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ What's New in astroid 3.0.3?
2222
============================
2323
Release date: TBA
2424

25+
26+
* Fix type of ``UnicodeDecodeError.object`` inferred as ``str`` instead of ``bytes``.
27+
28+
Closes pylint-dev/pylint#9342
29+
2530
* Fix ``no-member`` false positives for ``args`` and ``kwargs`` on ``ParamSpec`` under Python 3.12.
2631

2732
Closes pylint-dev/pylint#9401

astroid/interpreter/objectmodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ def attr_path(self):
818818
class UnicodeDecodeErrorInstanceModel(ExceptionInstanceModel):
819819
@property
820820
def attr_object(self):
821-
return node_classes.Const("")
821+
return node_classes.Const(b"")
822822

823823

824824
BUILTIN_EXCEPTIONS = {

tests/test_object_model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,14 @@ def test_oserror(self) -> None:
742742
def test_unicodedecodeerror(self) -> None:
743743
code = """
744744
try:
745-
raise UnicodeDecodeError("utf-8", "blob", 0, 1, "reason")
745+
raise UnicodeDecodeError("utf-8", b"blob", 0, 1, "reason")
746746
except UnicodeDecodeError as error:
747-
error.object[:1] #@
747+
error.object #@
748748
"""
749749
node = builder.extract_node(code)
750750
inferred = next(node.infer())
751751
assert isinstance(inferred, astroid.Const)
752+
assert inferred.value == b""
752753

753754
def test_import_error(self) -> None:
754755
ast_nodes = builder.extract_node(

0 commit comments

Comments
 (0)