Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Avoid falsepossibly-undefined errors due to omitted unrequired else statements.#20149

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tyralla wants to merge7 commits intopython:master
base:master
Choose a base branch
Loading
fromtyralla:fix/possibly_undefined_superfluous_else_block

Conversation

@tyralla
Copy link
Collaborator

@tyrallatyralla commentedOct 30, 2025
edited
Loading

Fixes#14771

The mentioned issue'sexcept: assert_never examples are already fixed by#15995. This PR only targets the "no unreachable else" examples.

Introducing the special-purpose attributeIfStmt.else_irrelevant_for_possibly_undefined is not super nice, but it appears more straightforward and less risky than starting to introduce empty else blocks withis_unreachable = True (as discussedhere).

@github-actions

This comment has been minimized.


if_map,else_map=self.find_isinstance_check(e)

s.else_irrelevant_for_possibly_undefined=else_mapisNone
Copy link
Collaborator

@sterliakovsterliakovOct 31, 2025
edited
Loading

Choose a reason for hiding this comment

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

Soelse_irrelevant_for_possibly_undefined is essentially equivalent toelse_unreachable oris_exhaustive? I'm a bit confused by this naming

Copy link
CollaboratorAuthor

@tyrallatyrallaOct 31, 2025
edited
Loading

Choose a reason for hiding this comment

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

Oh yes, I should have changed this. When I started working on this PR, I thought that theassert_never cases and similar ones would also need similar treatment. Soelse_irrelevant_for_possibly_undefined meant "unreachable or does not return" to me. I will change it later. Thanks!

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

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

done

@sterliakov
Copy link
Collaborator

sterliakov commentedOct 31, 2025
edited
Loading

That's a nice idea overall. Can I suggest setting the same flag inreachability::infer_reachability_of_if_statement if theif is true and there is noelse? Now we mark all the remaining blocks unreachable, so this flag should also become True there. And then_get_executable_if_block_with_overloads infastparse.py can make use of it...

Now that was one path I found that checks for "unreachable else". There may be more, so maybe introducing a dummy emptyelse is a reasonable alternative to consider?

I tried grepping forelse_body\.is_unreachable and there are only two hits, one mentioned above and one in dataclasses plugin (where this flag won't be helpful?), so maybe_get_executable_if_block_with_overloads is indeed the only other usage site. Or maybe not if there is some other access pattern...

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@tyralla
Copy link
CollaboratorAuthor

That's a nice idea overall. Can I suggest setting the same flag inreachability::infer_reachability_of_if_statement if theif is true and there is noelse? Now we mark all the remaining blocks unreachable, so this flag should also become True there. And then_get_executable_if_block_with_overloads infastparse.py can make use of it...

Now that was one path I found that checks for "unreachable else". There may be more, so maybe introducing a dummy emptyelse is a reasonable alternative to consider?

I am not sure about this.

Semantic analysis already adds fakeelse_body blocks:

# Make sure else body always exists and is marked as
# unreachable so the type checker always knows that
# all control flow paths will flow through the if
# statement body.
ifnots.else_body:
s.else_body=Block([])
mark_block_unreachable(s.else_body)

is_unreachable is a pure product of the semantic analysis step, whileunreachable_else is a type checking result that can (at least I think so) frequently change its value (e.g. in repeatedly visited loops). My feeling is that we should keep things separated.

Copy link
Collaborator

@sterliakovsterliakov left a comment

Choose a reason for hiding this comment

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

OK, makes sense! I'd maybe consider a separate PR refactoringreachability.py to no longer add fakeelse blocks, but that might also be too much of effort for little gain. Could you please add a backlink comment toreachability.py to mention that these two strategies coexist now? It might be surprising later to see that one place adds fakeelse blocks and another place only uses a flag to do the same

expr:list[Expression]
body:list[Block]
else_body:Block|None
unreachable_else:bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a brief comment explaining the attribute here? It would be nice to immediately see a notice with this attr's meaning and "volatile" nature

Copy link
CollaboratorAuthor

@tyrallatyrallaNov 15, 2025
edited
Loading

Choose a reason for hiding this comment

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

Again, sorry for the delay. I added some explanations that hopefully clarify things.

@github-actions
Copy link
Contributor

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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ilinumilinumAwaiting requested review from ilinum

@hauntsaninjahauntsaninjaAwaiting requested review from hauntsaninja

1 more reviewer

@sterliakovsterliakovsterliakov approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

possibly-undefined rule should respect exhaustive checking

2 participants

@tyralla@sterliakov

[8]ページ先頭

©2009-2025 Movatter.jp