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

Getting unspecified-encoding with Path().open("w") #5599

Closed
yarnabrina opened this issue Dec 25, 2021 · 3 comments
Closed

Getting unspecified-encoding with Path().open("w") #5599

yarnabrina opened this issue Dec 25, 2021 · 3 comments
Labels
Invalid Not a bug, already exists or already fixed

Comments

@yarnabrina
Copy link

yarnabrina commented Dec 25, 2021

I understood from #5017 that it is expected to be solved by version 2.11.1, but I'm facing same issues even now. I do not get this with binary files ("wb") though.

Reproducible Example

Code

"""Script to highlight misleading pylint warning."""

import pathlib

FILE = pathlib.Path("hello_world.txt")

with FILE.open("w") as FILE_STREAM:
    FILE_STREAM.write("hello world")

Folder Structure

❯ tree
.
└── hello_world.py

0 directories, 1 file

Configuration

None

Invocation Command

pylint --enable=all hello_world.py

(Same happens without --enable=all as well)

Pylint Output

❯ pylint --enable=all hello_world.py
************* Module hello_world
hello_world.py:7:5: W1514: Using open without explicitly specifying an encoding (unspecified-encoding)

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

Expected Output

None, with perfect score

Setup Information

Pylint Version

❯ pylint --version
pylint 2.12.2
astroid 2.9.0
Python 3.10.0 (default, Nov 10 2021, 11:24:47) [Clang 12.0.0 ]

OS Version

macOS Monterey 12.0.1

@yarnabrina yarnabrina changed the title False positive unspecified-encoding Getting unspecified-encoding with Path().open("w") Dec 25, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Dec 25, 2021
@DanielNoord
Copy link
Collaborator

I believe this is expected behaviour. You're not specifying that the file should be openend in binary mode, so it's openend in text mode making an encoding "required". The issue you're referring to indicated that we did not recognise the binary mode parameter correctly. This has been resolved.

@DanielNoord DanielNoord added Invalid Not a bug, already exists or already fixed and removed False Positive 🦟 A message is emitted but nothing is wrong with the code labels Dec 25, 2021
@yarnabrina
Copy link
Author

yarnabrina commented Dec 25, 2021

I do not want to open it in binary mode at all. I want to open it in text mode only, and it is a completely valid python code. It should not raise any warning at all, and IMHO this issue should not be closed.

If it is an "expected behaviour", how exactly will you specify the encoding without getting an warning? @DanielNoord @Pierre-Sassoulas

(And I know binary mode parameter is solved. Said so myself in the main thread already.)

@DanielNoord
Copy link
Collaborator

DanielNoord commented Dec 25, 2021

@yarnabrina Whether you open in binary or text mode is referred to as the mode in which you open. The encoding is the codec used to open the file. You can fix it (for example) by doing open("w", encoding="utf-8").
Please also the official Python documentation which explains the difference between them here.

Those docs also refer to as to why we have this check in the first place: default encodings differ per platform and therefore without specifying your encoding your code might fail or run differently on other platforms than the one you're currently working on. Even though it is technically valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, already exists or already fixed
Projects
None yet
Development

No branches or pull requests

3 participants