Skip to content

Fix typeguards crash when assigning guarded value in if statement #10671

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> ProperType:
assert False, "This should be never called, got {}".format(t)

def visit_type_guard_type(self, t: TypeGuardType) -> ProperType:
assert False, "This should be never called, got {}".format(t)
return t.type_guard.accept(self)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is right, since for join(T, TypeGuard[U]) it will end up with join(T, U), which is approximately Union[T, U] in practice. Computing join(T, bool) may make more sense.

I'm not sure what practical effect this has though. It's at least clear that we'll have to make sure we handle TypeGuard correctly in more places in the codebase.

Copy link
Collaborator Author

@A5rocks A5rocks Jun 19, 2021

Choose a reason for hiding this comment

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

The thing is, it's (in the example crash code) saying a is TypeGuard[B] -- when it's actually B (?). (and then it is joining the type with the type of a in the outer scope). However, when I changed that (3f068f5) some of the test cases started erroring... in specific, some of the narrowing ones:

_____________________________________________________________ testTypeGuardNonOverlapping _____________________________________________________________
[gw6] linux -- Python 3.8.2 /home/a5/.virtualenvs/tmp-7d419bcf0dbd427/bin/python
data: /home/a5/oss/mypy/test-data/unit/check-typeguard.test:78:
/home/a5/oss/mypy/mypy/test/testcheck.py:137: in run_case
    self.run_case_once(testcase)
/home/a5/oss/mypy/mypy/test/testcheck.py:236: in run_case_once
    assert_string_arrays_equal(output, a, msg.format(testcase.file, testcase.line))
E   AssertionError: Unexpected type checker output (/home/a5/oss/mypy/test-data/unit/check-typeguard.test, line 78)
---------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------
Expected:
  main:6: note: Revealed type is "builtins.list[builtins.str]" (diff)
Actual:
  main:6: note: Revealed type is "None"         (diff)

Alignment of first line difference:
  E: ...te: Revealed type is "builtins.list[builtins.str]"
  A: ...te: Revealed type is "None"
                              ^
______________________________________________________________ testTypeGuardNonzeroFloat ______________________________________________________________
[gw6] linux -- Python 3.8.2 /home/a5/.virtualenvs/tmp-7d419bcf0dbd427/bin/python
data: /home/a5/oss/mypy/test-data/unit/check-typeguard.test:105:
/home/a5/oss/mypy/mypy/test/testcheck.py:137: in run_case
    self.run_case_once(testcase)
/home/a5/oss/mypy/mypy/test/testcheck.py:236: in run_case_once
    assert_string_arrays_equal(output, a, msg.format(testcase.file, testcase.line))
E   AssertionError: Unexpected type checker output (/home/a5/oss/mypy/test-data/unit/check-typeguard.test, line 105)
---------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------
Expected:
  main:5: note: Revealed type is "builtins.float" (diff)
Actual:
  main:5: note: Revealed type is "builtins.int" (diff)

Alignment of first line difference:
  E: ...ed type is "builtins.float"
  A: ...ed type is "builtins.int"
                             ^
___________________________________________________________ testTypeGuardNarrowToTypedDict ____________________________________________________________
[gw6] linux -- Python 3.8.2 /home/a5/.virtualenvs/tmp-7d419bcf0dbd427/bin/python
data: /home/a5/oss/mypy/test-data/unit/check-typeguard.test:155:
/home/a5/oss/mypy/mypy/test/testcheck.py:137: in run_case
    self.run_case_once(testcase)
/home/a5/oss/mypy/mypy/test/testcheck.py:236: in run_case_once
    assert_string_arrays_equal(output, a, msg.format(testcase.file, testcase.line))
E   AssertionError: Unexpected type checker output (/home/a5/oss/mypy/test-data/unit/check-typeguard.test, line 155)
---------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------
Expected:
  main:10: note: Revealed type is "TypedDict('__main__.User', {'name': builtins.str, 'id': builtins.int})" (diff)
Actual:
  main:10: note: Revealed type is "None"        (diff)

Alignment of first line difference:
  E: ...ote: Revealed type is "TypedDict('__main__.User', {'name': builtins.s...
  A: ...ote: Revealed type is "None"...
                               ^

Because of that, I kind of assumed that TypeGuard[T] is just T -- I guess that isn't the case though? If so, then this might be more annoying to fix...


def join(self, s: Type, t: Type) -> ProperType:
return join_types(s, t)
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-typeguard.test
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,17 @@ def coverage(obj: Any) -> bool:
return True
return False
[builtins fixtures/classmethod.pyi]

[case testTypeGuardAssignment]
from typing_extensions import TypeGuard

class A: pass
class B(A): pass

def guard(a: A) -> TypeGuard[B]:
pass

a = A()
if not guard(a):
a = A()
[builtins fixtures/tuple.pyi]