Skip to content

assert not flag -> Cannot determine type of "x" [has-type] #18782

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
ikamensh opened this issue Mar 11, 2025 · 3 comments
Closed

assert not flag -> Cannot determine type of "x" [has-type] #18782

ikamensh opened this issue Mar 11, 2025 · 3 comments
Labels
bug mypy got something wrong

Comments

@ikamensh
Copy link

ikamensh commented Mar 11, 2025

Bug Report

Usecase: I found a bug and want to temporarily disable an execution branch in my code. I use assert not flag when I initialize my class. I expect this not to create new type errors.

To Reproduce

https://gist.github.com/mypy-play/c1ba8168e409b445cf187af04a4b42d8

or

class Bar:
    def __init__(self, b: bool) -> None:
        assert not b

        if b:
            self.x = "1"
        else:
            self.x = "2"  # main.py:8: error: Cannot determine type of "x" [has-type]

Expected Behavior

There is no type error in this code.

Actual Behavior

Failed (exit code: 1) (3473 ms)
main.py:8: error: Cannot determine type of "x" [has-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

mypy playground: mypy 1.15.0, python3.12

@ikamensh ikamensh added the bug mypy got something wrong label Mar 11, 2025
@Protim44
Copy link

Failed (exit code: 1) (3473 ms)
main.py:8: error: Cannot determine type of "x" [has-type]
Found 1 error in 1 file (checked 1 source file)

@sterliakov
Copy link
Collaborator

Minimal repro (the class is necessary, doesn't rep with a local variable):

class Bar:
    def __init__(self) -> None:
        if False:
            self.x = "1"  # E: Statement is unreachable  [unreachable]
        else:
            self.x = "2"  # E: Cannot determine type of "x" [has-type]

If an instance attribute is first set in an unreachable block, future attempts to set or read it fail with [has-type].

@A5rocks
Copy link
Collaborator

A5rocks commented Mar 11, 2025

Duplicate of #9709

@A5rocks A5rocks marked this as a duplicate of #9709 Mar 11, 2025
@A5rocks A5rocks closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants