Closed pierreBodineau closed 2 months ago
Thanks for the issue but this is by design. Your type indicates the value can only be a str. If you want Pylance to think that it's possible it's not a string, you'd do something like so:
def foo(bar: Any):
if not isinstance(bar, str):
raise TypeError
For more information see this similar issue: https://github.com/microsoft/pylance-release/issues/6131#issuecomment-2227140366
Also see https://github.com/microsoft/pylance-release/issues/6106, which proposes the ability to disable our unreachability hints.
And https://github.com/microsoft/pylance-release/issues/5647 which is an earlier issue discussing reachability from a library author's perspective.
Thanks for the issue but this is by design. Your type indicates the value can only be a str.
I understand that, but python being a dinamically typed language I think these guard clauses can still relevant.
If you want Pylance to think that it's possible it's not a string, you'd do something like so:
def foo(bar: Any): if not isinstance(bar, str): raise TypeError
That would be very counterproductive!
For more information see this similar issue: #6131 (comment)
Similar, but very different though, as it's not raising an error.
Also see #6106, which proposes the ability to disable our unreachability hints.
And #5647 which is an earlier issue discussing reachability from a library author's perspective.
Oh yeah, these are completely my points! Sorry for not having found them by myself before creating this duplicate issue.
Thank you both for your quick answers!
Environment data
Code Snippet
Expected behavior
I'm doing a type check, so the code under the
if
clause is relevant. It shouldn't be shown as unreachable.Actual behavior
Logs