-
Notifications
You must be signed in to change notification settings - Fork 765
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
Provide a way to disable unreachability hints #6106
Comments
The current (new) behavior is correct. Reachability determination is based on type analysis. In your example, you've indicated that If your intent is that |
Thanks for the quick response, Eric. I'm glad to see that it isn't a bug, but I personally disagree with the design choice. I use Is there a preferred way that I could enforce types in Python? I know that Python is not a statically typed language, nor do I need that feature. I just need to avoid runtime errors in the event that a user calls my function using an incorrectly typed parameter. I use Pylance only for type annotations and similar development features. Does it provide other capabilities to help me with types as the code runs? |
Some static analysis tools (type checkers and linters) treat unreachable code as an error. Our philosophy with pyright & pylance is informed by the behavior of TypeScript, which doesn't treat unreachable code as an error. Instead, it treats it as potentially (but not necessarily) a bug. We therefore use "tagged hints" to tell VS Code to display these regions in a subtle gray color. As a developer, you can easily see that static analysis has determined that code is unreachable. You can then make a determination for yourself whether this is something that you expect or not. If not, you can investigate it as a potential bug. In your case, you would expect this to be unreachable from the perspective of a static code analyzer, so you can ignore the fact that it's in gray. In other cases, you may see something in gray and say to yourself, "hey, that looks wrong; I should investigate more closely". This is the same philosophy pyright and pylance have been using since their inception. However, there was a bug that caused pyright to behave inconsistently and not detect unreachable code in certain cases. I recently fixed this bug, which explains why you saw a change in behavior. If you don't want to see any of the "grayed out" text in your Python code, you can set "pyright.disableTaggedHints" to true in your VS Code settings. |
This setting is currently not supported in Pylance, but we could add it. |
Related discussion: #5647 |
I am in the same boat as @SmartLamScott: because of the lack of runtime type checking in Python, I have a ton of statements with this pattern where I check if a variable is not of an allowed type, and then do something. I have found the "code is unreachable" greying out quite helpful for things like code after a return statement, but doing this inference based on unenforceable types feels fundamentally different in kind. I wonder if there is some way to treat these cases differently, or some other middle ground?
If not, then I would greatly appreciate this feature for VS Code. Currently finding it very distracting to have large sections of code greyed out, but I otherwise love Pylance! |
Reopening to track adding support for |
Pyright (the type checker that underlies pylance) offers a backward-compatibility option called strictParameterNoneValue. This is roughly equivalent to |
This issue has been fixed in prerelease version 2024.7.102, which we've just released. You can find the changelog here: CHANGELOG.md |
Which setting should I change to disable unreachability hints? |
Environment data
Code Snippet
Repro Steps
if
statement usingnot isinstance(parameters, hinted_type)
.if
statement are grayed out as if they cannot be executed. The are tagged with the hover text, "Code is unreachable".Expected behavior
The body of the
if
statement should have the default Pylance colors. It should not be tagged with the hover text, "Code is unreachable". Pylance behaves correctly in v2024.6.1, but not pre-release v2024.6.104.Actual behavior
The body of the
if
statement is grayed out. It is tagged with the hover text, "Code is unreachable". If the type in the hint and theisinstance
statement are different, Pylance functions as expected.The text was updated successfully, but these errors were encountered: