Skip to content

Redefinition doesn't work in else branch of try #18514

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
hannes-ucsc opened this issue Jan 23, 2025 · 1 comment · Fixed by #18515
Closed

Redefinition doesn't work in else branch of try #18514

hannes-ucsc opened this issue Jan 23, 2025 · 1 comment · Fixed by #18515
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code

Comments

@hannes-ucsc
Copy link

hannes-ucsc commented Jan 23, 2025

Bug Report

To Reproduce

https://mypy-play.net/?mypy=1.13.0&python=3.13&flags=allow-redefinition&gist=62f6a12961c14755216efcd52137fee0

import json

def f() -> str:
    try:
        pass
    except ValueError:
        return ''
    else:
        x = b"{'k':'v'}"
        x = json.loads(x)
        return x['k']  # error

def g() -> str:
    if False:
        pass
    else:
        x = b"{'k':'v'}"
        x = json.loads(x)
        return x['k']  # pass

Expected Behavior

No type errors should be reported when running mypy --allow-redefinition.

Actual Behavior

An error is reported after the redefinition of x in the else branch of the try statement even though

the redefinition is in the same block and nesting level as the original definition

quoting the documentation.

main.py:11: error: No overload variant of "__getitem__" of "bytes" matches argument type "str"  [call-overload]
main.py:11: note: Possible overload variants:
main.py:11: note:     def __getitem__(self, SupportsIndex, /) -> int
main.py:11: note:     def __getitem__(self, slice[Any, Any, Any], /) -> bytes
Found 1 error in 1 file (checked 1 source file)

The redefinition in the else branch of an if statement is accepted as documented.

Your Environment

  • Mypy versions used: 1.14.1 and master
  • Mypy command-line flags: --allow-redefinition
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python versions used: 3.12 and 3.13
@hannes-ucsc hannes-ucsc added the bug mypy got something wrong label Jan 23, 2025
@A5rocks A5rocks added the false-positive mypy gave an error on correct code label Jan 23, 2025
sobolevn pushed a commit that referenced this issue Jan 26, 2025
Fixes #18514.

Only `try` clause should be treated as fallible, this should not prevent
`--allow-redefinition` from working in other try clauses (except, else,
finally).
@hannes-ucsc
Copy link
Author

Thank you, @sterliakov, @sobolevn and @A5rocks! Much appreciated!

x612skm pushed a commit to x612skm/mypy-dev that referenced this issue Feb 24, 2025
Fixes python#18514.

Only `try` clause should be treated as fallible, this should not prevent
`--allow-redefinition` from working in other try clauses (except, else,
finally).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants