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

Request: flake8-encodings #3278

Open
henryiii opened this issue Feb 28, 2023 · 3 comments
Open

Request: flake8-encodings #3278

henryiii opened this issue Feb 28, 2023 · 3 comments
Labels
needs-decision Awaiting a decision from a maintainer plugin Implementing a known but unsupported plugin

Comments

@henryiii
Copy link
Contributor

henryiii commented Feb 28, 2023

I noticed https://pypi.org/project/flake8-encodings/ was missing when porting a package over to Ruff. This checks for encoding= missing from open (and configparser.ConfigParser and pathlib.Path) calls.

Here are the checks:

ENC001 = "ENC001 no encoding specified for 'open'."
ENC002 = "ENC002 'encoding=None' used for 'open'."
ENC003 = "ENC003 no encoding specified for 'open' with unknown mode."
ENC004 = "ENC004 'encoding=None' used for 'open' with unknown mode."

ENC011 = "ENC011 no encoding specified for 'configparser.ConfigParser.read'."
ENC012 = "ENC012 'encoding=None' used for 'configparser.ConfigParser.read'."

ENC021 = "ENC021 no encoding specified for 'pathlib.Path.open'."
ENC022 = "ENC022 'encoding=None' used for 'pathlib.Path.open'."
ENC023 = "ENC023 no encoding specified for 'pathlib.Path.read_text'."
ENC024 = "ENC024 'encoding=None' used for 'pathlib.Path.read_text'."
ENC025 = "ENC025 no encoding specified for 'pathlib.Path.write_text'."
ENC026 = "ENC026 'encoding=None' used for 'pathlib.Path.write_text'."

This is an extremely common need (it's available at runtime with PYTHONWARNDEFAULTENCODING (PEP 597) and is needed for Python 3.15 compatibility PEP 686), so I'm surprised no existing checks seem to cover it; wouldn't be surprised if my searching skills were just not good enough.

Edit: Ahh, this might be the not-yet-implemented unspecified-encoding / W1514 from pylint.

@charliermarsh
Copy link
Member

Python 3.15!

@charliermarsh charliermarsh added the plugin Implementing a known but unsupported plugin label Feb 28, 2023
@charliermarsh charliermarsh added the needs-decision Awaiting a decision from a maintainer label Jul 10, 2023
@pwuertz
Copy link

pwuertz commented Jan 31, 2024

Should this be detected when enabling PLW1514 in current versions of ruff? The PR #3416 does seem to include some tests for issues with Path, but it didn't make it into ruff yet?

@RubenVanEldik
Copy link
Contributor

I would like to bump this request!

Currently when you have some code such as:

import pathlib

with open(pathlib.Path("path/to/file.txt")) as f:
    print(f)

Ruff will raise 2 issues:

  1. PLW1514 open in text mode without explicit encoding argument
  2. PTH123 open() should be replaced by Path.open()

If you first solve the second issue, the first issue will be gone, which led me to believe that Path.open() doesn't require it, which is not the case!

Would the Ruff team be open to include the flake8-encodings ruleset? And do you think this could be a good first issue? If so I would be willing to pick it up (it would mean it will take a while before this will be implemented though 🙃 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer plugin Implementing a known but unsupported plugin
Projects
None yet
Development

No branches or pull requests

4 participants