-
Notifications
You must be signed in to change notification settings - Fork 770
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
Code is unreachable when testing for os.name #470
Comments
By default, Pylance assumes that you will run your program on the same platform as the development environment. This is generally a good assumption and allows for the most accurate completion suggestions, especially in cases where there are platform-specific functions and methods. If you are developing your code for cross-platform execution or for a platform other than the development environment, you can configure Pylance to assume otherwise. To do this, you will need to create a |
Noted, thanks! |
How come things like |
PEP 484 indicates that type checkers are supposed to understand "simple version and platform checks". The exact expression forms were not specified at the time PEP 484 was authored, but in the years since then, that list has been standardized and adopted by all Python type checkers, code analyzers, and typeshed type stubs. It includes:
You will see these forms used consistently in typeshed stubs that describe behaviors that differ between Python versions or platforms. The list doesn't include |
@erictraut ohh thank you for the info! |
Environment data
Expected behaviour
Developing a server process with management commands that should only be run on posix systems. When I test for OS type using
if os.name == 'nt'
, code below that block should show up as normal, with the assumption that it will be reached on a posix server.Ideally, Pylance should not be marking any code as unreachable when using the
os.name
check: it cannot account for which system the code is expected to run on other than the development box where I'm writing it.Actual behaviour
If a
return
or similar statement is used in the code block foros.name == 'nt'
, Pylance marks code below that block as "unreachable" when writing on a Windows system.The same occurs in reverse, as well: if I use
if os.name == 'posix'
, it will mark the inner code block as "unreachable" when writing on a Windows system.Logs
Following is printed to Python Language server logs immediately after uncommenting the
return
statement in below snippet:Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: