Skip to content

Allow redefinitions in except/else/finally - #18515

Merged
sobolevn merged 3 commits into
python:masterfrom
sterliakov:bugfix/st-allow-redefinition-try-clauses
Jan 26, 2025
Merged

sobolevn merged 3 commits into
python:masterfrom
sterliakov:bugfix/st-allow-redefinition-try-clauses

Conversation

@sterliakov

Copy link
Copy Markdown
Collaborator

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).

# E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar")
reveal_type(x) # N: Revealed type is "typing.TypeVar"
y = 1
# NOTE: '"int" not callable' is due to test stubs

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.

This note no longer applies, necessary stubs were connected in #17384.

@github-actions

This comment has been minimized.

@sterliakov
sterliakov marked this pull request as ready for review January 23, 2025 15:35
Comment thread test-data/unit/check-redefine.test Resolved
@github-actions

This comment has been minimized.

a = ""
a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
except:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please, also test except *

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@sobolevn
sobolevn merged commit 0451880 into python:master Jan 26, 2025
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request 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

None yet

3 participants