Add warning when users have incomplete setup for type checking (#166603)

Looking for feedback on this approach.
Received user reports of spurious pyrefly errors for users using hg instead of git. I think this was due to the fact that when using a venv and git, `make setup-env` installs requirements and pulls from a nightly torch wheel, which is needed for pyrefly to type check properly.

Initial documentation for `make setup-env` I found here: https://github.com/pytorch/pytorch/blob/main/CONTRIBUTING.md#developing-pytorch

Testing:
```
hg clone --git ssh://git@github.com/pytorch/pytorch.git
conda create -n pytorch_env python=3.10 # (or manually create venv instead of using script)
cd pytorch
pip install -r requirements.txt
pip install -r requirements-build.txt
lintrunner init
# check how many pyrefly errors - 15,709 errors (11,693 ignored)
lintrunner # confirm error message / warning appears
>>> General linter failure:
  Warning (PYREFLY) nightly-wheel-not-run
    pytorch-nightly.pth not found. You may need to run make setup-env or make
    setup-env-conda to install nightly binaries and type stubs.
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/166603
Approved by: https://github.com/aorenste
This commit is contained in:
Maggie Moss 2025-10-30 20:37:40 +00:00 committed by PyTorch MergeBot
parent 0d50e5d8d4
commit 80ba6e458f

View File

@ -106,6 +106,56 @@ def check_pyrefly_installed(code: str) -> list[LintMessage]:
]
def check_nightly_run(code: str) -> list[LintMessage]:
"""Check if make setup-env has been run successfully.
This checks for the presence of pytorch-nightly.pth file that is created
when installing nightly binaries and type stubs.
"""
import site
from pathlib import Path
try:
site_packages = Path(site.getsitepackages()[0])
pth_file = site_packages / "pytorch-nightly.pth"
if not pth_file.exists():
return [
LintMessage(
path=None,
line=None,
char=None,
code=code,
severity=LintSeverity.WARNING,
name="nightly-wheel-not-run",
original=None,
replacement=None,
description=(
"pytorch-nightly.pth not found. "
"You may need to run make setup-env "
"to install nightly binaries and type stubs."
),
)
]
return []
except Exception as e:
return [
LintMessage(
path=None,
line=None,
char=None,
code=code,
severity=LintSeverity.ERROR,
name="command-failed",
original=None,
replacement=None,
description=(
f" Could not check for pytorch-nightly {e.__class__.__name__}:\n{e}"
),
)
]
def in_github_actions() -> bool:
return bool(os.getenv("GITHUB_ACTIONS"))
@ -247,6 +297,11 @@ def main() -> None:
stream=sys.stderr,
)
nightly_check = check_nightly_run(args.code)
if len(nightly_check) != 0:
print(json.dumps(nightly_check[0]._asdict()), flush=True)
return
lint_messages = check_pyrefly_installed(args.code) + check_files(
args.code, args.config
)