FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix faulty casts in checker by hauntsaninja · Pull Request #10560 · python/mypy · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .test  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 5 additions & 2 deletions mypy/checker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2796,7 +2796,9 @@ def check_lvalue(self, lvalue: Lvalue) -> Tuple[Optional[Type],
index_lvalue = None
inferred = None

if self.is_definition(lvalue):
if self.is_definition(lvalue) and (
not isinstance(lvalue, NameExpr) or isinstance(lvalue.node, Var)
):
if isinstance(lvalue, NameExpr):
inferred = cast(Var, lvalue.node)
assert isinstance(inferred, Var)
Expand Down Expand Up @@ -3420,7 +3422,8 @@ def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
source = ('(exception variable "{}", which we do not '
'accept outside except: blocks even in '
'python 2)'.format(var.name))
cast(Var, var.node).type = DeletedType(source=source)
if isinstance(var.node, Var):
var.node.type = DeletedType(source=source)
self.binder.cleanse(var)
if s.else_body:
self.accept(s.else_body)
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-redefine.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,12 @@ with A() as x:
reveal_type(x) # N: Revealed type is "builtins.int"
with B() as x:
x = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str")


[case testRedefineModuleAsException]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Note that if you run this in real life you get:

crash.py:5: error: Incompatible types in assignment (expression has type "Exception", variable has type Module)
Found 1 error in 1 file (checked 1 source file)

I think the fixtures just aren't up to scratch here.

import typing
try:
pass
except Exception as typing:
pass
[builtins fixtures/exception.pyi]
16 changes: 16 additions & 0 deletions test-data/unit/check-unreachable-code.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1305,3 +1305,19 @@ async def f_malformed_2() -> int:

[typing fixtures/typing-full.pyi]
[builtins fixtures/tuple.pyi]


[case testConditionalTypeVarException]
# every part of this test case was necessary to trigger the crash
import sys
from typing import TypeVar

T = TypeVar("T", int, str)

def f(t: T) -> None:
if sys.platform == "lol":
try:
pass
except BaseException as e:
pass
[builtins fixtures/dict.pyi]

Back | FazBrowse Home | New Git URL