facebook / facebook/pyrefly

bug(stubgen): sys info conditionals are dropped from emitted stubs

Open
#4,204 0 comments 0 reactions 0 assignees View on GitHub
stubgen
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

The [Python typing spec](https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checking) prescribes support for version and platform checking:

> Type checkers are expected to understand simple version and platform checks, e.g.:
>
> ```py
> import sys
>
> if sys.version_info >= (3, 12):
> # Python 3.12+
> else:
> # Python 3.11 and lower
>
> if sys.platform == 'win32':
> # Windows specific definitions
> else:
> # Posix specific definitions
> ```

As such, if a source file contains conditionals like this around publicly declared names, functions, classes, or methods, a stub generated from that source file should retain these conditionals. `pyrefly stubgen` doesn't recognise anything special about these conditionals, so they just get dropped from the stub.

Here's an example version-guarded method stub from `aiohttp`, `if sys.version_info >= (3, 11) and TYPE_CHECKING`: [`aiohttp/client.py`](https://sourcegraph.com/r/github.com/aio-libs/aiohttp/-/blob/aiohttp/client.py?L439).

### Example
#### Input
```python
import sys

FLAG = True
if sys.platform == "win32":
from pkg._windows import PlatformFeature
else:
from pkg._posix import PlatformFeature
```

#### Expected output
```python
import sys

FLAG: bool = True
if sys.platform == "win32":
from pkg._windows import PlatformFeature
else:
from pkg._posix import PlatformFeature
```

#### Actual output
```python
FLAG: bool = True
```

### Related
Split from #3888.

Contributor guide

Open the contributing guide

Research direction

Start at the pyrefly stubgen entry point and reproduce the issue with the Python example in this report. Compare the emitted stub with the expected output, including the sys.platform conditional and both imports. Done means version- and platform-guarded public definitions are retained in generated stubs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.