Skip to content
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

[red-knot] Detect unreachable attributes assignments #15967

Open
Tracked by #14164
sharkdp opened this issue Feb 5, 2025 · 0 comments
Open
Tracked by #14164

[red-knot] Detect unreachable attributes assignments #15967

sharkdp opened this issue Feb 5, 2025 · 0 comments
Labels
red-knot Multi-file analysis & type inference

Comments

@sharkdp
Copy link
Contributor

sharkdp commented Feb 5, 2025

This is probably not a high-priority feature, but it would be great if we could detect attribute assignments in unreachable code sections, and eliminate them from the set of possible answers:

class C:
    def __init__(self) -> None:
        if True:
            self.x = 1
        else:
            self.x = "a"

        return

        self.y = "unreachable"

reveal_type(C().x)  # should be `Unknown | Literal[1]` (i.e. not include `Literal["a"]`)

# Should be an error:
C().y

Note that we have one existing test for this here:

```py
class C:
def __init__(self) -> None:
# We use a "significantly complex" condition here (instead of just `False`)
# for a proper comparison with mypy and pyright, which distinguish between
# conditions that can be resolved from a simple pattern matching and those
# that need proper type inference.
if (2 + 3) < 4:
self.x: str = "a"
# TODO: Ideally, this would result in a `unresolved-attribute` error. But mypy and pyright
# do not support this either (for conditions that can only be resolved to `False` in type
# inference), so it does not seem to be particularly important.
reveal_type(C().x) # revealed: str
```

@sharkdp sharkdp changed the title Hide attributes assignments that occur in statically-known-to-be-false branches [red-knot] Attributes assignments in statically-known branches Feb 5, 2025
@sharkdp sharkdp added the red-knot Multi-file analysis & type inference label Feb 5, 2025
@sharkdp sharkdp changed the title [red-knot] Attributes assignments in statically-known branches [red-knot] Detect unreachable attributes assignments Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

No branches or pull requests

1 participant