Skip to content
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

Pylance reports code is unreachable in if/else blocking checking sys.platform #6943

Closed
boydgreenfield opened this issue Feb 13, 2025 · 1 comment
Assignees

Comments

@boydgreenfield
Copy link

Environment data

  • Language Server version: 2024.8.1
  • OS and version: darwin arm64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.indexing: true
  • python.analysis.typeCheckingMode: off

Code Snippet

patch_args = ["patch", "--fuzz", "0", "--unified"]
if sys.platform == "darwin":
    patch_args.append("--check")
else:
    patch_args.append("--dry-run")

This code works but feels, well, obtuse:

patch_args = ["patch", "--fuzz", "0", "--unified"]
platform: str = sys.platform
if platform == "darwin":
    patch_args.append("--check")
else:
    patch_args.append("--dry-run")

Expected behavior

No problem raised on if sys.platform ... block

Actual behavior

Problem is raised.

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Feb 13, 2025
@debonte
Copy link
Contributor

debonte commented Feb 13, 2025

This is by design. There's some discussion about this in these pyright issues:

The key part IMO is this:

Pyright needs to make assumptions about the value of sys.platform during analysis because many type stub files (including the typeshed stub files for stdlib) use sys.platform to conditionalize code for different platforms. By default, Pyright assumes that sys.platform is "" (empty string). This is an appropriate assumption for code that is intended to run on all platforms. If you intend to run your code on a specific platform, you can specify the target in the pythonconfig.json file using the pythonPlatform setting. If you set this to "Linux", Pyright will assume that sys.platform == 'linux'.

@debonte debonte closed this as completed Feb 13, 2025
@debonte debonte added by design and removed needs repro Issue has not been reproduced yet labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants