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

False positive unspecified-encoding when passing encoding as kwarg #8719

Closed
jamesbraza opened this issue May 26, 2023 · 4 comments · Fixed by #8728
Closed

False positive unspecified-encoding when passing encoding as kwarg #8719

jamesbraza opened this issue May 26, 2023 · 4 comments · Fixed by #8728
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation performance
Milestone

Comments

@jamesbraza
Copy link

Bug description

When passing an encoding as a kwarg to open, I get unspecified-encoding thrown.

CSV_KWARGS = {"newline": "", "encoding": "utf-8"}
with open("foo.csv", **CSV_KWARGS):
    pass

Configuration

No response

Command used

pylint --score=false a.py

Pylint output

************* Module quick_play
a.py:2:5: W1514: Using open without explicitly specifying an encoding (unspecified-encoding)

Expected behavior

I think it would be cool if pylint wouldn't throw unspecified-encoding in this case.

Pylint version

pylint 2.17.4
astroid 2.15.4
Python 3.10.10 (main, Feb 19 2023, 17:57:18) [Clang 14.0.0 (clang-1400.0.29.102)]

OS / Environment

macOS Monterey v12.6

Additional dependencies

No response

@jamesbraza jamesbraza added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 26, 2023
@Pierre-Sassoulas Pierre-Sassoulas added performance False Positive 🦟 A message is emitted but nothing is wrong with the code Needs decision 🔒 Needs a decision before implemention or rejection and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 27, 2023
@crazybolillo
Copy link
Contributor

I briefly reviewed the code involved and it seems there are two places where a fix could be applied. Either in the checker itself or in utils.get_argument_from_call which is what it uses to retrieve the encoding argument.

Modifying get_argument_from_call to take into account **kwargs when a keyword argument is requested seems nice, but I don't know if it is too big of a change for stable.

@crazybolillo
Copy link
Contributor

crazybolillo commented May 30, 2023

Ok, it seems the actual problem is being able to get the kwargs from the function call. The astroid node only contains the name of the variable used (in the example above CSV_KWARGS), but not its definition (the value it contains).

@Pierre-Sassoulas
Copy link
Member

In order to get the actual values we'll have to use inference. I think we need to make sure we use inference only if absolutely necessary because of the performance impact and the fact that using a dict to pass arguments to open is far from the primary way to do it.

crazybolillo added a commit to crazybolillo/pylint that referenced this issue May 31, 2023
crazybolillo added a commit to crazybolillo/pylint that referenced this issue May 31, 2023
False positives were being generated when passing arguments as
kwargs to open() calls. This has been resolved by using inference inside
`get_argument_from_call`.

This is an opt-in function, for inference to be used, `infer=True` must
be specified when calling `get_argument_from_call`. This ensures that
the rest of the codebase does not suffer a performance hit unnecessarily.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue May 31, 2023
False positives were being generated when passing arguments as
kwargs to open() calls. This has been resolved by using inference inside
`get_argument_from_call`.

This is an opt-in function, for inference to be used, `infer=True` must
be specified when calling `get_argument_from_call`. This ensures that
the rest of the codebase does not suffer a performance hit unnecessarily.

Closes pylint-dev#8719
@crazybolillo
Copy link
Contributor

Ah, inference is a nice thing, learnt something new. I made #8728 which puts it to the test. Let me know if I can do anything else to be of help.

crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 1, 2023
False positives were being generated when passing arguments as
kwargs to open() calls. This has been resolved by using inference inside
`get_argument_from_call`.

This is an opt-in function, for inference to be used, `infer=True` must
be specified when calling `get_argument_from_call`. This ensures that
the rest of the codebase does not suffer a performance hit unnecessarily.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 1, 2023
False positives were being generated when passing arguments as
kwargs to open() calls. This has been resolved by using inference inside
`get_argument_from_call`.

This is an opt-in function, for inference to be used, `infer=True` must
be specified when calling `get_argument_from_call`. This ensures that
the rest of the codebase does not suffer a performance hit unnecessarily.

Closes pylint-dev#8719
@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs decision 🔒 Needs a decision before implemention or rejection labels Jun 1, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.5 milestone Jun 1, 2023
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 2, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 2, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 2, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 2, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes pylint-dev#8719
crazybolillo added a commit to crazybolillo/pylint that referenced this issue Jun 13, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes pylint-dev#8719
Pierre-Sassoulas pushed a commit that referenced this issue Jun 13, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes #8719
github-actions bot pushed a commit that referenced this issue Jun 13, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes #8719

(cherry picked from commit 6fca823)
Pierre-Sassoulas pushed a commit that referenced this issue Jun 14, 2023
False positives were being generated when passing arguments as
kwargs to open() and other IO calls. This has been fixed by
using inference whenever the argument was not found through
previously existing methods (position and keyword) and
kwargs are present.

The confidence levels for both methods with/without inference
have also been updated.

Closes #8719

(cherry picked from commit 6fca823)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation performance
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants