Skip to content

Commit 3e16c95

Browse files
Fix UnicodeDecodeError.object inferred as str instead of bytes (#2358) (#2373)
(cherry picked from commit 73afab5) Co-authored-by: JulianJvn <[email protected]>
1 parent 80a556d commit 3e16c95

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
@@ -13,6 +13,11 @@ What's New in astroid 3.0.3?
1313
============================
1414
Release date: TBA
1515

16+
17+
* Fix type of ``UnicodeDecodeError.object`` inferred as ``str`` instead of ``bytes``.
18+
19+
Closes pylint-dev/pylint#9342
20+
1621
* Fix ``no-member`` false positives for ``args`` and ``kwargs`` on ``ParamSpec`` under Python 3.12.
1722

1823
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)