microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

`NoReturn` behaving unexpectedly after using the decorator with added type annotation #6391

Closed Ailitonia closed 1 month ago

Ailitonia commented 1 month ago

Environment data

Code Snippet

from functools import wraps
from typing import Callable, NoReturn

def no_typing_decorator(func):
    @wraps(func)
    def _wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return _wrapper

def typing_decorator[**P, R](func: Callable[P, R]) -> Callable[P, R]:
    @wraps(func)
    def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        return func(*args, **kwargs)
    return _wrapper

@no_typing_decorator
def no_typing_wrapped_raise_some_exception() -> NoReturn:
    raise RuntimeError

@typing_decorator
def typing_wrapped_raise_some_exception() -> NoReturn:
    raise RuntimeError

def no_typing_test(int_or_str: int | str) -> str:
    if isinstance(int_or_str, int):
        r = no_typing_wrapped_raise_some_exception()

    return int_or_str.strip()  # It's OK, int_or_str: str

def typing_test(int_or_str: int | str) -> str:
    if isinstance(int_or_str, int):
        r = typing_wrapped_raise_some_exception()

    return int_or_str.strip()  # Not fine, int_or_str: int | str

动画3

Repro Steps

  1. input the above code

Expected behavior

The int_or_str variables in lines 33 and 40 both should be recognized as str ​​type

Actual behavior

The int_or_str variables in line 40 still be recognized as int | str ​​type

Logs

Logs ```log 2024-09-15 13:01:19.022 [info] (Client) Pylance async client (2024.9.1) started with python extension (2024.14.1) 2024-09-15 13:01:33.035 [info] (12492) [BG(1)] SemanticTokens full at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:33.272 [info] (12492) [BG(1)] indexing: file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py [found 6] (0ms) 2024-09-15 13:01:33.272 [info] (12492) Indexing Done: file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py 2024-09-15 13:01:33.272 [info] [Info - 13:01:33] (12492) Heap stats: total_memory_size=65345MB, total_free_size=51697MB, total_heap_size=121MB, used_heap_size=100MB, cross_worker_used_heap_size=100MB, total_physical_size=121MB, total_available_size=3992MB, heap_size_limit=4096MB 2024-09-15 13:01:33.294 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:33.775 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:33.842 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:36.728 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:38.547 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (1ms) 2024-09-15 13:01:39.026 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:39.303 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) 2024-09-15 13:01:39.369 [info] (12492) [BG(1)] InlayHints range 0:0 - 40:0 at file:///c%3A/Users/Ailit/Documents/GitHub/omega-miya/foo_decorator.py (0ms) ```
erictraut commented 1 month ago

Thanks for the bug report. I'm surprised this bug hasn't been reported previously. It will be fixed in the next release of pyright.

StellaHuang95 commented 1 month ago

This issue has been fixed in prerelease version 2024.9.101, which we've just released. You can find the changelog here: CHANGELOG.md