Comments (2)
Thanks for the response! Your suggested patch does indeed fix the more complex conditional case, though the simple case still produces a false negative. I'll see if I can create a test from the more complex case, and if so then I'll submit a PR.
from pylint.
This change was made in #4764 to address false positives where the if
guards are performing a type-narrowing function.
There are two follow-ups I see immediately from your report, though.
-
there's a better way of doing this now that we have
constraints
inastroid
. See pylint-dev/astroid#1189. I think your report should stand as the feature request for that. -
Regarding:
This can also happen for more complex conditionals, where it's not clear whether or not the branch is reachable.
We might be able to solve that by leveraging a recent feature:
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py
index 9c73fee82..e559a8bdd 100644
--- a/pylint/checkers/typecheck.py
+++ b/pylint/checkers/typecheck.py
@@ -513,7 +513,7 @@ def _emit_no_member(
parent: nodes.NodeNG = node.parent
while parent != scope:
if isinstance(parent, (nodes.If, nodes.IfExp)):
- inferred = safe_infer(parent.test)
+ inferred = safe_infer(parent.test, compare_constants=True)
if ( # pylint: disable=too-many-boolean-expressions
isinstance(inferred, nodes.Const)
and inferred.bool_value() is False
Would you be willing to see if that solves your use case? And if so, open a modest PR attaching a functional test? Many thanks!
from pylint.
Related Issues (20)
- Suggestion conflicting with black 2024 style for inlined semantic / empty classes HOT 6
- False Positive: `no-member` for `ParamSpec` under Python 3.12 HOT 8
- [unknown-option-value] False negative after `--disable=all` HOT 1
- [unsubscriptable-object] FP for generic classes (PEP 695) HOT 2
- pyreverse working with attrs.org
- [duplicate-code] unclear how to disable the message for select occurrences, while keeping the option enabled in general
- Feature Request: Naming Convention Enforcement for Boolean Values & Functions HOT 8
- Pylint incorrectly resolving type vs instances
- astroid.node Attribute Error HOT 1
- protected-access false positive in class method of child class
- unused-argument in .pyi stub file HOT 1
- too-many-try-statements should exclude `pass` statements if required by Python HOT 2
- `too-few-public-methods` counts dunder methods as private HOT 2
- `unexpected-keyword-arg` (E1123) and `missing-kwoa` (E1125) false positives with changing dict
- [Enhancement] `newline` argument for `csv.reader()`
- pylint's type checker ignores type interface files HOT 2
- False positive: overriding a generic `ParamSpec` method triggers `arguments-differ` (W0221)
- false positive: no-member when importing from matplotlib.cm
- False positive: import-error and no-name-in-module when importing from python-docx HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pylint.