Skip to content

Suppress errors for unreachable branches in conditional expressions #18295

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

Merged

Conversation

brianschubert
Copy link
Collaborator

Fixes #4134
Fixes #9195

Suppress errors when analyzing unreachable conditional expression branches. Same idea as what's done when analyzing the right-hand operand of and/or:

mypy/mypy/checkexpr.py

Lines 4252 to 4256 in 973618a

# If right_map is None then we know mypy considers the right branch
# to be unreachable and therefore any errors found in the right branch
# should be suppressed.
with self.msg.filter_errors(filter_errors=right_map is None):
right_type = self.analyze_cond_branch(right_map, e.right, expanded_left_type)

This PR originally added filters of the same form to the places where analyze_cond_branch is called in ExpressionChecker.visit_conditional_expr. However, since 5 out of the 6 analyze_cond_branch call sites now use filter_errors for the case when map is None, I decided to move the error filtering logic to inside analyze_cond_branch.

Given:

from typing import TypeVar
T = TypeVar("T", int, str)

def foo(x: T) -> T:
    return x + 1 if isinstance(x, int) else x + "a"

Before:

main.py:5:16: error: Unsupported operand types for + ("str" and "int")  [operator]
main.py:5:49: error: Unsupported operand types for + ("int" and "str")  [operator]
Found 2 errors in 1 file (checked 1 source file)

After:

Success: no issues found in 1 source file

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/_internal/concurrency/cancellation.py:240: error: Unsupported operand types for - ("float" and "None")  [operator]

vision (https://github.com/pytorch/vision)
+ torchvision/transforms/v2/functional/_geometry.py:628: error: Unused "type: ignore" comment  [unused-ignore]

jax (https://github.com/google/jax)
+ jax/_src/numpy/linalg.py:393: error: Unused "type: ignore" comment  [unused-ignore]
+ jax/_src/export/shape_poly.py:1642: error: Unused "type: ignore" comment  [unused-ignore]
+ jax/_src/export/shape_poly.py:1661: error: Unused "type: ignore" comment  [unused-ignore]

graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/type/definition.py:793: error: Unused "type: ignore" comment  [unused-ignore]
+ src/graphql/type/definition.py:898: error: Unused "type: ignore" comment  [unused-ignore]
+ src/graphql/type/definition.py:1365: error: Unused "type: ignore" comment  [unused-ignore]

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Nice, thanks for the fix!

@JukkaL JukkaL merged commit 7959a20 into python:master Dec 20, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants