Git Product home page Git Product logo

Comments (2)

hemberger avatar hemberger commented on June 23, 2024 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.

jacobtylerwalls avatar jacobtylerwalls commented on June 23, 2024

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.

  1. there's a better way of doing this now that we have constraints in astroid. See pylint-dev/astroid#1189. I think your report should stand as the feature request for that.

  2. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.