option to disable unreachable code hints #2708
Replies: 11 comments 11 replies
-
Pyright never issues warnings or errors for unreachable code, so there's really nothing to disable here. The LSP standard supports a special type of optional "hint" that can be used to indicate that a text range should be displayed in a "grayed out" manner. Pyright does use this hint if the client claims to support it. The problem is that some clients interpret this hint incorrectly and display these hints as a regular diagnostic. If these clients don't properly support these hints, they should either ignore them or not request them in the first place. |
Beta Was this translation helpful? Give feedback.
-
Judging by the issue history here, I believe they are using VS Code, though potentially not Pylance. |
Beta Was this translation helpful? Give feedback.
-
@DetachHead, perhaps I misinterpreted your request then. Could you provide an example of where you are seeing a warning or error diagnostic for unreachable code? |
Beta Was this translation helpful? Give feedback.
-
@erictraut the error level (info, warning, error, etc.) isn't really relevant to the problem here imo. the issue is that the "hint" or whatever it's called is incorrect in many cases. it'd be like saying a hint like this from the editor isn't a problem simply because it's not a warning: like, not only is that not correct but it's also probably the lsp causing it and not the editor. so in this case, this check implies that it's possible for both branches of the statement to run. why would i write a check like this otherwise? if there's no way to tell the editor that there's more than one supported python version such that neither of these branches are reported as unreachable, i would like an option to disable these hints entirely. if sys.version_info < (3, 8):
print("🧓")
elif sys.version_info == (3,10):
print("✔") and in this case, the hint is obviously 100% incorrect. even though it's obviously doing this because of the type checker, it should have logic to not report this hint on the |
Beta Was this translation helpful? Give feedback.
-
OK, thanks for the explanation. From the perspective of the code analyzer, this code is unreachable, and it does not impact the code analysis. So in that sense, it is not incorrect to indicate that the code is unreachable. The question is whether it's useful to report this to the programmer. I'd argue that it is, but others may see it differently. Consider the following code: if sys.version_info < (3, 10):
foo = None
else:
foo = "hi"
print(foo.capitalize()) If you run this code in Python 3.9, it will crash, but in Python 3.10, it will run without error. Pylance will correctly report the bug if you have a Python 3.9 interpreter selected but will not show any errors if you have a Python 3.10 interpreter selected. That's because the statement When you hover over the grayed-out text, you currently see the text "Code is unreachable". Perhaps you would find the grayed-out text less objectionable and less confusing if we changed that text to say something like "Code is unreachable when run in the selected Python interpreter"? Or is it the presence of the grayed-out text that bothers you? |
Beta Was this translation helpful? Give feedback.
-
i see your point now. i would suggest changing the text to something like "Code is not type-checked" however, since that will also make sense for the though that still only makes sense for these special cases. obviously "Code is unreachable" is still the more accurate message for something like if False:
... |
Beta Was this translation helpful? Give feedback.
-
I had a function with a annotated return type of # foo/bar.py
def myfunc() -> NoReturn:
...
sys.exit() # tests/test_bar.py
def test_myfunc():
myfunc: Callable
from foo.bar import myfunc
... |
Beta Was this translation helpful? Give feedback.
-
Moving this issue to discussion as an enhancement request for comments and upvotes. |
Beta Was this translation helpful? Give feedback.
-
This warning is not precise with objects initialisation. When I define classes' import typing
class MyObject:
def __init__(self, some: str, attr: str) -> typing.NoReturn:
self._some = some
self._attr = attr
class MyObjectManager:
def __init__(self) -> typing.NoReturn:
self._objects: list[MyObject] = []
def add(self, obj: MyObject) -> typing.NoReturn:
self._objects.append(obj)
def create_object(self, some: str, attr: str) -> MyObject:
obj = MyObject(some, attr)
self.add(obj) # this line is unreachable
return obj # this line too
|
Beta Was this translation helpful? Give feedback.
-
pyright now supports this using the btw basedpyright supports a more flexible version of this option called |
Beta Was this translation helpful? Give feedback.
-
This works to suppress all these hints, but I would prefer to suppress these on a case-per-case basis using # type: ignore [reachability] |
Beta Was this translation helpful? Give feedback.
-
there are many problems with pyright's unreachable code detection. see microsoft/pyright#1951, microsoft/pyright#2231, microsoft/pyright#1531, microsoft/pyright#2258
it would be nice to have an option to disable unreachable code warnings. especially since my project already uses mypy to detect unreachable code anyway.
Beta Was this translation helpful? Give feedback.
All reactions