GithubHelp home page GithubHelp logo

Comments (8)

ravilnicki avatar ravilnicki commented on September 2, 2024

It looks like this bug was introduced in version 1.5.0. I tested previous versions and version 1.4.1 is the last one that works as expected.

from mypy.

ravilnicki avatar ravilnicki commented on September 2, 2024

I've found the culprit - 3bf8521.

from mypy.

ravilnicki avatar ravilnicki commented on September 2, 2024

@ikonst please take a look

file mypy/checker.py line 467-

-    if (
-        self.binder.is_unreachable()
-        and self.should_report_unreachable_issues()
-        and not self.is_raising_or_empty(d)
-     ):
-        self.msg.unreachable_statement(d)
-        break
-    self.accept(d)

+    if self.binder.is_unreachable():
+        if not self.should_report_unreachable_issues():
+             break
+         if not self.is_noop_for_reachability(d):
+             self.msg.unreachable_statement(d)
+             break
+    else:
+        self.accept(d)

In the previous version it was possible for self.accept(d) to run even if the self.binder.is_unreachable() was True (eg when self.should_report_unreachable_issues() was False). After your changes when self.binder.is_unreachable() is True there is no possibility to run self.accept(d).

In the example I provided (the one with lambda function) the line after lambda is marked as unreachable which I think is also not right. Nevertheless in versions before 1.5.0 this is not an issue and the next lines are checked.

from mypy.

ikonst avatar ikonst commented on September 2, 2024

You can see me discussing in the PR that maybe the right thing to do is not to avoid type checking unreachable code. I just don't think I ever got to make that change since it seemed larger.

from mypy.

ravilnicki avatar ravilnicki commented on September 2, 2024

How do you think we should proceed? I think that mypy falsely marks the statement after this lambda expression as an unreachable code. So that is the actual bug. In the older versions it went unnoticed though and after your change it stopped checking the presumably unreachable code. I think it shouldn’t be marked as u reachable in the first place. What do you think @ikonst?

from mypy.

ikonst avatar ikonst commented on September 2, 2024

Yeah, fixing the false unreachable is probably the way to go.

from mypy.

andersk avatar andersk commented on September 2, 2024

By moving the code into a function (and adjusting for syntax modernization), I can reproduce the same bug all the way back to the original introduction of NoReturn in 9406886 = v0.500~45.

from mypy_extensions import NoReturn

def foo():
    # type: () -> NoReturn
    raise

def main():
    # type: () -> None
    f = lambda _: foo()  # when this line is commented mypy works as expected
    bar = "baz"  # type: int

from mypy.

andersk avatar andersk commented on September 2, 2024

It looks like the problem is that ExpressionChecker.visit_lambda_expr never pushes a new Frame onto self.chk.binder, and unreachability is associated with a Frame so it leaks outside the lambda body.

Indeed, this effect is noted in a comment there, although the chosen solution only works around unreachability from the implicit return statement and not other kinds of unreachability:

mypy/mypy/checkexpr.py

Lines 5227 to 5235 in 43a605f

# Lambdas can have more than one element in body,
# when we add "fictional" AssigmentStatement nodes, like in:
# `lambda (a, b): a`
for stmt in e.body.body[:-1]:
stmt.accept(self.chk)
# Only type check the return expression, not the return statement.
# This is important as otherwise the following statements would be
# considered unreachable. There's no useful type context.
ret_type = self.accept(e.expr(), allow_none_return=True)

from mypy.

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.