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

False positive unbound variables though all cases are checked with isinstance #8233

Closed
Andrej730 opened this issue Jun 26, 2024 · 2 comments
Closed
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@Andrej730
Copy link

Andrej730 commented Jun 26, 2024

False positive unbound variables, see example below:

class A: ...
class B: ...

# works fine
def test_1(obj: A | B) -> None:
    if isinstance(obj, A):
        new_name = "test"
    elif isinstance(obj, B):
        new_name = "test"
    a = new_name


# breaks
def get_obj() -> A | B: ...
def test_2() -> None:
    obj = get_obj()
    if isinstance(obj, A):
        new_name = "test"
    elif isinstance(obj, B):
        new_name = "test"
    # "new_name" is possibly unbound
    a = new_name
@Andrej730 Andrej730 added the bug Something isn't working label Jun 26, 2024
@erictraut
Copy link
Collaborator

Pyright is working as designed here, so this isn't a bug.

See this documentation for limitations on "implied else" narrowing.

@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2024
@erictraut erictraut added the as designed Not a bug, working as intended label Jun 26, 2024
@Andrej730
Copy link
Author

Okay, I guess a function return type is too implicit for this :(
As a workaround obj: A | B = get_obj() seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants