Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeDecodeError.object inferred as str instead of bytes #9342

Closed
JulianJvn opened this issue Jan 3, 2024 · 1 comment · Fixed by pylint-dev/astroid#2358
Closed

UnicodeDecodeError.object inferred as str instead of bytes #9342

JulianJvn opened this issue Jan 3, 2024 · 1 comment · Fixed by pylint-dev/astroid#2358
Labels
Bug 🪲 Needs astroid update Needs an astroid update (probably a release too) before being mergable
Milestone

Comments

@JulianJvn
Copy link

JulianJvn commented Jan 3, 2024

Bug description

In #3639, it was reported that accessing UnicodeDecodeError.object resulted in an error. pylint-dev/astroid#797 fixed this, unfortunately incorrectly. Pylint thinks UnicodeDecodeError.object is of type str, when it is actually of type bytes. (A UnicodeDecodeError occurs when decoding a bytes object; str is the result.)

Example code (file test.py):

"""Shows that Pylint incorrectly deduces the type of
``UnicodeDecodeError.object`` as ``str`` instead of ``bytes``."""

try:
    print(b"\x80".decode())
except UnicodeDecodeError as exception:
    print("Error: " + exception.object.hex())

Output when run via python test.py: Error: 80

I don't know anything about astroid, but I'd guess the fix is to change this line as follows:

-return node_classes.Const("")
+return node_classes.Const(b"")

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:7:22: E1101: Instance of 'str' has no 'hex' member (no-member)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Expected behavior

No Pylint errors.

Pylint version

pylint 3.0.3
astroid 3.0.2
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]

OS / Environment

Windows 11, Git Bash

Additional dependencies

No response

@JulianJvn JulianJvn added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 3, 2024
@JulianJvn
Copy link
Author

Confirming that my proposed fix fixes the issue.

@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 Needs astroid update Needs an astroid update (probably a release too) before being mergable and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 4, 2024
@jacobtylerwalls jacobtylerwalls added this to the 3.0.4 milestone Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Needs astroid update Needs an astroid update (probably a release too) before being mergable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants